Add scratch file handles.
[pspp-builds.git] / src / 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 "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_needs_compaction (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 = casefile_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 void
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       casefile_append_xfer (handle->casefile, &tmp_case);
99     }
100   else 
101     casefile_append (handle->casefile, c);
102 }
103
104 /* Closes WRITER. */
105 void
106 scratch_writer_close (struct scratch_writer *writer) 
107 {
108   struct scratch_handle *handle = writer->handle;
109   casefile_mode_reader (handle->casefile);
110   fh_close (writer->fh, "scratch file", "we");
111   free (writer);
112 }