Release PSPP version 1.4.0.
[pspp.git] / src / data / any-reader.c
blob8ece73f62bb85ffc50941f8983e6bc4fd235f062
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2010, 2011, 2012, 2014 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 #include <config.h>
19 #include "data/any-reader.h"
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "data/casereader.h"
28 #include "data/dataset.h"
29 #include "data/dictionary.h"
30 #include "data/file-handle-def.h"
31 #include "data/file-name.h"
32 #include "libpspp/assertion.h"
33 #include "libpspp/cast.h"
34 #include "libpspp/message.h"
35 #include "libpspp/str.h"
37 #include "gl/xalloc.h"
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) (msgid)
43 static const struct any_reader_class dataset_reader_class;
45 static const struct any_reader_class *classes[] =
47 &sys_file_reader_class,
48 &por_file_reader_class,
49 &pcp_file_reader_class,
51 enum { N_CLASSES = sizeof classes / sizeof *classes };
53 int
54 any_reader_detect (const struct file_handle *file_handle,
55 const struct any_reader_class **classp)
57 struct detector
59 enum any_type type;
60 int (*detect) (FILE *);
63 FILE *file;
64 int retval;
66 if (classp)
67 *classp = NULL;
69 file = fn_open (file_handle, "rb");
70 if (file == NULL)
72 msg (ME, _("An error occurred while opening `%s': %s."),
73 fh_get_file_name (file_handle), strerror (errno));
74 return -errno;
77 retval = 0;
78 for (int i = 0; i < N_CLASSES; i++)
80 int rc = classes[i]->detect (file);
81 if (rc == 1)
83 retval = 1;
84 if (classp)
85 *classp = classes[i];
86 break;
88 else if (rc < 0)
89 retval = rc;
92 if (retval < 0)
93 msg (ME, _("Error reading `%s': %s."), fh_get_file_name (file_handle), strerror (-retval));
95 fn_close (file_handle, file);
97 return retval;
100 struct any_reader *
101 any_reader_open (struct file_handle *handle)
103 switch (fh_get_referent (handle))
105 case FH_REF_FILE:
107 const struct any_reader_class *class;
108 int retval;
110 retval = any_reader_detect (handle, &class);
111 if (retval <= 0)
113 if (retval == 0)
114 msg (SE, _("`%s' is not a system or portable file."),
115 fh_get_file_name (handle));
116 return NULL;
119 return class->open (handle);
122 case FH_REF_INLINE:
123 msg (SE, _("The inline file is not allowed here."));
124 return NULL;
126 case FH_REF_DATASET:
127 return dataset_reader_class.open (handle);
129 NOT_REACHED ();
132 /* gnulib on some systems defines "close" as something else,
133 which causes problems for this code. So undefine it here. */
134 #undef close
136 bool
137 any_reader_close (struct any_reader *any_reader)
139 return any_reader ? any_reader->klass->close (any_reader) : true;
142 struct casereader *
143 any_reader_decode (struct any_reader *any_reader,
144 const char *encoding,
145 struct dictionary **dictp,
146 struct any_read_info *info)
148 const struct any_reader_class *class = any_reader->klass;
149 struct casereader *reader;
151 reader = any_reader->klass->decode (any_reader, encoding, dictp, info);
152 if (reader && info)
153 info->klass = class;
154 return reader;
157 size_t
158 any_reader_get_strings (const struct any_reader *any_reader, struct pool *pool,
159 char ***labels, bool **ids, char ***values)
161 return (any_reader->klass->get_strings
162 ? any_reader->klass->get_strings (any_reader, pool, labels, ids,
163 values)
164 : 0);
167 struct casereader *
168 any_reader_open_and_decode (struct file_handle *handle,
169 const char *encoding,
170 struct dictionary **dictp,
171 struct any_read_info *info)
173 struct any_reader *any_reader = any_reader_open (handle);
174 return (any_reader
175 ? any_reader_decode (any_reader, encoding, dictp, info)
176 : NULL);
179 struct dataset_reader
181 struct any_reader any_reader;
182 struct dictionary *dict;
183 struct casereader *reader;
186 /* Opens FH, which must have referent type FH_REF_DATASET, and returns a
187 dataset_reader for it, or a null pointer on failure. Stores a copy of the
188 dictionary for the dataset file into *DICT. The caller takes ownership of
189 the casereader and the dictionary. */
190 static struct any_reader *
191 dataset_reader_open (struct file_handle *fh)
193 struct dataset_reader *reader;
194 struct dataset *ds;
196 /* We don't bother doing fh_lock or fh_ref on the file handle,
197 as there's no advantage in this case, and doing these would
198 require us to keep track of the "struct file_handle" and
199 "struct fh_lock" and undo our work later. */
200 assert (fh_get_referent (fh) == FH_REF_DATASET);
202 ds = fh_get_dataset (fh);
203 if (ds == NULL || !dataset_has_source (ds))
205 msg (SE, _("Cannot read from dataset %s because no dictionary or data "
206 "has been written to it yet."),
207 fh_get_name (fh));
208 return NULL;
211 reader = xmalloc (sizeof *reader);
212 reader->any_reader.klass = &dataset_reader_class;
213 reader->dict = dict_clone (dataset_dict (ds));
214 reader->reader = casereader_clone (dataset_source (ds));
215 return &reader->any_reader;
218 static struct dataset_reader *
219 dataset_reader_cast (const struct any_reader *r_)
221 assert (r_->klass == &dataset_reader_class);
222 return UP_CAST (r_, struct dataset_reader, any_reader);
225 static bool
226 dataset_reader_close (struct any_reader *r_)
228 struct dataset_reader *r = dataset_reader_cast (r_);
229 dict_unref (r->dict);
230 casereader_destroy (r->reader);
231 free (r);
233 return true;
236 static struct casereader *
237 dataset_reader_decode (struct any_reader *r_, const char *encoding UNUSED,
238 struct dictionary **dictp, struct any_read_info *info)
240 struct dataset_reader *r = dataset_reader_cast (r_);
241 struct casereader *reader;
243 *dictp = r->dict;
244 reader = r->reader;
245 if (info)
247 memset (info, 0, sizeof *info);
248 info->integer_format = INTEGER_NATIVE;
249 info->float_format = FLOAT_NATIVE_DOUBLE;
250 info->compression = ANY_COMP_NONE;
251 info->case_cnt = casereader_get_case_cnt (reader);
253 free (r);
255 return reader;
258 static const struct any_reader_class dataset_reader_class =
260 N_("Dataset"),
261 NULL,
262 dataset_reader_open,
263 dataset_reader_close,
264 dataset_reader_decode,
265 NULL,