13ccee8c6f0aef698c2542ca495dbf4280bf5677
[pspp-builds.git] / src / data / scratch-writer.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include "scratch-writer.h"
22 #include <stdlib.h>
23 #include "case.h"
24 #include "casefile.h"
25 #include "fastfile.h"
26 #include "dictionary.h"
27 #include "file-handle-def.h"
28 #include "scratch-handle.h"
29 #include "xalloc.h"
30
31 /* A scratch file writer. */
32 struct scratch_writer 
33   {
34     struct scratch_handle *handle;      /* Underlying scratch handle. */
35     struct file_handle *fh;             /* Underlying file handle. */
36     struct dict_compactor *compactor;   /* Compacts into handle->dictionary. */
37   };
38
39 /* Opens FH, which must have referent type FH_REF_SCRATCH, and
40    returns a scratch_writer for it, or a null pointer on
41    failure.  Cases stored in the scratch_writer will be expected
42    to be drawn from DICTIONARY.
43
44    If you use an any_writer instead, then your code can be more
45    flexible without being any harder to write. */
46 struct scratch_writer *
47 scratch_writer_open (struct file_handle *fh,
48                      const struct dictionary *dictionary) 
49 {
50   struct scratch_handle *sh;
51   struct scratch_writer *writer;
52   struct dictionary *scratch_dict;
53   struct dict_compactor *compactor;
54
55   if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "we"))
56     return NULL;
57
58   /* Destroy previous contents of handle. */
59   sh = fh_get_scratch_handle (fh);
60   if (sh != NULL) 
61     scratch_handle_destroy (sh);
62
63   /* Copy the dictionary and compact if needed. */
64   scratch_dict = dict_clone (dictionary);
65   if (dict_compacting_would_shrink (scratch_dict)) 
66     {
67       compactor = dict_make_compactor (scratch_dict);
68       dict_compact_values (scratch_dict);
69     }
70   else
71     compactor = NULL;
72
73   /* Create new contents. */
74   sh = xmalloc (sizeof *sh);
75   sh->dictionary = scratch_dict;
76   sh->casefile = fastfile_create (dict_get_next_value_idx (sh->dictionary));
77
78   /* Create writer. */
79   writer = xmalloc (sizeof *writer);
80   writer->handle = sh;
81   writer->fh = fh;
82   writer->compactor = compactor;
83
84   fh_set_scratch_handle (fh, sh);
85   return writer;
86 }
87
88 /* Writes case C to WRITER. */
89 bool
90 scratch_writer_write_case (struct scratch_writer *writer,
91                            const struct ccase *c) 
92 {
93   struct scratch_handle *handle = writer->handle;
94   if (writer->compactor) 
95     {
96       struct ccase tmp_case;
97       case_create (&tmp_case, dict_get_next_value_idx (handle->dictionary));
98       dict_compactor_compact (writer->compactor, &tmp_case, c);
99       return casefile_append_xfer (handle->casefile, &tmp_case);
100     }
101   else 
102     return casefile_append (handle->casefile, c);
103 }
104
105 /* Returns true if an I/O error occurred on WRITER, false otherwise. */
106 bool
107 scratch_writer_error (const struct scratch_writer *writer) 
108 {
109   return casefile_error (writer->handle->casefile);
110 }
111
112 /* Closes WRITER.
113    Returns true if successful, false if an I/O error occurred on WRITER. */
114 bool
115 scratch_writer_close (struct scratch_writer *writer) 
116 {
117   struct casefile *cf = writer->handle->casefile;
118   bool ok = casefile_error (cf);
119   fh_close (writer->fh, "scratch file", "we");
120   free (writer);
121   return ok;
122 }