1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-2004, 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 <language/data-io/data-reader.h>
29 #include <data/file-handle-def.h>
30 #include <data/file-name.h>
31 #include <data/procedure.h>
32 #include <language/command.h>
33 #include <language/data-io/file-handle.h>
34 #include <language/lexer/lexer.h>
35 #include <language/line-buffer.h>
36 #include <libpspp/alloc.h>
37 #include <libpspp/message.h>
38 #include <libpspp/str.h>
41 #define _(msgid) gettext (msgid)
43 /* Flags for DFM readers. */
46 DFM_ADVANCE = 002, /* Read next line on dfm_get_record() call? */
47 DFM_SAW_BEGIN_DATA = 004, /* For inline_file only, whether we've
48 already read a BEGIN DATA line. */
49 DFM_TABS_EXPANDED = 010, /* Tabs have been expanded. */
52 /* Data file reader. */
55 struct file_handle *fh; /* File handle. */
56 struct msg_locator where; /* Current location in data file. */
57 struct string line; /* Current line. */
58 struct string scratch; /* Extra line buffer. */
59 enum dfm_reader_flags flags; /* Zero or more of DFM_*. */
60 FILE *file; /* Associated file. */
61 size_t pos; /* Offset in line of current character. */
62 unsigned eof_cnt; /* # of attempts to advance past EOF. */
65 /* Closes reader R opened by dfm_open_reader(). */
67 dfm_close_reader (struct dfm_reader *r)
76 is_inline = r->fh == fh_inline_file ();
77 file_name = is_inline ? NULL : xstrdup (fh_get_file_name (r->fh));
78 still_open = fh_close (r->fh, "data file", "rs");
86 fn_close (file_name, r->file);
89 /* Skip any remaining data on the inline file. */
90 if (r->flags & DFM_SAW_BEGIN_DATA)
92 dfm_reread_record (r, 0);
94 dfm_forward_record (r);
98 ds_destroy (&r->line);
99 ds_destroy (&r->scratch);
104 /* Opens the file designated by file handle FH for reading as a
105 data file. Providing fh_inline_file() for FH designates the
106 "inline file", that is, data included inline in the command
107 file between BEGIN FILE and END FILE. Returns a reader if
108 successful, or a null pointer otherwise. */
110 dfm_open_reader (struct file_handle *fh)
112 struct dfm_reader *r;
115 rp = fh_open (fh, FH_REF_FILE | FH_REF_INLINE, "data file", "rs");
121 r = xmalloc (sizeof *r);
123 ds_init (&r->line, 64);
124 ds_init (&r->scratch, 0);
125 r->flags = DFM_ADVANCE;
127 if (fh != fh_inline_file ())
129 r->where.file_name = fh_get_file_name (fh);
130 r->where.line_number = 0;
131 r->file = fn_open (fh_get_file_name (fh), "rb");
134 msg (ME, _("Could not open \"%s\" for reading as a data file: %s."),
135 fh_get_file_name (r->fh), strerror (errno));
136 fh_close (fh,"data file", "rs");
146 /* Returns true if an I/O error occurred on READER, false otherwise. */
148 dfm_reader_error (const struct dfm_reader *r)
150 return fh_get_referent (r->fh) == FH_REF_FILE && ferror (r->file);
153 /* Reads a record from the inline file into R.
154 Returns true if successful, false on failure. */
156 read_inline_record (struct dfm_reader *r)
158 if ((r->flags & DFM_SAW_BEGIN_DATA) == 0)
160 r->flags |= DFM_SAW_BEGIN_DATA;
164 if (!lex_force_match_id ("BEGIN") || !lex_force_match_id ("DATA"))
166 getl_set_prompt_style (GETL_PROMPT_DATA);
169 if (!getl_read_line (NULL))
171 msg (SE, _("Unexpected end-of-file while reading data in BEGIN "
172 "DATA. This probably indicates "
173 "a missing or misformatted END DATA command. "
174 "END DATA must appear by itself on a single line "
175 "with exactly one space between words."));
179 if (ds_length (&getl_buf) >= 8
180 && !strncasecmp (ds_c_str (&getl_buf), "end data", 8))
182 lex_set_prog (ds_c_str (&getl_buf) + ds_length (&getl_buf));
186 ds_assign_string (&r->line, &getl_buf);
190 /* Reads a record from a disk file into R.
191 Returns true if successful, false on failure. */
193 read_file_record (struct dfm_reader *r)
195 assert (r->fh != fh_inline_file ());
196 if (fh_get_mode (r->fh) == FH_MODE_TEXT)
199 if (!ds_gets (&r->line, r->file))
201 if (ferror (r->file))
202 msg (ME, _("Error reading file %s: %s."),
203 fh_get_name (r->fh), strerror (errno));
207 else if (fh_get_mode (r->fh) == FH_MODE_BINARY)
209 size_t record_width = fh_get_record_width (r->fh);
212 if (ds_length (&r->line) < record_width)
213 ds_rpad (&r->line, record_width, 0);
215 amt = fread (ds_c_str (&r->line), 1, record_width, r->file);
216 if (record_width != amt)
218 if (ferror (r->file))
219 msg (ME, _("Error reading file %s: %s."),
220 fh_get_name (r->fh), strerror (errno));
222 msg (ME, _("%s: Partial record at end of file."),
223 fh_get_name (r->fh));
231 r->where.line_number++;
236 /* Reads a record from R, setting the current position to the
237 start of the line. If an error occurs or end-of-file is
238 encountered, the current line is set to null. */
240 read_record (struct dfm_reader *r)
242 return (fh_get_referent (r->fh) == FH_REF_FILE
243 ? read_file_record (r)
244 : read_inline_record (r));
247 /* Returns the number of attempts, thus far, to advance past
248 end-of-file in reader R. Reads forward in HANDLE's file, if
249 necessary, to find out.
251 Normally, the user stops attempting to read from the file the
252 first time EOF is reached (a return value of 1). If the user
253 tries to read past EOF again (a return value of 2 or more),
254 an error message is issued, and the caller should more
255 forcibly abort to avoid an infinite loop. */
257 dfm_eof (struct dfm_reader *r)
259 if (r->flags & DFM_ADVANCE)
261 r->flags &= ~DFM_ADVANCE;
263 if (r->eof_cnt == 0 && read_record (r))
272 if (r->fh != fh_inline_file ())
273 msg (ME, _("Attempt to read beyond end-of-file on file %s."),
274 fh_get_name (r->fh));
276 msg (ME, _("Attempt to read beyond END DATA."));
283 /* Returns the current record in the file corresponding to
284 HANDLE. Aborts if reading from the file is necessary or at
285 end of file, so call dfm_eof() first. Sets *LINE to the line,
286 which is not null-terminated. The caller must not free or
287 modify the returned string. */
289 dfm_get_record (struct dfm_reader *r, struct fixed_string *line)
291 assert ((r->flags & DFM_ADVANCE) == 0);
292 assert (r->eof_cnt == 0);
293 assert (r->pos <= ds_length (&r->line));
295 line->string = ds_data (&r->line) + r->pos;
296 line->length = ds_length (&r->line) - r->pos;
299 /* Expands tabs in the current line into the equivalent number of
300 spaces, if appropriate for this kind of file. Aborts if
301 reading from the file is necessary or at end of file, so call
304 dfm_expand_tabs (struct dfm_reader *r)
307 size_t ofs, new_pos, tab_width;
309 assert ((r->flags & DFM_ADVANCE) == 0);
310 assert (r->eof_cnt == 0);
311 assert (r->pos <= ds_length (&r->line));
313 if (r->flags & DFM_TABS_EXPANDED)
315 r->flags |= DFM_TABS_EXPANDED;
317 if (r->fh != fh_inline_file ()
318 && (fh_get_mode (r->fh) == FH_MODE_BINARY
319 || fh_get_tab_width (r->fh) == 0
320 || memchr (ds_c_str (&r->line), '\t', ds_length (&r->line)) == NULL))
323 /* Expand tabs from r->line into r->scratch, and figure out
324 new value for r->pos. */
325 tab_width = fh_get_tab_width (r->fh);
326 ds_clear (&r->scratch);
328 for (ofs = 0; ofs < ds_length (&r->line); ofs++)
333 new_pos = ds_length (&r->scratch);
335 c = ds_c_str (&r->line)[ofs];
337 ds_putc (&r->scratch, c);
341 ds_putc (&r->scratch, ' ');
342 while (ds_length (&r->scratch) % tab_width != 0);
346 /* Swap r->line and r->scratch and set new r->pos. */
348 r->line = r->scratch;
353 /* Causes dfm_get_record() to read in the next record the next time it
354 is executed on file HANDLE. */
356 dfm_forward_record (struct dfm_reader *r)
358 r->flags |= DFM_ADVANCE;
361 /* Cancels the effect of any previous dfm_fwd_record() executed
362 on file HANDLE. Sets the current line to begin in the 1-based
365 dfm_reread_record (struct dfm_reader *r, size_t column)
367 r->flags &= ~DFM_ADVANCE;
370 else if (column > ds_length (&r->line))
371 r->pos = ds_length (&r->line);
376 /* Sets the current line to begin COLUMNS characters following
377 the current start. */
379 dfm_forward_columns (struct dfm_reader *r, size_t columns)
381 dfm_reread_record (r, (r->pos + 1) + columns);
384 /* Returns the 1-based column to which the line pointer in HANDLE
385 is set. Unless dfm_reread_record() or dfm_forward_columns()
386 have been called, this is 1. */
388 dfm_column_start (struct dfm_reader *r)
393 /* Pushes the file name and line number on the fn/ln stack. */
395 dfm_push (struct dfm_reader *r)
397 if (r->fh != fh_inline_file ())
398 msg_push_msg_locator (&r->where);
401 /* Pops the file name and line number from the fn/ln stack. */
403 dfm_pop (struct dfm_reader *r)
405 if (r->fh != fh_inline_file ())
406 msg_pop_msg_locator (&r->where);
409 /* BEGIN DATA...END DATA procedure. */
411 /* Perform BEGIN DATA...END DATA as a procedure in itself. */
413 cmd_begin_data (void)
415 struct dfm_reader *r;
418 if (!fh_is_open (fh_inline_file ()))
420 msg (SE, _("This command is not valid here since the current "
421 "input program does not access the inline file."));
422 return CMD_CASCADING_FAILURE;
425 /* Open inline file. */
426 r = dfm_open_reader (fh_inline_file ());
427 r->flags |= DFM_SAW_BEGIN_DATA;
429 /* Input procedure reads from inline file. */
430 getl_set_prompt_style (GETL_PROMPT_DATA);
431 ok = procedure (NULL, NULL);
433 dfm_close_reader (r);
435 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;