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