1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include <data/casewriter.h>
20 #include <data/casewriter-provider.h>
24 #include <libpspp/taint.h>
28 struct casewriter_translator
30 struct casewriter *subwriter;
32 struct ccase *(*translate) (struct ccase *, void *aux);
33 bool (*destroy) (void *aux);
37 static const struct casewriter_class casewriter_translator_class;
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
45 The cases returned by TRANSLATE must match OUTPUT_PROTO.
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.
51 When the translating casewriter is destroyed, DESTROY will be
52 called to allow any state maintained by TRANSLATE to be freed.
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. */
58 casewriter_create_translator (struct casewriter *subwriter,
59 const struct caseproto *translated_proto,
60 struct ccase *(*translate) (struct ccase *,
62 bool (*destroy) (void *aux),
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;
71 writer = casewriter_create (translated_proto,
72 &casewriter_translator_class, ct);
73 taint_propagate (casewriter_get_taint (ct->subwriter),
74 casewriter_get_taint (writer));
79 casewriter_translator_write (struct casewriter *writer UNUSED,
80 void *ct_, struct ccase *c)
82 struct casewriter_translator *ct = ct_;
83 c = ct->translate (c, ct->aux);
85 casewriter_write (ct->subwriter, c);
89 casewriter_translator_destroy (struct casewriter *writer UNUSED, void *ct_)
91 struct casewriter_translator *ct = ct_;
92 casewriter_destroy (ct->subwriter);
93 ct->destroy (ct->aux);
97 static struct casereader *
98 casewriter_translator_convert_to_reader (struct casewriter *writer UNUSED,
101 struct casewriter_translator *ct = ct_;
102 struct casereader *reader = casewriter_make_reader (ct->subwriter);
104 ct->destroy (ct->aux);
108 static const struct casewriter_class casewriter_translator_class =
110 casewriter_translator_write,
111 casewriter_translator_destroy,
112 casewriter_translator_convert_to_reader,