1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007 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/casereader.h>
23 #include <data/casereader-provider.h>
24 #include <libpspp/taint.h>
28 /* Casereader that applies a user-supplied function to translate
29 each case into another in an arbitrary fashion. */
31 /* A translating casereader. */
32 struct casereader_translator
34 struct casereader *subreader; /* Source of input cases. */
36 void (*translate) (const struct ccase *input, struct ccase *output,
38 bool (*destroy) (void *aux);
42 static struct casereader_class casereader_translator_class;
44 /* Creates and returns a new casereader whose cases are produced
45 by reading from SUBREADER and passing through TRANSLATE, which
46 must create case OUTPUT, with OUTPUT_VALUE_CNT values, and
47 populate it based on INPUT and auxiliary data AUX. TRANSLATE
48 must also destroy INPUT.
50 When the translating casereader is destroyed, DESTROY will be
51 called to allow any state maintained by TRANSLATE to be freed.
53 After this function is called, SUBREADER must not ever again
54 be referenced directly. It will be destroyed automatically
55 when the translating casereader is destroyed. */
57 casereader_create_translator (struct casereader *subreader,
58 size_t output_value_cnt,
59 void (*translate) (const struct ccase *input,
62 bool (*destroy) (void *aux),
65 struct casereader_translator *ct = xmalloc (sizeof *ct);
66 struct casereader *reader;
67 ct->subreader = casereader_rename (subreader);
68 ct->translate = translate;
69 ct->destroy = destroy;
71 reader = casereader_create_sequential (
72 NULL, output_value_cnt, casereader_get_case_cnt (ct->subreader),
73 &casereader_translator_class, ct);
74 taint_propagate (casereader_get_taint (ct->subreader),
75 casereader_get_taint (reader));
79 /* Internal read function for translating casereader. */
81 casereader_translator_read (struct casereader *reader UNUSED,
82 void *ct_, struct ccase *c)
84 struct casereader_translator *ct = ct_;
87 if (casereader_read (ct->subreader, &tmp))
89 ct->translate (&tmp, c, ct->aux);
96 /* Internal destroy function for translating casereader. */
98 casereader_translator_destroy (struct casereader *reader UNUSED, void *ct_)
100 struct casereader_translator *ct = ct_;
101 casereader_destroy (ct->subreader);
102 ct->destroy (ct->aux);
106 /* Casereader class for translating casereader. */
107 static struct casereader_class casereader_translator_class =
109 casereader_translator_read,
110 casereader_translator_destroy,