Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / data / casewriter-translator.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007 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     void (*translate) (const struct ccase *input, struct ccase *output,
33                        void *aux);
34     bool (*destroy) (void *aux);
35     void *aux;
36   };
37
38 static struct casewriter_class casewriter_translator_class;
39
40 struct casewriter *
41 casewriter_create_translator (struct casewriter *subwriter,
42                               void (*translate) (const struct ccase *input,
43                                                  struct ccase *output,
44                                                  void *aux),
45                               bool (*destroy) (void *aux),
46                               void *aux)
47 {
48   struct casewriter_translator *ct = xmalloc (sizeof *ct);
49   struct casewriter *writer;
50   ct->subwriter = casewriter_rename (subwriter);
51   ct->translate = translate;
52   ct->destroy = destroy;
53   ct->aux = aux;
54   writer = casewriter_create (&casewriter_translator_class, ct);
55   taint_propagate (casewriter_get_taint (ct->subwriter),
56                    casewriter_get_taint (writer));
57   return writer;
58 }
59
60 static void
61 casewriter_translator_write (struct casewriter *writer UNUSED,
62                              void *ct_, struct ccase *c)
63 {
64   struct casewriter_translator *ct = ct_;
65   struct ccase tmp;
66
67   ct->translate (c, &tmp, ct->aux);
68   casewriter_write (ct->subwriter, &tmp);
69 }
70
71 static void
72 casewriter_translator_destroy (struct casewriter *writer UNUSED, void *ct_)
73 {
74   struct casewriter_translator *ct = ct_;
75   casewriter_destroy (ct->subwriter);
76   ct->destroy (ct->aux);
77   free (ct);
78 }
79
80 static struct casereader *
81 casewriter_translator_convert_to_reader (struct casewriter *writer UNUSED,
82                                          void *ct_)
83 {
84   struct casewriter_translator *ct = ct_;
85   struct casereader *reader = casewriter_make_reader (ct->subwriter);
86   free (ct);
87   ct->destroy (ct->aux);
88   return reader;
89 }
90
91 static struct casewriter_class casewriter_translator_class =
92   {
93     casewriter_translator_write,
94     casewriter_translator_destroy,
95     casewriter_translator_convert_to_reader,
96   };