1 /* PSPP - computes sample statistics.
2 Copyright (C) 2006 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
21 #include "scratch-writer.h"
25 #include "dictionary.h"
26 #include "file-handle-def.h"
27 #include "scratch-handle.h"
30 /* A scratch file writer. */
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. */
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.
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)
49 struct scratch_handle *sh;
50 struct scratch_writer *writer;
51 struct dictionary *scratch_dict;
52 struct dict_compactor *compactor;
54 if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "we"))
57 /* Destroy previous contents of handle. */
58 sh = fh_get_scratch_handle (fh);
60 scratch_handle_destroy (sh);
62 /* Copy the dictionary and compact if needed. */
63 scratch_dict = dict_clone (dictionary);
64 if (dict_needs_compaction (scratch_dict))
66 compactor = dict_make_compactor (scratch_dict);
67 dict_compact_values (scratch_dict);
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));
78 writer = xmalloc (sizeof *writer);
81 writer->compactor = compactor;
83 fh_set_scratch_handle (fh, sh);
87 /* Writes case C to WRITER. */
89 scratch_writer_write_case (struct scratch_writer *writer,
90 const struct ccase *c)
92 struct scratch_handle *handle = writer->handle;
93 if (writer->compactor)
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);
101 return casefile_append (handle->casefile, c);
104 /* Returns true if an I/O error occurred on WRITER, false otherwise. */
106 scratch_writer_error (const struct scratch_writer *writer)
108 return casefile_error (writer->handle->casefile);
112 Returns true if successful, false if an I/O error occurred on WRITER. */
114 scratch_writer_close (struct scratch_writer *writer)
116 struct casefile *cf = writer->handle->casefile;
117 bool ok = casefile_error (cf);
118 casefile_mode_reader (cf);
119 fh_close (writer->fh, "scratch file", "we");