8cce42f2081758075221fa9f4196a8b0c98ff2b4
[pspp-builds.git] / src / data / 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
22 #include "scratch-reader.h"
23
24 #include <stdlib.h>
25
26 #include "casefile.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>
32
33 #include "xalloc.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 /* A reader for a scratch file. */
39 struct scratch_reader 
40   {
41     struct file_handle *fh;             /* Underlying file handle. */
42     struct casereader *casereader;      /* Case reader. */
43   };
44
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
48    *DICT.
49
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)
54 {
55   struct scratch_handle *sh;
56   struct scratch_reader *reader;
57   
58   if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "rs"))
59     return NULL;
60   
61   sh = fh_get_scratch_handle (fh);
62   if (sh == NULL) 
63     {
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."),
67            fh_get_name (fh));
68       return NULL;
69     }
70
71   *dict = dict_clone (sh->dictionary);
72   reader = xmalloc (sizeof *reader);
73   reader->fh = fh;
74   reader->casereader = casefile_get_reader (sh->casefile, NULL);
75   return reader;
76 }
77
78 /* Reads a case from READER and copies it into C.
79    Returns true if successful, false on error or at end of file. */
80 bool
81 scratch_reader_read_case (struct scratch_reader *reader, struct ccase *c)
82 {
83   struct ccase tmp;
84   if (casereader_read (reader->casereader, &tmp)) 
85     {
86       case_copy (c, 0, &tmp, 0,
87                  casefile_get_value_cnt (
88                    casereader_get_casefile (reader->casereader)));
89       case_destroy (&tmp);
90       return true;
91     }
92   else
93     return false;
94 }
95
96 /* Returns true if an I/O error occurred on READER, false otherwise. */
97 bool
98 scratch_reader_error (const struct scratch_reader *reader) 
99 {
100   return casefile_error (casereader_get_casefile (reader->casereader));
101 }
102
103 /* Closes READER. */
104 void
105 scratch_reader_close (struct scratch_reader *reader) 
106 {
107   fh_close (reader->fh, "scratch file", "rs");
108   casereader_destroy (reader->casereader);
109   free (reader);
110 }