Implemented the find dialog.
[pspp] / 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/case-map.h>
25 #include <data/casereader.h>
26 #include <data/casewriter-provider.h>
27 #include <data/casewriter.h>
28 #include <data/dictionary.h>
29 #include <data/file-handle-def.h>
30 #include <data/scratch-handle.h>
31 #include <data/variable.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 case_map *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 case_map *compactor;
60   struct casewriter *casewriter;
61   size_t dict_value_cnt;
62
63   if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "we"))
64     return NULL;
65
66   /* Destroy previous contents of handle. */
67   sh = fh_get_scratch_handle (fh);
68   if (sh != NULL)
69     scratch_handle_destroy (sh);
70
71   /* Copy the dictionary and compact if needed. */
72   scratch_dict = dict_clone (dictionary);
73   dict_delete_scratch_vars (scratch_dict);
74   if (dict_count_values (scratch_dict, 0)
75       < dict_get_next_value_idx (scratch_dict))
76     {
77       compactor = case_map_to_compact_dict (scratch_dict, 0);
78       dict_compact_values (scratch_dict);
79     }
80   else
81     compactor = NULL;
82   dict_value_cnt = dict_get_next_value_idx (scratch_dict);
83
84   /* Create new contents. */
85   sh = xmalloc (sizeof *sh);
86   sh->dictionary = scratch_dict;
87   sh->casereader = NULL;
88
89   /* Create writer. */
90   writer = xmalloc (sizeof *writer);
91   writer->handle = sh;
92   writer->fh = fh;
93   writer->compactor = compactor;
94   writer->subwriter = autopaging_writer_create (dict_value_cnt);
95
96   fh_set_scratch_handle (fh, sh);
97   casewriter = casewriter_create (dict_value_cnt,
98                                   &scratch_writer_casewriter_class, writer);
99   taint_propagate (casewriter_get_taint (writer->subwriter),
100                    casewriter_get_taint (casewriter));
101   return casewriter;
102 }
103
104 /* Writes case C to WRITER. */
105 static void
106 scratch_writer_casewriter_write (struct casewriter *w UNUSED, void *writer_,
107                                  struct ccase *c)
108 {
109   struct scratch_writer *writer = writer_;
110   struct ccase tmp;
111   if (writer->compactor)
112     {
113       case_map_execute (writer->compactor, c, &tmp);
114       case_destroy (c);
115     }
116   else
117     case_move (&tmp, c);
118   casewriter_write (writer->subwriter, &tmp);
119 }
120
121 /* Closes WRITER. */
122 static void
123 scratch_writer_casewriter_destroy (struct casewriter *w UNUSED, void *writer_)
124 {
125   struct scratch_writer *writer = writer_;
126   struct casereader *reader = casewriter_make_reader (writer->subwriter);
127   if (!casereader_error (reader))
128     writer->handle->casereader = reader;
129   fh_close (writer->fh, "scratch file", "we");
130   free (writer);
131 }
132
133 static struct casewriter_class scratch_writer_casewriter_class =
134   {
135     scratch_writer_casewriter_write,
136     scratch_writer_casewriter_destroy,
137     NULL,
138   };