Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[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 #include "scratch-writer.h"
21 #include <stdlib.h>
22 #include "case.h"
23 #include "casefile.h"
24 #include "fastfile.h"
25 #include "dictionary.h"
26 #include "file-handle-def.h"
27 #include "scratch-handle.h"
28 #include "xalloc.h"
29
30 /* A scratch file writer. */
31 struct scratch_writer 
32   {
33     struct scratch_handle *handle;      /* Underlying scratch handle. */
34     struct file_handle *fh;             /* Underlying file handle. */
35     struct dict_compactor *compactor;   /* Compacts into handle->dictionary. */
36   };
37
38 /* Opens FH, which must have referent type FH_REF_SCRATCH, and
39    returns a scratch_writer for it, or a null pointer on
40    failure.  Cases stored in the scratch_writer will be expected
41    to be drawn from DICTIONARY.
42
43    If you use an any_writer instead, then your code can be more
44    flexible without being any harder to write. */
45 struct scratch_writer *
46 scratch_writer_open (struct file_handle *fh,
47                      const struct dictionary *dictionary) 
48 {
49   struct scratch_handle *sh;
50   struct scratch_writer *writer;
51   struct dictionary *scratch_dict;
52   struct dict_compactor *compactor;
53
54   if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "we"))
55     return NULL;
56
57   /* Destroy previous contents of handle. */
58   sh = fh_get_scratch_handle (fh);
59   if (sh != NULL) 
60     scratch_handle_destroy (sh);
61
62   /* Copy the dictionary and compact if needed. */
63   scratch_dict = dict_clone (dictionary);
64   if (dict_compacting_would_shrink (scratch_dict)) 
65     {
66       compactor = dict_make_compactor (scratch_dict);
67       dict_compact_values (scratch_dict);
68     }
69   else
70     compactor = NULL;
71
72   /* Create new contents. */
73   sh = xmalloc (sizeof *sh);
74   sh->dictionary = scratch_dict;
75   sh->casefile = fastfile_create (dict_get_next_value_idx (sh->dictionary));
76
77   /* Create writer. */
78   writer = xmalloc (sizeof *writer);
79   writer->handle = sh;
80   writer->fh = fh;
81   writer->compactor = compactor;
82
83   fh_set_scratch_handle (fh, sh);
84   return writer;
85 }
86
87 /* Writes case C to WRITER. */
88 bool
89 scratch_writer_write_case (struct scratch_writer *writer,
90                            const struct ccase *c) 
91 {
92   struct scratch_handle *handle = writer->handle;
93   if (writer->compactor) 
94     {
95       struct ccase tmp_case;
96       case_create (&tmp_case, dict_get_next_value_idx (handle->dictionary));
97       dict_compactor_compact (writer->compactor, &tmp_case, c);
98       return casefile_append_xfer (handle->casefile, &tmp_case);
99     }
100   else 
101     return casefile_append (handle->casefile, c);
102 }
103
104 /* Returns true if an I/O error occurred on WRITER, false otherwise. */
105 bool
106 scratch_writer_error (const struct scratch_writer *writer) 
107 {
108   return casefile_error (writer->handle->casefile);
109 }
110
111 /* Closes WRITER.
112    Returns true if successful, false if an I/O error occurred on WRITER. */
113 bool
114 scratch_writer_close (struct scratch_writer *writer) 
115 {
116   struct casefile *cf = writer->handle->casefile;
117   bool ok = casefile_error (cf);
118   fh_close (writer->fh, "scratch file", "we");
119   free (writer);
120   return ok;
121 }