Change "union value" to dynamically allocate long strings.
[pspp-builds.git] / src / data / casewriter-translator.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <data/casewriter.h>
20 #include <data/casewriter-provider.h>
21
22 #include <stdlib.h>
23
24 #include <libpspp/taint.h>
25
26 #include "xalloc.h"
27
28 struct casewriter_translator
29   {
30     struct casewriter *subwriter;
31
32     struct ccase *(*translate) (struct ccase *, void *aux);
33     bool (*destroy) (void *aux);
34     void *aux;
35   };
36
37 static const struct casewriter_class casewriter_translator_class;
38
39 /* Creates and returns a new casewriter whose cases are passed
40    through TRANSLATE, based on INPUT and auxiliary data AUX.
41    (TRANSLATE may also return a null pointer, in which case no
42    case is written to the output.)  The translated cases are then
43    written to SUBWRITER.
44
45    The cases returned by TRANSLATE must match OUTPUT_PROTO.
46
47    TRANSLATE takes ownership of each case passed to it.  Thus, it
48    should either unref each case and return a new case, or
49    (unshare and then) modify and return the same case.
50
51    When the translating casewriter is destroyed, DESTROY will be
52    called to allow any state maintained by TRANSLATE to be freed.
53
54    After this function is called, SUBWRITER must not ever again
55    be referenced directly.  It will be destroyed automatically
56    when the translating casewriter is destroyed. */
57 struct casewriter *
58 casewriter_create_translator (struct casewriter *subwriter,
59                               const struct caseproto *translated_proto,
60                               struct ccase *(*translate) (struct ccase *,
61                                                           void *aux),
62                               bool (*destroy) (void *aux),
63                               void *aux)
64 {
65   struct casewriter_translator *ct = xmalloc (sizeof *ct);
66   struct casewriter *writer;
67   ct->subwriter = casewriter_rename (subwriter);
68   ct->translate = translate;
69   ct->destroy = destroy;
70   ct->aux = aux;
71   writer = casewriter_create (translated_proto,
72                               &casewriter_translator_class, ct);
73   taint_propagate (casewriter_get_taint (ct->subwriter),
74                    casewriter_get_taint (writer));
75   return writer;
76 }
77
78 static void
79 casewriter_translator_write (struct casewriter *writer UNUSED,
80                              void *ct_, struct ccase *c)
81 {
82   struct casewriter_translator *ct = ct_;
83   c = ct->translate (c, ct->aux);
84   if (c != NULL)
85     casewriter_write (ct->subwriter, c);
86 }
87
88 static void
89 casewriter_translator_destroy (struct casewriter *writer UNUSED, void *ct_)
90 {
91   struct casewriter_translator *ct = ct_;
92   casewriter_destroy (ct->subwriter);
93   ct->destroy (ct->aux);
94   free (ct);
95 }
96
97 static struct casereader *
98 casewriter_translator_convert_to_reader (struct casewriter *writer UNUSED,
99                                          void *ct_)
100 {
101   struct casewriter_translator *ct = ct_;
102   struct casereader *reader = casewriter_make_reader (ct->subwriter);
103   free (ct);
104   ct->destroy (ct->aux);
105   return reader;
106 }
107
108 static const struct casewriter_class casewriter_translator_class =
109   {
110     casewriter_translator_write,
111     casewriter_translator_destroy,
112     casewriter_translator_convert_to_reader,
113   };