Add scratch file handles.
[pspp-builds.git] / src / scratch-reader.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-reader.h"
22 #include <stdlib.h>
23 #include "casefile.h"
24 #include "dictionary.h"
25 #include "error.h"
26 #include "file-handle-def.h"
27 #include "scratch-handle.h"
28 #include "xalloc.h"
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32
33 /* A reader for a scratch file. */
34 struct scratch_reader 
35   {
36     struct file_handle *fh;             /* Underlying file handle. */
37     struct casereader *casereader;      /* Case reader. */
38   };
39
40 /* Opens FH, which must have referent type FH_REF_SCRATCH, and
41    returns a scratch_reader for it, or a null pointer on
42    failure.  Stores the dictionary for the scratch file into
43    *DICT.
44
45    If you use an any_reader instead, then your code can be more
46    flexible without being any harder to write. */
47 struct scratch_reader *
48 scratch_reader_open (struct file_handle *fh, struct dictionary **dict)
49 {
50   struct scratch_handle *sh;
51   struct scratch_reader *reader;
52   
53   if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "rs"))
54     return NULL;
55   
56   sh = fh_get_scratch_handle (fh);
57   if (sh == NULL) 
58     {
59       msg (SE, _("Scratch file handle %s has not yet been written, "
60                  "using SAVE or another procedure, so it cannot yet "
61                  "be used for reading."),
62            fh_get_name (fh));
63       return NULL;
64     }
65
66   *dict = dict_clone (sh->dictionary);
67   reader = xmalloc (sizeof *reader);
68   reader->fh = fh;
69   reader->casereader = casefile_get_reader (sh->casefile);
70   return reader;
71 }
72
73 /* Reads a case from READER into C.
74    Returns true if successful, false on error or at end of file. */
75 bool
76 scratch_reader_read_case (struct scratch_reader *reader, struct ccase *c)
77 {
78   return casereader_read (reader->casereader, c);
79 }
80
81 /* Closes READER. */
82 void
83 scratch_reader_close (struct scratch_reader *reader) 
84 {
85   fh_close (reader->fh, "scratch file", "rs");
86   casereader_destroy (reader->casereader);
87   free (reader);
88 }