1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 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
27 #include <data/case-source.h>
28 #include <data/case.h>
29 #include <data/case-source.h>
30 #include <data/data-in.h>
31 #include <data/dictionary.h>
32 #include <data/format.h>
33 #include <data/procedure.h>
34 #include <data/settings.h>
35 #include <data/transformations.h>
36 #include <data/variable.h>
37 #include <language/command.h>
38 #include <language/data-io/data-reader.h>
39 #include <language/data-io/file-handle.h>
40 #include <language/data-io/inpt-pgm.h>
41 #include <language/data-io/placement-parser.h>
42 #include <language/lexer/format-parser.h>
43 #include <language/lexer/lexer.h>
44 #include <language/lexer/variable-parser.h>
45 #include <libpspp/alloc.h>
46 #include <libpspp/assertion.h>
47 #include <libpspp/compiler.h>
48 #include <libpspp/ll.h>
49 #include <libpspp/message.h>
50 #include <libpspp/misc.h>
51 #include <libpspp/pool.h>
52 #include <libpspp/str.h>
53 #include <output/table.h>
59 #define _(msgid) gettext (msgid)
61 /* Utility function. */
63 /* Describes how to parse one variable. */
66 struct ll ll; /* List element. */
69 struct fmt_spec input; /* Input format of this field. */
70 int fv; /* First value in case. */
71 char name[LONG_NAME_LEN + 1]; /* Var name for error messages and tables. */
73 /* Fixed format only. */
74 int record; /* Record number (1-based). */
75 int first_column; /* Column numbers in record. */
78 static struct dls_var_spec *
79 ll_to_dls_var_spec (struct ll *ll)
81 return ll_data (ll, struct dls_var_spec, ll);
84 /* Constants for DATA LIST type. */
92 /* DATA LIST private data structure. */
95 struct pool *pool; /* Used for all DATA LIST storage. */
96 struct ll_list specs; /* List of dls_var_specs. */
97 struct dfm_reader *reader; /* Data file reader. */
98 enum dls_type type; /* Type of DATA LIST construct. */
99 struct variable *end; /* Variable specified on END subcommand. */
100 int record_cnt; /* Number of records. */
101 struct string delims; /* Field delimiters. */
102 int skip_records; /* Records to skip before first case. */
105 static const struct case_source_class data_list_source_class;
107 static bool parse_fixed (struct lexer *, struct dictionary *dict,
108 struct pool *tmp_pool, struct data_list_pgm *);
109 static bool parse_free (struct lexer *, struct dictionary *dict,
110 struct pool *tmp_pool, struct data_list_pgm *);
111 static void dump_fixed_table (const struct ll_list *,
112 const struct file_handle *, int record_cnt);
113 static void dump_free_table (const struct data_list_pgm *,
114 const struct file_handle *);
116 static trns_free_func data_list_trns_free;
117 static trns_proc_func data_list_trns_proc;
120 cmd_data_list (struct lexer *lexer, struct dataset *ds)
122 struct dictionary *dict = dataset_dict (ds);
123 struct data_list_pgm *dls;
124 int table = -1; /* Print table if nonzero, -1=undecided. */
125 struct file_handle *fh = fh_inline_file ();
126 struct pool *tmp_pool;
129 if (!in_input_program ())
130 discard_variables (ds);
132 dls = pool_create_container (struct data_list_pgm, pool);
133 ll_init (&dls->specs);
138 dls->skip_records = 0;
139 ds_init_empty (&dls->delims);
140 ds_register_pool (&dls->delims, dls->pool);
142 tmp_pool = pool_create_subpool (dls->pool);
144 while (lex_token (lexer) != '/')
146 if (lex_match_id (lexer, "FILE"))
148 lex_match (lexer, '=');
149 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
153 else if (lex_match_id (lexer, "RECORDS"))
155 lex_match (lexer, '=');
156 lex_match (lexer, '(');
157 if (!lex_force_int (lexer))
159 dls->record_cnt = lex_integer (lexer);
161 lex_match (lexer, ')');
163 else if (lex_match_id (lexer, "SKIP"))
165 lex_match (lexer, '=');
166 if (!lex_force_int (lexer))
168 dls->skip_records = lex_integer (lexer);
171 else if (lex_match_id (lexer, "END"))
175 msg (SE, _("The END subcommand may only be specified once."));
179 lex_match (lexer, '=');
180 if (!lex_force_id (lexer))
182 dls->end = dict_lookup_var (dataset_dict (ds), lex_tokid (lexer));
184 dls->end = dict_create_var_assert (dataset_dict (ds), lex_tokid (lexer), 0);
187 else if (lex_token (lexer) == T_ID)
189 if (lex_match_id (lexer, "NOTABLE"))
191 else if (lex_match_id (lexer, "TABLE"))
196 if (lex_match_id (lexer, "FIXED"))
198 else if (lex_match_id (lexer, "FREE"))
200 else if (lex_match_id (lexer, "LIST"))
204 lex_error (lexer, NULL);
210 msg (SE, _("Only one of FIXED, FREE, or LIST may "
216 if ((dls->type == DLS_FREE || dls->type == DLS_LIST)
217 && lex_match (lexer, '('))
219 while (!lex_match (lexer, ')'))
223 if (lex_match_id (lexer, "TAB"))
225 else if (lex_token (lexer) == T_STRING && ds_length (lex_tokstr (lexer)) == 1)
227 delim = ds_first (lex_tokstr (lexer));
232 lex_error (lexer, NULL);
236 ds_put_char (&dls->delims, delim);
238 lex_match (lexer, ',');
245 lex_error (lexer, NULL);
250 fh_set_default_handle (fh);
253 dls->type = DLS_FIXED;
256 table = dls->type != DLS_FREE;
258 ok = (dls->type == DLS_FIXED ? parse_fixed : parse_free) (lexer, dict, tmp_pool, dls);
262 if (lex_end_of_command (lexer) != CMD_SUCCESS)
267 if (dls->type == DLS_FIXED)
268 dump_fixed_table (&dls->specs, fh, dls->record_cnt);
270 dump_free_table (dls, fh);
273 dls->reader = dfm_open_reader (fh, lexer);
274 if (dls->reader == NULL)
277 if (in_input_program ())
278 add_transformation (ds, data_list_trns_proc, data_list_trns_free, dls);
280 proc_set_source (ds, create_case_source (&data_list_source_class, dls));
282 pool_destroy (tmp_pool);
287 data_list_trns_free (dls);
288 return CMD_CASCADING_FAILURE;
291 /* Fixed-format parsing. */
293 /* Parses all the variable specifications for DATA LIST FIXED,
294 storing them into DLS. Uses TMP_POOL for data that is not
295 needed once parsing is complete. Returns true only if
298 parse_fixed (struct lexer *lexer, struct dictionary *dict,
299 struct pool *tmp_pool, struct data_list_pgm *dls)
301 int last_nonempty_record;
305 while (lex_token (lexer) != '.')
308 size_t name_cnt, name_idx;
309 struct fmt_spec *formats, *f;
312 /* Parse everything. */
313 if (!parse_record_placement (lexer, &record, &column)
314 || !parse_DATA_LIST_vars_pool (lexer, tmp_pool,
315 &names, &name_cnt, PV_NONE)
316 || !parse_var_placements (lexer, tmp_pool, name_cnt, true,
317 &formats, &format_cnt))
320 /* Create variables and var specs. */
322 for (f = formats; f < &formats[format_cnt]; f++)
323 if (!execute_placement_format (f, &record, &column))
328 struct dls_var_spec *spec;
330 name = names[name_idx++];
332 /* Create variable. */
333 width = fmt_var_width (f);
334 v = dict_create_var (dict, name, width);
338 struct fmt_spec output = fmt_for_output_from_input (f);
339 var_set_both_formats (v, &output);
344 This can be acceptable if we're in INPUT
345 PROGRAM, but only if the existing variable has
346 the same width as the one we would have
348 if (!in_input_program ())
350 msg (SE, _("%s is a duplicate variable name."), name);
354 v = dict_lookup_var_assert (dict, name);
355 if ((width != 0) != (var_get_width (v) != 0))
357 msg (SE, _("There is already a variable %s of a "
362 if (width != 0 && width != var_get_width (v))
364 msg (SE, _("There is already a string variable %s of a "
365 "different width."), name);
370 /* Create specifier for parsing the variable. */
371 spec = pool_alloc (dls->pool, sizeof *spec);
374 spec->record = record;
375 spec->first_column = column;
376 strcpy (spec->name, var_get_name (v));
377 ll_push_tail (&dls->specs, &spec->ll);
381 assert (name_idx == name_cnt);
383 if (ll_is_empty (&dls->specs))
385 msg (SE, _("At least one variable must be specified."));
389 last_nonempty_record = ll_to_dls_var_spec (ll_tail (&dls->specs))->record;
390 if (dls->record_cnt && last_nonempty_record > dls->record_cnt)
392 msg (SE, _("Variables are specified on records that "
393 "should not exist according to RECORDS subcommand."));
396 else if (!dls->record_cnt)
397 dls->record_cnt = last_nonempty_record;
402 /* Displays a table giving information on fixed-format variable
403 parsing on DATA LIST. */
405 dump_fixed_table (const struct ll_list *specs,
406 const struct file_handle *fh, int record_cnt)
410 struct dls_var_spec *spec;
413 spec_cnt = ll_count (specs);
414 t = tab_create (4, spec_cnt + 1, 0);
415 tab_columns (t, TAB_COL_DOWN, 1);
416 tab_headers (t, 0, 0, 1, 0);
417 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Variable"));
418 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Record"));
419 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Columns"));
420 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Format"));
421 tab_box (t, TAL_1, TAL_1, TAL_0, TAL_1, 0, 0, 3, spec_cnt);
422 tab_hline (t, TAL_2, 0, 3, 1);
423 tab_dim (t, tab_natural_dimensions);
426 ll_for_each (spec, struct dls_var_spec, ll, specs)
428 char fmt_string[FMT_STRING_LEN_MAX + 1];
429 tab_text (t, 0, row, TAB_LEFT, spec->name);
430 tab_text (t, 1, row, TAT_PRINTF, "%d", spec->record);
431 tab_text (t, 2, row, TAT_PRINTF, "%3d-%3d",
432 spec->first_column, spec->first_column + spec->input.w - 1);
433 tab_text (t, 3, row, TAB_LEFT | TAB_FIX,
434 fmt_to_string (&spec->input, fmt_string));
438 tab_title (t, ngettext ("Reading %d record from %s.",
439 "Reading %d records from %s.", record_cnt),
440 record_cnt, fh_get_name (fh));
444 /* Free-format parsing. */
446 /* Parses variable specifications for DATA LIST FREE and adds
447 them to DLS. Uses TMP_POOL for data that is not needed once
448 parsing is complete. Returns true only if successful. */
450 parse_free (struct lexer *lexer, struct dictionary *dict, struct pool *tmp_pool,
451 struct data_list_pgm *dls)
454 while (lex_token (lexer) != '.')
456 struct fmt_spec input, output;
461 if (!parse_DATA_LIST_vars_pool (lexer, tmp_pool,
462 &name, &name_cnt, PV_NONE))
465 if (lex_match (lexer, '('))
467 if (!parse_format_specifier (lexer, &input)
468 || !fmt_check_input (&input)
469 || !lex_force_match (lexer, ')'))
472 /* As a special case, N format is treated as F format
473 for free-field input. */
474 if (input.type == FMT_N)
477 output = fmt_for_output_from_input (&input);
481 lex_match (lexer, '*');
482 input = fmt_for_input (FMT_F, 8, 0);
483 output = *get_format ();
486 for (i = 0; i < name_cnt; i++)
488 struct dls_var_spec *spec;
491 v = dict_create_var (dict, name[i], fmt_var_width (&input));
494 msg (SE, _("%s is a duplicate variable name."), name[i]);
497 var_set_both_formats (v, &output);
499 spec = pool_alloc (dls->pool, sizeof *spec);
502 strcpy (spec->name, var_get_name (v));
503 ll_push_tail (&dls->specs, &spec->ll);
510 /* Displays a table giving information on free-format variable parsing
513 dump_free_table (const struct data_list_pgm *dls,
514 const struct file_handle *fh)
517 struct dls_var_spec *spec;
521 spec_cnt = ll_count (&dls->specs);
523 t = tab_create (2, spec_cnt + 1, 0);
524 tab_columns (t, TAB_COL_DOWN, 1);
525 tab_headers (t, 0, 0, 1, 0);
526 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Variable"));
527 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Format"));
528 tab_box (t, TAL_1, TAL_1, TAL_0, TAL_1, 0, 0, 1, spec_cnt);
529 tab_hline (t, TAL_2, 0, 1, 1);
530 tab_dim (t, tab_natural_dimensions);
532 ll_for_each (spec, struct dls_var_spec, ll, &dls->specs)
534 char str[FMT_STRING_LEN_MAX + 1];
535 tab_text (t, 0, row, TAB_LEFT, spec->name);
536 tab_text (t, 1, row, TAB_LEFT | TAB_FIX,
537 fmt_to_string (&spec->input, str));
541 tab_title (t, _("Reading free-form data from %s."), fh_get_name (fh));
546 /* Input procedure. */
548 /* Extracts a field from the current position in the current
549 record. Fields can be unquoted or quoted with single- or
550 double-quote characters.
552 *FIELD is set to the field content. The caller must not
553 or destroy this constant string.
555 After parsing the field, sets the current position in the
556 record to just past the field and any trailing delimiter.
557 Returns 0 on failure or a 1-based column number indicating the
558 beginning of the field on success. */
560 cut_field (const struct data_list_pgm *dls, struct substring *field)
562 struct substring line, p;
564 if (dfm_eof (dls->reader))
566 if (ds_is_empty (&dls->delims))
567 dfm_expand_tabs (dls->reader);
568 line = p = dfm_get_record (dls->reader);
570 if (ds_is_empty (&dls->delims))
572 bool missing_quote = false;
574 /* Skip leading whitespace. */
575 ss_ltrim (&p, ss_cstr (CC_SPACES));
579 /* Handle actual data, whether quoted or unquoted. */
580 if (ss_match_char (&p, '\''))
581 missing_quote = !ss_get_until (&p, '\'', field);
582 else if (ss_match_char (&p, '"'))
583 missing_quote = !ss_get_until (&p, '"', field);
585 ss_get_chars (&p, ss_cspan (p, ss_cstr ("," CC_SPACES)), field);
587 msg (SW, _("Quoted string extends beyond end of line."));
589 /* Skip trailing whitespace and a single comma if present. */
590 ss_ltrim (&p, ss_cstr (CC_SPACES));
591 ss_match_char (&p, ',');
593 dfm_forward_columns (dls->reader, ss_length (line) - ss_length (p));
597 if (!ss_is_empty (p))
598 ss_get_chars (&p, ss_cspan (p, ds_ss (&dls->delims)), field);
599 else if (dfm_columns_past_end (dls->reader) == 0)
601 /* A blank line or a line that ends in a delimiter has a
602 trailing blank field. */
608 /* Advance past the field.
610 Also advance past a trailing delimiter, regardless of
611 whether one actually existed. If we "skip" a delimiter
612 that was not actually there, then we will return
613 end-of-line on our next call, which is what we want. */
614 dfm_forward_columns (dls->reader, ss_length (line) - ss_length (p) + 1);
619 static bool read_from_data_list_fixed (const struct data_list_pgm *,
621 static bool read_from_data_list_free (const struct data_list_pgm *,
623 static bool read_from_data_list_list (const struct data_list_pgm *,
626 /* Reads a case from DLS into C.
627 Returns true if successful, false at end of file or on I/O error. */
629 read_from_data_list (const struct data_list_pgm *dls, struct ccase *c)
633 dfm_push (dls->reader);
637 retval = read_from_data_list_fixed (dls, c);
640 retval = read_from_data_list_free (dls, c);
643 retval = read_from_data_list_list (dls, c);
648 dfm_pop (dls->reader);
653 /* Reads a case from the data file into C, parsing it according
654 to fixed-format syntax rules in DLS.
655 Returns true if successful, false at end of file or on I/O error. */
657 read_from_data_list_fixed (const struct data_list_pgm *dls, struct ccase *c)
659 struct dls_var_spec *spec;
662 if (dfm_eof (dls->reader))
665 spec = ll_to_dls_var_spec (ll_head (&dls->specs));
666 for (row = 1; row <= dls->record_cnt; row++)
668 struct substring line;
670 if (dfm_eof (dls->reader))
672 msg (SW, _("Partial case of %d of %d records discarded."),
673 row - 1, dls->record_cnt);
676 dfm_expand_tabs (dls->reader);
677 line = dfm_get_record (dls->reader);
679 ll_for_each_continue (spec, struct dls_var_spec, ll, &dls->specs)
680 data_in (ss_substr (line, spec->first_column - 1, spec->input.w),
681 spec->input.type, spec->input.d, spec->first_column,
682 case_data_rw (c, spec->fv), fmt_var_width (&spec->input));
684 dfm_forward_record (dls->reader);
690 /* Reads a case from the data file into C, parsing it according
691 to free-format syntax rules in DLS.
692 Returns true if successful, false at end of file or on I/O error. */
694 read_from_data_list_free (const struct data_list_pgm *dls, struct ccase *c)
696 struct dls_var_spec *spec;
698 ll_for_each (spec, struct dls_var_spec, ll, &dls->specs)
700 struct substring field;
702 /* Cut out a field and read in a new record if necessary. */
703 while (!cut_field (dls, &field))
705 if (!dfm_eof (dls->reader))
706 dfm_forward_record (dls->reader);
707 if (dfm_eof (dls->reader))
709 if (&spec->ll != ll_head (&dls->specs))
710 msg (SW, _("Partial case discarded. The first variable "
711 "missing was %s."), spec->name);
716 data_in (field, spec->input.type, 0,
717 dfm_get_column (dls->reader, ss_data (field)),
718 case_data_rw (c, spec->fv), fmt_var_width (&spec->input));
723 /* Reads a case from the data file and parses it according to
724 list-format syntax rules.
725 Returns true if successful, false at end of file or on I/O error. */
727 read_from_data_list_list (const struct data_list_pgm *dls, struct ccase *c)
729 struct dls_var_spec *spec;
731 if (dfm_eof (dls->reader))
734 ll_for_each (spec, struct dls_var_spec, ll, &dls->specs)
736 struct substring field;
738 if (!cut_field (dls, &field))
740 if (get_undefined ())
741 msg (SW, _("Missing value(s) for all variables from %s onward. "
742 "These will be filled with the system-missing value "
743 "or blanks, as appropriate."),
745 ll_for_each_continue (spec, struct dls_var_spec, ll, &dls->specs)
747 int width = fmt_var_width (&spec->input);
749 case_data_rw (c, spec->fv)->f = SYSMIS;
751 memset (case_data_rw (c, spec->fv)->s, ' ', width);
756 data_in (field, spec->input.type, 0,
757 dfm_get_column (dls->reader, ss_data (field)),
758 case_data_rw (c, spec->fv), fmt_var_width (&spec->input));
761 dfm_forward_record (dls->reader);
765 /* Destroys DATA LIST transformation DLS.
766 Returns true if successful, false if an I/O error occurred. */
768 data_list_trns_free (void *dls_)
770 struct data_list_pgm *dls = dls_;
771 dfm_close_reader (dls->reader);
772 pool_destroy (dls->pool);
776 /* Handle DATA LIST transformation DLS, parsing data into C. */
778 data_list_trns_proc (void *dls_, struct ccase *c, casenumber case_num UNUSED)
780 struct data_list_pgm *dls = dls_;
783 if (read_from_data_list (dls, c))
784 retval = TRNS_CONTINUE;
785 else if (dfm_reader_error (dls->reader) || dfm_eof (dls->reader) > 1)
787 /* An I/O error, or encountering end of file for a second
788 time, should be escalated into a more serious error. */
792 retval = TRNS_END_FILE;
794 /* If there was an END subcommand handle it. */
795 if (dls->end != NULL)
797 double *end = &case_data_rw (c, dls->end->fv)->f;
798 if (retval == TRNS_DROP_CASE)
801 retval = TRNS_END_FILE;
810 /* Reads all the records from the data file and passes them to
812 Returns true if successful, false if an I/O error occurred. */
814 data_list_source_read (struct case_source *source,
816 write_case_func *write_case, write_case_data wc_data)
818 struct data_list_pgm *dls = source->aux;
820 /* Skip the requested number of records before reading the
822 while (dls->skip_records > 0)
824 if (dfm_eof (dls->reader))
826 dfm_forward_record (dls->reader);
834 if (!read_from_data_list (dls, c))
835 return !dfm_reader_error (dls->reader);
837 dfm_push (dls->reader);
838 ok = write_case (wc_data);
839 dfm_pop (dls->reader);
845 /* Destroys the source's internal data. */
847 data_list_source_destroy (struct case_source *source)
849 data_list_trns_free (source->aux);
852 static const struct case_source_class data_list_source_class =
856 data_list_source_read,
857 data_list_source_destroy,