7252eaa50542bafdd627ce44c56eda881a8c6351
[pspp-builds.git] / src / data / casewriter-translator.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <data/casewriter.h>
22 #include <data/casewriter-provider.h>
23
24 #include <stdlib.h>
25
26 #include <libpspp/taint.h>
27
28 #include "xalloc.h"
29
30 struct casewriter_translator
31   {
32     struct casewriter *subwriter;
33
34     void (*translate) (const struct ccase *input, struct ccase *output,
35                        void *aux);
36     bool (*destroy) (void *aux);
37     void *aux;
38   };
39
40 static struct casewriter_class casewriter_translator_class;
41
42 struct casewriter *
43 casewriter_create_translator (struct casewriter *subwriter,
44                               void (*translate) (const struct ccase *input,
45                                                  struct ccase *output,
46                                                  void *aux),
47                               bool (*destroy) (void *aux),
48                               void *aux)
49 {
50   struct casewriter_translator *ct = xmalloc (sizeof *ct);
51   struct casewriter *writer;
52   ct->subwriter = casewriter_rename (subwriter);
53   ct->translate = translate;
54   ct->destroy = destroy;
55   ct->aux = aux;
56   writer = casewriter_create (&casewriter_translator_class, ct);
57   taint_propagate (casewriter_get_taint (ct->subwriter),
58                    casewriter_get_taint (writer));
59   return writer;
60 }
61
62 static void
63 casewriter_translator_write (struct casewriter *writer UNUSED,
64                              void *ct_, struct ccase *c)
65 {
66   struct casewriter_translator *ct = ct_;
67   struct ccase tmp;
68
69   ct->translate (c, &tmp, ct->aux);
70   casewriter_write (ct->subwriter, &tmp);
71 }
72
73 static void
74 casewriter_translator_destroy (struct casewriter *writer UNUSED, void *ct_)
75 {
76   struct casewriter_translator *ct = ct_;
77   casewriter_destroy (ct->subwriter);
78   ct->destroy (ct->aux);
79   free (ct);
80 }
81
82 static struct casereader *
83 casewriter_translator_convert_to_reader (struct casewriter *writer UNUSED,
84                                          void *ct_)
85 {
86   struct casewriter_translator *ct = ct_;
87   struct casereader *reader = casewriter_make_reader (ct->subwriter);
88   free (ct);
89   ct->destroy (ct->aux);
90   return reader;
91 }
92
93 static struct casewriter_class casewriter_translator_class =
94   {
95     casewriter_translator_write,
96     casewriter_translator_destroy,
97     casewriter_translator_convert_to_reader,
98   };