* psppire-dict.c (psppire_dict_dump): Don't use
[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 <data/variable.h>
31 #include <libpspp/compiler.h>
32 #include <libpspp/taint.h>
33
34 #include "xalloc.h"
35
36 /* A scratch file writer. */
37 struct scratch_writer
38   {
39     struct scratch_handle *handle;      /* Underlying scratch handle. */
40     struct file_handle *fh;             /* Underlying file handle. */
41     struct dict_compactor *compactor;   /* Compacts into handle->dictionary. */
42     struct casewriter *subwriter;       /* Data output. */
43   };
44
45 static struct casewriter_class scratch_writer_casewriter_class;
46
47 /* Opens FH, which must have referent type FH_REF_SCRATCH, and
48    returns a scratch_writer for it, or a null pointer on
49    failure.  Cases stored in the scratch_writer will be expected
50    to be drawn from DICTIONARY. */
51 struct casewriter *
52 scratch_writer_open (struct file_handle *fh,
53                      const struct dictionary *dictionary)
54 {
55   struct scratch_handle *sh;
56   struct scratch_writer *writer;
57   struct dictionary *scratch_dict;
58   struct dict_compactor *compactor;
59   struct casewriter *casewriter;
60   size_t dict_value_cnt;
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   dict_delete_scratch_vars (scratch_dict);
73   if (dict_count_values (scratch_dict, 0)
74       < dict_get_next_value_idx (scratch_dict))
75     {
76       compactor = dict_make_compactor (scratch_dict, 0);
77       dict_compact_values (scratch_dict);
78     }
79   else
80     compactor = NULL;
81   dict_value_cnt = dict_get_next_value_idx (scratch_dict);
82
83   /* Create new contents. */
84   sh = xmalloc (sizeof *sh);
85   sh->dictionary = scratch_dict;
86   sh->casereader = NULL;
87
88   /* Create writer. */
89   writer = xmalloc (sizeof *writer);
90   writer->handle = sh;
91   writer->fh = fh;
92   writer->compactor = compactor;
93   writer->subwriter = autopaging_writer_create (dict_value_cnt);
94
95   fh_set_scratch_handle (fh, sh);
96   casewriter = casewriter_create (dict_value_cnt,
97                                   &scratch_writer_casewriter_class, writer);
98   taint_propagate (casewriter_get_taint (writer->subwriter),
99                    casewriter_get_taint (casewriter));
100   return casewriter;
101 }
102
103 /* Writes case C to WRITER. */
104 static void
105 scratch_writer_casewriter_write (struct casewriter *w UNUSED, void *writer_,
106                                  struct ccase *c)
107 {
108   struct scratch_writer *writer = writer_;
109   struct scratch_handle *handle = writer->handle;
110   struct ccase tmp;
111   if (writer->compactor)
112     {
113       case_create (&tmp, dict_get_next_value_idx (handle->dictionary));
114       dict_compactor_compact (writer->compactor, &tmp, c);
115       case_destroy (c);
116     }
117   else
118     case_move (&tmp, c);
119   casewriter_write (writer->subwriter, &tmp);
120 }
121
122 /* Closes WRITER. */
123 static void
124 scratch_writer_casewriter_destroy (struct casewriter *w UNUSED, void *writer_)
125 {
126   struct scratch_writer *writer = writer_;
127   struct casereader *reader = casewriter_make_reader (writer->subwriter);
128   if (!casereader_error (reader))
129     writer->handle->casereader = reader;
130   fh_close (writer->fh, "scratch file", "we");
131   free (writer);
132 }
133
134 static struct casewriter_class scratch_writer_casewriter_class =
135   {
136     scratch_writer_casewriter_write,
137     scratch_writer_casewriter_destroy,
138     NULL,
139   };