Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / data / scratch-writer.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006 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 "scratch-writer.h"
20
21 #include <stdlib.h>
22
23 #include <data/case.h>
24 #include <data/casereader.h>
25 #include <data/casewriter-provider.h>
26 #include <data/casewriter.h>
27 #include <data/dictionary.h>
28 #include <data/file-handle-def.h>
29 #include <data/scratch-handle.h>
30 #include <libpspp/compiler.h>
31 #include <libpspp/taint.h>
32
33 #include "xalloc.h"
34
35 /* A scratch file writer. */
36 struct scratch_writer
37   {
38     struct scratch_handle *handle;      /* Underlying scratch handle. */
39     struct file_handle *fh;             /* Underlying file handle. */
40     struct dict_compactor *compactor;   /* Compacts into handle->dictionary. */
41     struct casewriter *subwriter;       /* Data output. */
42   };
43
44 static struct casewriter_class scratch_writer_casewriter_class;
45
46 /* Opens FH, which must have referent type FH_REF_SCRATCH, and
47    returns a scratch_writer for it, or a null pointer on
48    failure.  Cases stored in the scratch_writer will be expected
49    to be drawn from DICTIONARY. */
50 struct casewriter *
51 scratch_writer_open (struct file_handle *fh,
52                      const struct dictionary *dictionary)
53 {
54   struct scratch_handle *sh;
55   struct scratch_writer *writer;
56   struct dictionary *scratch_dict;
57   struct dict_compactor *compactor;
58   struct casewriter *casewriter;
59
60   if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "we"))
61     return NULL;
62
63   /* Destroy previous contents of handle. */
64   sh = fh_get_scratch_handle (fh);
65   if (sh != NULL)
66     scratch_handle_destroy (sh);
67
68   /* Copy the dictionary and compact if needed. */
69   scratch_dict = dict_clone (dictionary);
70   if (dict_compacting_would_shrink (scratch_dict))
71     {
72       compactor = dict_make_compactor (scratch_dict);
73       dict_compact_values (scratch_dict);
74     }
75   else
76     compactor = NULL;
77
78   /* Create new contents. */
79   sh = xmalloc (sizeof *sh);
80   sh->dictionary = scratch_dict;
81   sh->casereader = NULL;
82
83   /* Create writer. */
84   writer = xmalloc (sizeof *writer);
85   writer->handle = sh;
86   writer->fh = fh;
87   writer->compactor = compactor;
88   writer->subwriter = autopaging_writer_create (dict_get_next_value_idx (
89                                                scratch_dict));
90
91   fh_set_scratch_handle (fh, sh);
92   casewriter = casewriter_create (&scratch_writer_casewriter_class, writer);
93   taint_propagate (casewriter_get_taint (writer->subwriter),
94                    casewriter_get_taint (casewriter));
95   return casewriter;
96 }
97
98 /* Writes case C to WRITER. */
99 static void
100 scratch_writer_casewriter_write (struct casewriter *w UNUSED, void *writer_,
101                                  struct ccase *c)
102 {
103   struct scratch_writer *writer = writer_;
104   struct scratch_handle *handle = writer->handle;
105   struct ccase tmp;
106   if (writer->compactor)
107     {
108       case_create (&tmp, dict_get_next_value_idx (handle->dictionary));
109       dict_compactor_compact (writer->compactor, &tmp, c);
110       case_destroy (c);
111     }
112   else
113     case_move (&tmp, c);
114   casewriter_write (writer->subwriter, &tmp);
115 }
116
117 /* Closes WRITER. */
118 static void
119 scratch_writer_casewriter_destroy (struct casewriter *w UNUSED, void *writer_)
120 {
121   struct scratch_writer *writer = writer_;
122   struct casereader *reader = casewriter_make_reader (writer->subwriter);
123   if (!casereader_error (reader))
124     writer->handle->casereader = reader;
125   fh_close (writer->fh, "scratch file", "we");
126   free (writer);
127 }
128
129 static struct casewriter_class scratch_writer_casewriter_class =
130   {
131     scratch_writer_casewriter_write,
132     scratch_writer_casewriter_destroy,
133     NULL,
134   };