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
22 #include "scratch-reader.h"
27 #include "dictionary.h"
28 #include "file-handle-def.h"
29 #include "scratch-handle.h"
30 #include <data/case.h>
31 #include <libpspp/message.h>
36 #define _(msgid) gettext (msgid)
38 /* A reader for a scratch file. */
41 struct file_handle *fh; /* Underlying file handle. */
42 struct casereader *casereader; /* Case reader. */
45 /* Opens FH, which must have referent type FH_REF_SCRATCH, and
46 returns a scratch_reader for it, or a null pointer on
47 failure. Stores the dictionary for the scratch file into
50 If you use an any_reader instead, then your code can be more
51 flexible without being any harder to write. */
52 struct scratch_reader *
53 scratch_reader_open (struct file_handle *fh, struct dictionary **dict)
55 struct scratch_handle *sh;
56 struct scratch_reader *reader;
58 if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "rs"))
61 sh = fh_get_scratch_handle (fh);
64 msg (SE, _("Scratch file handle %s has not yet been written, "
65 "using SAVE or another procedure, so it cannot yet "
66 "be used for reading."),
71 *dict = dict_clone (sh->dictionary);
72 reader = xmalloc (sizeof *reader);
74 reader->casereader = casefile_get_reader (sh->casefile);
78 /* Reads a case from READER and copies it into C.
79 Returns true if successful, false on error or at end of file. */
81 scratch_reader_read_case (struct scratch_reader *reader, struct ccase *c)
84 if (casereader_read (reader->casereader, &tmp))
86 case_copy (c, 0, &tmp, 0,
87 casefile_get_value_cnt (
88 casereader_get_casefile (reader->casereader)));
96 /* Returns true if an I/O error occurred on READER, false otherwise. */
98 scratch_reader_error (const struct scratch_reader *reader)
100 return casefile_error (casereader_get_casefile (reader->casereader));
105 scratch_reader_close (struct scratch_reader *reader)
107 fh_close (reader->fh, "scratch file", "rs");
108 casereader_destroy (reader->casereader);