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"
26 #include "dictionary.h"
27 #include "file-handle-def.h"
28 #include "scratch-handle.h"
31 /* A scratch file writer. */
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. */
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.
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)
50 struct scratch_handle *sh;
51 struct scratch_writer *writer;
52 struct dictionary *scratch_dict;
53 struct dict_compactor *compactor;
55 if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "we"))
58 /* Destroy previous contents of handle. */
59 sh = fh_get_scratch_handle (fh);
61 scratch_handle_destroy (sh);
63 /* Copy the dictionary and compact if needed. */
64 scratch_dict = dict_clone (dictionary);
65 if (dict_compacting_would_shrink (scratch_dict))
67 compactor = dict_make_compactor (scratch_dict);
68 dict_compact_values (scratch_dict);
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));
79 writer = xmalloc (sizeof *writer);
82 writer->compactor = compactor;
84 fh_set_scratch_handle (fh, sh);
88 /* Writes case C to WRITER. */
90 scratch_writer_write_case (struct scratch_writer *writer,
91 const struct ccase *c)
93 struct scratch_handle *handle = writer->handle;
94 if (writer->compactor)
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);
102 return casefile_append (handle->casefile, c);
105 /* Returns true if an I/O error occurred on WRITER, false otherwise. */
107 scratch_writer_error (const struct scratch_writer *writer)
109 return casefile_error (writer->handle->casefile);
113 Returns true if successful, false if an I/O error occurred on WRITER. */
115 scratch_writer_close (struct scratch_writer *writer)
117 struct casefile *cf = writer->handle->casefile;
118 bool ok = casefile_error (cf);
119 fh_close (writer->fh, "scratch file", "we");