1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 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., 59 Temple Place - Suite 330, Boston, MA
29 #include "file-handle.h"
37 #include "debug-print.h"
39 /* file_handle extension structure. */
42 struct file_ext file; /* Associated file. */
44 char *line; /* Current line, not null-terminated. */
45 size_t len; /* Length of line. */
47 char *ptr; /* Pointer into line that is returned by
49 size_t size; /* Number of bytes allocated for line. */
50 int advance; /* Nonzero=dfm_get_record() reads a new
51 record; otherwise returns current record. */
54 /* These are defined at the end of this file. */
55 static struct fh_ext_class dfm_r_class;
56 static struct fh_ext_class dfm_w_class;
58 static void read_record (struct file_handle *h);
60 /* Internal (low level). */
62 /* Closes the file handle H which was opened by open_file_r() or
65 dfm_close (struct file_handle *h)
67 struct dfm_fhuser_ext *ext = h->ext;
69 /* Skip any remaining data on the inline file. */
71 while (ext->line != NULL)
74 msg (VM (2), _("%s: Closing data-file handle %s."),
75 fh_handle_filename (h), fh_handle_name (h));
76 assert (h->class == &dfm_r_class || h->class == &dfm_w_class);
79 fn_close_ext (&ext->file);
80 free (ext->file.filename);
81 ext->file.filename = NULL;
87 /* Initializes EXT properly as an inline data file. */
89 open_inline_file (struct dfm_fhuser_ext *ext)
91 /* We want to indicate that the file is open, that we are not at
92 eof, and that another line needs to be read in. */
93 ext->file.file = NULL;
94 ext->line = xmalloc (128);
96 strcpy (ext->line, _("<<Bug in dfm.c>>"));
98 ext->len = strlen (ext->line);
104 /* Opens a file handle for reading as a data file. */
106 open_file_r (struct file_handle *h)
108 struct dfm_fhuser_ext ext;
110 h->where.line_number = 0;
111 ext.file.file = NULL;
118 msg (VM (1), _("%s: Opening data-file handle %s for reading."),
119 fh_handle_filename (h), fh_handle_name (h));
122 if (h == inline_file)
126 /* WTF can't this just be done with tokens?
127 Is this really a special case?
133 if (!getl_read_line ())
135 msg (SE, _("BEGIN DATA expected."));
139 /* Skip leading whitespace, separate out first word, so that
140 S points to a single word reduced to lowercase. */
141 s = ds_value (&getl_buf);
142 while (isspace ((unsigned char) *s))
144 for (cp = s; isalpha ((unsigned char) *cp); cp++)
145 *cp = tolower ((unsigned char) (*cp));
146 ds_truncate (&getl_buf, cp - s);
150 if (!lex_id_match_len ("begin", 5, s, strcspn (s, " \t\r\v\n")))
152 msg (SE, _("BEGIN DATA expected."));
154 lex_preprocess_line ();
157 getl_prompt = GETL_PRPT_DATA;
159 open_inline_file (&ext);
163 ext.file.filename = xstrdup (h->norm_fn);
164 ext.file.mode = "rb";
165 ext.file.file = NULL;
166 ext.file.sequence_no = NULL;
167 ext.file.param = NULL;
168 ext.file.postopen = NULL;
169 ext.file.preclose = NULL;
170 if (!fn_open_ext (&ext.file))
172 msg (ME, _("An error occurred while opening \"%s\" for reading "
173 "as a data file: %s."), h->fn, strerror (errno));
179 h->class = &dfm_r_class;
180 h->ext = xmalloc (sizeof (struct dfm_fhuser_ext));
181 memcpy (h->ext, &ext, sizeof (struct dfm_fhuser_ext));
186 /* Opens a file handle for writing as a data file. */
188 open_file_w (struct file_handle *h)
190 struct dfm_fhuser_ext ext;
192 ext.file.file = NULL;
199 h->where.line_number = 0;
201 msg (VM (1), _("%s: Opening data-file handle %s for writing."),
202 fh_handle_filename (h), fh_handle_name (h));
205 if (h == inline_file)
207 msg (ME, _("Cannot open the inline file for writing."));
212 ext.file.filename = xstrdup (h->norm_fn);
213 ext.file.mode = "wb";
214 ext.file.file = NULL;
215 ext.file.sequence_no = NULL;
216 ext.file.param = NULL;
217 ext.file.postopen = NULL;
218 ext.file.preclose = NULL;
220 if (!fn_open_ext (&ext.file))
222 msg (ME, _("An error occurred while opening \"%s\" for writing "
223 "as a data file: %s."), h->fn, strerror (errno));
228 h->class = &dfm_w_class;
229 h->ext = xmalloc (sizeof (struct dfm_fhuser_ext));
230 memcpy (h->ext, &ext, sizeof (struct dfm_fhuser_ext));
235 /* Ensures that the line buffer in file handle with extension EXT is
236 big enough to hold a line of length EXT->LEN characters not
237 including null terminator. */
238 #define force_line_buffer_expansion() \
241 if (ext->len + 1 > ext->size) \
243 ext->size = ext->len * 2; \
244 ext->line = xrealloc (ext->line, ext->size); \
249 /* Counts the number of tabs in string STRING of length LEN. */
251 count_tabs (char *s, size_t len)
257 char *cp = memchr (s, '\t', len);
266 /* Converts all the tabs in H->EXT->LINE to an equivalent number of
267 spaces, if necessary. */
269 tabs_to_spaces (struct file_handle *h)
271 struct dfm_fhuser_ext *ext = h->ext;
273 char *first_tab; /* Location of first tab (if any). */
274 char *second_tab; /* Location of second tab (if any). */
275 size_t orig_len; /* Line length at function entry. */
277 /* If there aren't any tabs then there's nothing to do. */
278 first_tab = memchr (ext->line, '\t', ext->len);
279 if (first_tab == NULL)
283 /* If there's just one tab then expand it inline. Otherwise do a
284 full string copy to another buffer. */
285 second_tab = memchr (first_tab + 1, '\t',
286 ext->len - (first_tab - ext->line + 1));
287 if (second_tab == NULL)
289 int n_spaces = 8 - (first_tab - ext->line) % 8;
291 ext->len += n_spaces - 1;
293 /* Expand the line if necessary, keeping the first_tab pointer
296 size_t ofs = first_tab - ext->line;
297 force_line_buffer_expansion ();
298 first_tab = ext->line + ofs;
301 memmove (first_tab + n_spaces, first_tab + 1,
302 orig_len - (first_tab - ext->line + 1));
303 memset (first_tab, ' ', n_spaces);
305 /* Make a local copy of original text. */
306 char *orig_line = local_alloc (ext->len + 1);
307 memcpy (orig_line, ext->line, ext->len);
309 /* Allocate memory assuming we need to add 8 spaces for every tab. */
310 ext->len += 2 + count_tabs (second_tab + 1,
311 ext->len - (second_tab - ext->line + 1));
313 /* Expand the line if necessary, keeping the first_tab pointer
316 size_t ofs = first_tab - ext->line;
317 force_line_buffer_expansion ();
318 first_tab = ext->line + ofs;
321 /* Walk through orig_line, expanding tabs into ext->line. */
323 char *src_p = orig_line + (first_tab - ext->line);
324 char *dest_p = first_tab;
326 for (; src_p < orig_line + orig_len; src_p++)
328 /* Most characters simply pass through untouched. */
335 /* Tabs are expanded into an equivalent number of
338 int n_spaces = 8 - (dest_p - ext->line) % 8;
340 memset (dest_p, ' ', n_spaces);
345 /* Supply null terminator and actual string length. */
347 ext->len = dest_p - ext->line;
350 local_free (orig_line);
354 /* Reads a record from H->EXT->FILE into H->EXT->LINE, setting
355 H->EXT->PTR to H->EXT->LINE, and setting H->EXT-LEN to the length
356 of the line. The line is not null-terminated. If an error occurs
357 or end-of-file is encountered, H->EXT->LINE is set to NULL. */
359 read_record (struct file_handle *h)
361 struct dfm_fhuser_ext *ext = h->ext;
363 if (h == inline_file)
365 if (!getl_read_line ())
367 msg (SE, _("Unexpected end-of-file while reading data in BEGIN "
368 "DATA. This probably indicates "
369 "a missing or misformatted END DATA command. "
370 "END DATA must appear by itself on a single line "
371 "with exactly one space between words."));
375 h->where.line_number++;
377 if (ds_length (&getl_buf) >= 8
378 && !strncasecmp (ds_value (&getl_buf), "end data", 8))
380 lex_set_prog (ds_value (&getl_buf) + ds_length (&getl_buf));
384 ext->len = ds_length (&getl_buf);
385 force_line_buffer_expansion ();
386 strcpy (ext->line, ds_value (&getl_buf));
390 if (h->recform == FH_RF_VARIABLE)
392 /* PORTME: here you should adapt the routine to your
393 system's concept of a "line" of text. */
394 int read_len = getline (&ext->line, &ext->size, ext->file.file);
398 if (ferror (ext->file.file))
400 msg (ME, _("Error reading file %s: %s."),
401 fh_handle_name (h), strerror (errno));
406 ext->len = (size_t) read_len;
408 else if (h->recform == FH_RF_FIXED)
412 if (ext->size < h->lrecl)
414 ext->size = h->lrecl;
415 ext->line = xmalloc (ext->size);
417 amt = fread (ext->line, 1, h->lrecl, ext->file.file);
420 if (ferror (ext->file.file))
421 msg (ME, _("Error reading file %s: %s."),
422 fh_handle_name (h), strerror (errno));
424 msg (ME, _("%s: Partial record at end of file."),
436 h->where.line_number++;
439 /* Strip trailing whitespace, I forget why. But there's a good
440 reason, I'm sure. I'm too scared to eliminate this code. */
441 if (h->recform == FH_RF_VARIABLE)
443 while (ext->len && isspace ((unsigned char) ext->line[ext->len - 1]))
446 /* Convert tabs to spaces. */
449 ext->ptr = ext->line;
454 /*hit eof or an error, clean up everything. */
458 ext->line = ext->ptr = NULL;
462 /* Public (high level). */
464 /* Returns the current record in the file corresponding to HANDLE.
465 Opens files and reads records, etc., as necessary. Sets *LEN to
466 the length of the line. The line returned is not null-terminated.
467 Returns NULL at end of file. Calls fail() on attempt to read past
470 dfm_get_record (struct file_handle *h, int *len)
474 if (h->class == NULL)
476 if (!open_file_r (h))
480 else if (h->class != &dfm_r_class)
482 msg (ME, _("Cannot read from file %s already opened for %s."),
483 fh_handle_name (h), gettext (h->class->name));
488 struct dfm_fhuser_ext *ext = h->ext;
496 msg (SE, _("Attempt to read beyond end-of-file on file %s."),
504 struct dfm_fhuser_ext *ext = h->ext;
510 *len = ext->len - (ext->ptr - ext->line);
518 /* Come here on reading beyond eof or reading from a file already
519 open for something else. */
525 /* Causes dfm_get_record() to read in the next record the next time it
526 is executed on file HANDLE. */
528 dfm_fwd_record (struct file_handle *h)
530 struct dfm_fhuser_ext *ext = h->ext;
532 assert (h->class == &dfm_r_class);
536 /* Cancels the effect of any previous dfm_fwd_record() executed on
537 file HANDLE. Sets the current line to begin in the 1-based column
538 COLUMN, as with dfm_set_record but based on a column number instead
539 of a character pointer. */
541 dfm_bkwd_record (struct file_handle *h, int column)
543 struct dfm_fhuser_ext *ext = h->ext;
545 assert (h->class == &dfm_r_class);
547 ext->ptr = ext->line + min ((int) ext->len + 1, column) - 1;
550 /* Sets the current line in HANDLE to NEW_LINE, which must point
551 somewhere in the line last returned by dfm_get_record(). Used by
552 DATA LIST FREE to strip the leading portion off the current line. */
554 dfm_set_record (struct file_handle *h, char *new_line)
556 struct dfm_fhuser_ext *ext = h->ext;
558 assert (h->class == &dfm_r_class);
562 /* Returns the 0-based current column to which the line pointer in
563 HANDLE is set. Unless dfm_set_record() or dfm_bkwd_record() have
564 been called, this is 0. */
566 dfm_get_cur_col (struct file_handle *h)
568 struct dfm_fhuser_ext *ext = h->ext;
570 assert (h->class == &dfm_r_class);
571 return ext->ptr - ext->line;
574 /* Writes record REC having length LEN to the file corresponding to
575 HANDLE. REC is not null-terminated. Returns nonzero on success,
578 dfm_put_record (struct file_handle *h, const char *rec, size_t len)
583 if (h->class == NULL)
585 if (!open_file_w (h))
588 else if (h->class != &dfm_w_class)
590 msg (ME, _("Cannot write to file %s already opened for %s."),
591 fh_handle_name (h), gettext (h->class->name));
596 if (h->recform == FH_RF_FIXED && len < h->lrecl)
601 ptr = local_alloc (amt);
602 memcpy (ptr, rec, len);
603 ch = h->mode == FH_MD_CHARACTER ? ' ' : 0;
604 memset (&ptr[len], ch, amt - len);
612 if (1 != fwrite (ptr, amt, 1, ((struct dfm_fhuser_ext *) h->ext)->file.file))
614 msg (ME, _("Error writing file %s: %s."), fh_handle_name (h),
626 /* Pushes the filename and line number on the fn/ln stack. */
628 dfm_push (struct file_handle *h)
630 if (h != inline_file)
631 err_push_file_locator (&h->where);
634 /* Pops the filename and line number from the fn/ln stack. */
636 dfm_pop (struct file_handle *h)
638 if (h != inline_file)
639 err_pop_file_locator (&h->where);
642 /* BEGIN DATA...END DATA procedure. */
644 /* Perform BEGIN DATA...END DATA as a procedure in itself. */
646 cmd_begin_data (void)
648 struct dfm_fhuser_ext *ext;
650 /* FIXME: figure out the *exact* conditions, not these really
651 lenient conditions. */
652 if (vfm_source == NULL
653 || case_source_is_class (vfm_source, &memory_source_class)
654 || case_source_is_class (vfm_source, &disk_source_class)
655 || case_source_is_class (vfm_source, &sort_source_class))
657 msg (SE, _("This command is not valid here since the current "
658 "input program does not access the inline file."));
663 /* Initialize inline_file. */
664 msg (VM (1), _("inline file: Opening for reading."));
665 inline_file->class = &dfm_r_class;
666 inline_file->ext = xmalloc (sizeof (struct dfm_fhuser_ext));
667 open_inline_file (inline_file->ext);
669 /* We don't actually read from the inline file. The input procedure
670 is what reads from it. */
671 getl_prompt = GETL_PRPT_DATA;
672 procedure (NULL, NULL, NULL, NULL);
674 ext = inline_file->ext;
676 if (ext && ext->line)
678 msg (MW, _("Skipping remaining inline data."));
679 for (read_record (inline_file); ext->line; read_record (inline_file))
682 assert (inline_file->ext == NULL);
687 static struct fh_ext_class dfm_r_class =
690 N_("reading as a data file"),
694 static struct fh_ext_class dfm_w_class =
697 N_("writing as a data file"),