1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-2004 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
29 #include "file-handle.h"
37 #define _(msgid) gettext (msgid)
39 #include "debug-print.h"
41 /* Flags for DFM readers. */
44 DFM_EOF = 001, /* At end-of-file? */
45 DFM_ADVANCE = 002, /* Read next line on dfm_get_record() call? */
46 DFM_SAW_BEGIN_DATA = 004, /* For inline_file only, whether we've
47 already read a BEGIN DATA line. */
48 DFM_TABS_EXPANDED = 010, /* Tabs have been expanded. */
51 /* Data file reader. */
54 struct file_handle *fh; /* File handle. */
55 struct file_ext file; /* Associated file. */
56 struct file_locator where; /* Current location in data file. */
57 struct string line; /* Current line. */
58 size_t pos; /* Offset in line of current character. */
59 struct string scratch; /* Extra line buffer. */
60 enum dfm_reader_flags flags; /* Zero or more of DFM_*. */
63 static int inline_open_cnt;
64 static struct dfm_reader *inline_file;
66 static void read_record (struct dfm_reader *r);
68 /* Closes reader R opened by dfm_open_reader(). */
70 dfm_close_reader (struct dfm_reader *r)
78 still_open = fh_close (r->fh, "data file", "rs");
81 assert (inline_open_cnt > 0);
82 still_open = --inline_open_cnt;
86 /* Skip any remaining data on the inline file. */
87 if (r->flags & DFM_SAW_BEGIN_DATA)
88 while ((r->flags & DFM_EOF) == 0)
96 if (r->fh != NULL && r->file.file)
98 fn_close_ext (&r->file);
99 free (r->file.filename);
100 r->file.filename = NULL;
102 ds_destroy (&r->line);
103 ds_destroy (&r->scratch);
107 /* Opens the file designated by file handle FH for reading as a
108 data file. Providing a null pointer for FH designates the
109 "inline file", that is, data included inline in the command
110 file between BEGIN FILE and END FILE. Returns nonzero only if
113 dfm_open_reader (struct file_handle *fh)
115 struct dfm_reader *r;
120 rp = fh_open (fh, "data file", "rs");
128 assert (inline_open_cnt >= 0);
129 if (inline_open_cnt++ > 0)
134 r = xmalloc (sizeof *r);
138 r->where.filename = handle_get_filename (fh);
139 r->where.line_number = 0;
142 ds_init (&r->line, 64);
143 ds_init (&r->scratch, 0);
144 r->flags = DFM_ADVANCE;
148 r->file.filename = xstrdup (handle_get_filename (r->fh));
151 r->file.sequence_no = NULL;
152 r->file.param = NULL;
153 r->file.postopen = NULL;
154 r->file.preclose = NULL;
155 if (!fn_open_ext (&r->file))
157 msg (ME, _("Could not open \"%s\" for reading "
158 "as a data file: %s."),
159 handle_get_filename (r->fh), strerror (errno));
161 fh_close (fh,"data file", "rs");
174 read_inline_record (struct dfm_reader *r)
176 if ((r->flags & DFM_SAW_BEGIN_DATA) == 0)
180 r->flags |= DFM_SAW_BEGIN_DATA;
182 /* FIXME: WTF can't this just be done with tokens?
183 Is this really a special case? */
188 if (!getl_read_line ())
190 msg (SE, _("BEGIN DATA expected."));
194 /* Skip leading whitespace, separate out first
195 word, so that S points to a single word reduced
197 s = ds_c_str (&getl_buf);
198 while (isspace ((unsigned char) *s))
200 for (cp = s; isalpha ((unsigned char) *cp); cp++)
201 *cp = tolower ((unsigned char) (*cp));
202 ds_truncate (&getl_buf, cp - s);
206 if (!lex_id_match_len ("begin", 5, s, strcspn (s, " \t\r\v\n")))
208 msg (SE, _("BEGIN DATA expected."));
209 lex_preprocess_line ();
212 getl_prompt = GETL_PRPT_DATA;
215 if (!getl_read_line ())
217 msg (SE, _("Unexpected end-of-file while reading data in BEGIN "
218 "DATA. This probably indicates "
219 "a missing or misformatted END DATA command. "
220 "END DATA must appear by itself on a single line "
221 "with exactly one space between words."));
226 r->where.line_number++;
228 if (ds_length (&getl_buf) >= 8
229 && !strncasecmp (ds_c_str (&getl_buf), "end data", 8))
231 lex_set_prog (ds_c_str (&getl_buf) + ds_length (&getl_buf));
235 ds_replace (&r->line, ds_c_str (&getl_buf));
240 read_file_record (struct dfm_reader *r)
242 assert (r->fh != NULL);
243 if (handle_get_mode (r->fh) == MODE_TEXT)
246 if (!ds_gets (&r->line, r->file.file))
248 if (ferror (r->file.file))
250 msg (ME, _("Error reading file %s: %s."),
251 handle_get_name (r->fh), strerror (errno));
257 else if (handle_get_mode (r->fh) == MODE_BINARY)
259 size_t record_width = handle_get_record_width (r->fh);
262 if (ds_length (&r->line) < record_width)
263 ds_rpad (&r->line, record_width, 0);
265 amt = fread (ds_c_str (&r->line), 1, record_width,
267 if (record_width != amt)
269 if (ferror (r->file.file))
270 msg (ME, _("Error reading file %s: %s."),
271 handle_get_name (r->fh), strerror (errno));
273 msg (ME, _("%s: Partial record at end of file."),
274 handle_get_name (r->fh));
285 r->where.line_number++;
290 /* Reads a record from R, setting the current position to the
291 start of the line. If an error occurs or end-of-file is
292 encountered, the current line is set to null. */
294 read_record (struct dfm_reader *r)
296 int success = r->fh != NULL ? read_file_record (r) : read_inline_record (r);
303 /* Returns nonzero if end of file has been reached on HANDLE.
304 Reads forward in HANDLE's file, if necessary to tell. */
306 dfm_eof (struct dfm_reader *r)
308 if (r->flags & DFM_ADVANCE)
310 r->flags &= ~DFM_ADVANCE;
311 if ((r->flags & DFM_EOF) == 0)
316 msg (SE, _("Attempt to read beyond end-of-file on file %s."),
317 handle_get_name (r->fh));
319 msg (SE, _("Attempt to read beyond END DATA."));
324 return (r->flags & DFM_EOF) != 0;
327 /* Returns the current record in the file corresponding to
328 HANDLE. Aborts if reading from the file is necessary or at
329 end of file, so call dfm_eof() first. Sets *LINE to the line,
330 which is not null-terminated. The caller must not free or
331 modify the returned string. */
333 dfm_get_record (struct dfm_reader *r, struct fixed_string *line)
335 assert ((r->flags & DFM_ADVANCE) == 0);
336 assert ((r->flags & DFM_EOF) == 0);
337 assert (r->pos <= ds_length (&r->line));
339 line->string = ds_data (&r->line) + r->pos;
340 line->length = ds_length (&r->line) - r->pos;
343 /* Expands tabs in the current line into the equivalent number of
344 spaces, if appropriate for this kind of file. Aborts if
345 reading from the file is necessary or at end of file, so call
348 dfm_expand_tabs (struct dfm_reader *r)
351 size_t ofs, new_pos, tab_width;
353 assert ((r->flags & DFM_ADVANCE) == 0);
354 assert ((r->flags & DFM_EOF) == 0);
355 assert (r->pos <= ds_length (&r->line));
357 if (r->flags & DFM_TABS_EXPANDED)
359 r->flags |= DFM_TABS_EXPANDED;
362 && (handle_get_mode (r->fh) == MODE_BINARY
363 || handle_get_tab_width (r->fh) == 0
364 || memchr (ds_c_str (&r->line), '\t', ds_length (&r->line)) == NULL))
367 /* Expand tabs from r->line into r->scratch, and figure out
368 new value for r->pos. */
369 tab_width = r->fh != NULL ? handle_get_tab_width (r->fh) : 8;
370 ds_clear (&r->scratch);
372 for (ofs = 0; ofs < ds_length (&r->line); ofs++)
377 new_pos = ds_length (&r->scratch);
379 c = ds_c_str (&r->line)[ofs];
381 ds_putc (&r->scratch, c);
385 ds_putc (&r->scratch, ' ');
386 while (ds_length (&r->scratch) % tab_width != 0);
390 /* Swap r->line and r->scratch and set new r->pos. */
392 r->line = r->scratch;
397 /* Causes dfm_get_record() to read in the next record the next time it
398 is executed on file HANDLE. */
400 dfm_forward_record (struct dfm_reader *r)
402 r->flags |= DFM_ADVANCE;
405 /* Cancels the effect of any previous dfm_fwd_record() executed
406 on file HANDLE. Sets the current line to begin in the 1-based
409 dfm_reread_record (struct dfm_reader *r, size_t column)
411 r->flags &= ~DFM_ADVANCE;
414 else if (column > ds_length (&r->line))
415 r->pos = ds_length (&r->line);
420 /* Sets the current line to begin COLUMNS characters following
421 the current start. */
423 dfm_forward_columns (struct dfm_reader *r, size_t columns)
425 dfm_reread_record (r, (r->pos + 1) + columns);
428 /* Returns the 1-based column to which the line pointer in HANDLE
429 is set. Unless dfm_reread_record() or dfm_forward_columns()
430 have been called, this is 1. */
432 dfm_column_start (struct dfm_reader *r)
437 /* Pushes the filename and line number on the fn/ln stack. */
439 dfm_push (struct dfm_reader *r)
442 err_push_file_locator (&r->where);
445 /* Pops the filename and line number from the fn/ln stack. */
447 dfm_pop (struct dfm_reader *r)
450 err_pop_file_locator (&r->where);
453 /* BEGIN DATA...END DATA procedure. */
455 /* Perform BEGIN DATA...END DATA as a procedure in itself. */
457 cmd_begin_data (void)
459 struct dfm_reader *r;
461 /* FIXME: figure out the *exact* conditions, not these really
462 lenient conditions. */
463 if (vfm_source == NULL
464 || case_source_is_class (vfm_source, &storage_source_class))
466 msg (SE, _("This command is not valid here since the current "
467 "input program does not access the inline file."));
472 /* Open inline file. */
473 r = dfm_open_reader (NULL);
474 r->flags |= DFM_SAW_BEGIN_DATA;
476 /* Input procedure reads from inline file. */
477 getl_prompt = GETL_PRPT_DATA;
478 procedure (NULL, NULL);
480 dfm_close_reader (r);