a19533d65d330b6f443b308522e88d61b25687f6
[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, which must return a case with
41    OUTPUT_VALUE_CNT values, based on INPUT and auxiliary data
42    AUX.  (TRANSLATE may also return a null pointer, in which case
43    no case is written to the output.)  The translated cases are
44    then written to SUBWRITER.
45
46    TRANSLATE takes ownership of each case passed to it.  Thus, it
47    should either unref each case and return a new case, or
48    (unshare and then) modify and return the same case.
49
50    When the translating casewriter is destroyed, DESTROY will be
51    called to allow any state maintained by TRANSLATE to be freed.
52
53    After this function is called, SUBWRITER must not ever again
54    be referenced directly.  It will be destroyed automatically
55    when the translating casewriter is destroyed. */
56 struct casewriter *
57 casewriter_create_translator (struct casewriter *subwriter,
58                               size_t translated_value_cnt,
59                               struct ccase *(*translate) (struct ccase *,
60                                                           void *aux),
61                               bool (*destroy) (void *aux),
62                               void *aux)
63 {
64   struct casewriter_translator *ct = xmalloc (sizeof *ct);
65   struct casewriter *writer;
66   ct->subwriter = casewriter_rename (subwriter);
67   ct->translate = translate;
68   ct->destroy = destroy;
69   ct->aux = aux;
70   writer = casewriter_create (translated_value_cnt,
71                               &casewriter_translator_class, ct);
72   taint_propagate (casewriter_get_taint (ct->subwriter),
73                    casewriter_get_taint (writer));
74   return writer;
75 }
76
77 static void
78 casewriter_translator_write (struct casewriter *writer UNUSED,
79                              void *ct_, struct ccase *c)
80 {
81   struct casewriter_translator *ct = ct_;
82   c = ct->translate (c, ct->aux);
83   if (c != NULL)
84     casewriter_write (ct->subwriter, c);
85 }
86
87 static void
88 casewriter_translator_destroy (struct casewriter *writer UNUSED, void *ct_)
89 {
90   struct casewriter_translator *ct = ct_;
91   casewriter_destroy (ct->subwriter);
92   ct->destroy (ct->aux);
93   free (ct);
94 }
95
96 static struct casereader *
97 casewriter_translator_convert_to_reader (struct casewriter *writer UNUSED,
98                                          void *ct_)
99 {
100   struct casewriter_translator *ct = ct_;
101   struct casereader *reader = casewriter_make_reader (ct->subwriter);
102   free (ct);
103   ct->destroy (ct->aux);
104   return reader;
105 }
106
107 static const struct casewriter_class casewriter_translator_class =
108   {
109     casewriter_translator_write,
110     casewriter_translator_destroy,
111     casewriter_translator_convert_to_reader,
112   };