Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[pspp-builds.git] / src / data / scratch-reader.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include "scratch-reader.h"
22
23 #include <stdlib.h>
24
25 #include "casefile.h"
26 #include "dictionary.h"
27 #include "file-handle-def.h"
28 #include "scratch-handle.h"
29 #include <data/case.h>
30 #include <libpspp/message.h>
31
32 #include "xalloc.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 /* A reader for a scratch file. */
38 struct scratch_reader 
39   {
40     struct file_handle *fh;             /* Underlying file handle. */
41     struct casereader *casereader;      /* Case reader. */
42   };
43
44 /* Opens FH, which must have referent type FH_REF_SCRATCH, and
45    returns a scratch_reader for it, or a null pointer on
46    failure.  Stores the dictionary for the scratch file into
47    *DICT.
48
49    If you use an any_reader instead, then your code can be more
50    flexible without being any harder to write. */
51 struct scratch_reader *
52 scratch_reader_open (struct file_handle *fh, struct dictionary **dict)
53 {
54   struct scratch_handle *sh;
55   struct scratch_reader *reader;
56   
57   if (!fh_open (fh, FH_REF_SCRATCH, "scratch file", "rs"))
58     return NULL;
59   
60   sh = fh_get_scratch_handle (fh);
61   if (sh == NULL) 
62     {
63       msg (SE, _("Scratch file handle %s has not yet been written, "
64                  "using SAVE or another procedure, so it cannot yet "
65                  "be used for reading."),
66            fh_get_name (fh));
67       return NULL;
68     }
69
70   *dict = dict_clone (sh->dictionary);
71   reader = xmalloc (sizeof *reader);
72   reader->fh = fh;
73   reader->casereader = casefile_get_reader (sh->casefile, NULL);
74   return reader;
75 }
76
77 /* Reads a case from READER and copies it into C.
78    Returns true if successful, false on error or at end of file. */
79 bool
80 scratch_reader_read_case (struct scratch_reader *reader, struct ccase *c)
81 {
82   struct ccase tmp;
83   if (casereader_read (reader->casereader, &tmp)) 
84     {
85       case_copy (c, 0, &tmp, 0,
86                  casefile_get_value_cnt (
87                    casereader_get_casefile (reader->casereader)));
88       case_destroy (&tmp);
89       return true;
90     }
91   else
92     return false;
93 }
94
95 /* Returns true if an I/O error occurred on READER, false otherwise. */
96 bool
97 scratch_reader_error (const struct scratch_reader *reader) 
98 {
99   return casefile_error (casereader_get_casefile (reader->casereader));
100 }
101
102 /* Closes READER. */
103 void
104 scratch_reader_close (struct scratch_reader *reader) 
105 {
106   fh_close (reader->fh, "scratch file", "rs");
107   casereader_destroy (reader->casereader);
108   free (reader);
109 }