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
21 #include "data-list.h"
31 #include "debug-print.h"
33 #include "dictionary.h"
35 #include "file-handle.h"
45 /* Utility function. */
47 /* FIXME: Either REPEATING DATA must be the last transformation, or we
48 must multiplex the transformations that follow (i.e., perform them
49 for every case that we produce from a repetition instance).
50 Currently we do neither. We should do one or the other. */
52 /* Describes how to parse one variable. */
55 struct dls_var_spec *next; /* Next specification in list. */
57 /* Both free and fixed formats. */
58 struct fmt_spec input; /* Input format of this field. */
59 struct variable *v; /* Associated variable. Used only in
60 parsing. Not safe later. */
61 int fv; /* First value in case. */
63 /* Fixed format only. */
64 int rec; /* Record number (1-based). */
65 int fc, lc; /* Column numbers in record. */
67 /* Free format only. */
68 char name[9]; /* Name of variable. */
71 /* Constants for DATA LIST type. */
72 /* Must match table in cmd_data_list(). */
80 /* DATA LIST private data structure. */
85 struct dls_var_spec *first, *last; /* Variable parsing specifications. */
86 struct dfm_reader *reader; /* Data file reader. */
88 int type; /* A DLS_* constant. */
89 struct variable *end; /* Variable specified on END subcommand. */
90 int eof; /* End of file encountered. */
91 int rec_cnt; /* Number of records. */
92 size_t case_size; /* Case size in bytes. */
93 char *delims; /* Delimiters if any; not null-terminated. */
94 size_t delim_cnt; /* Number of delimiter, or 0 for spaces. */
97 static int parse_fixed (struct data_list_pgm *);
98 static int parse_free (struct dls_var_spec **, struct dls_var_spec **);
99 static void dump_fixed_table (const struct dls_var_spec *,
100 const struct file_handle *, int rec_cnt);
101 static void dump_free_table (const struct data_list_pgm *,
102 const struct file_handle *);
103 static void destroy_dls_var_spec (struct dls_var_spec *);
104 static trns_free_func data_list_trns_free;
105 static trns_proc_func data_list_trns_proc;
107 /* Message title for REPEATING DATA. */
108 #define RPD_ERR "REPEATING DATA: "
113 struct data_list_pgm *dls; /* DATA LIST program under construction. */
114 int table = -1; /* Print table if nonzero, -1=undecided. */
115 struct file_handle *fh = NULL; /* File handle of source, NULL=inline file. */
117 if (!case_source_is_complex (vfm_source))
118 discard_variables ();
120 dls = xmalloc (sizeof *dls);
128 dls->first = dls->last = NULL;
132 if (lex_match_id ("FILE"))
138 if (case_source_is_class (vfm_source, &file_type_source_class)
139 && fh != default_handle)
141 msg (SE, _("DATA LIST may not use a different file from "
142 "that specified on its surrounding FILE TYPE."));
146 else if (lex_match_id ("RECORDS"))
150 if (!lex_force_int ())
152 dls->rec_cnt = lex_integer ();
156 else if (lex_match_id ("END"))
160 msg (SE, _("The END subcommand may only be specified once."));
165 if (!lex_force_id ())
167 dls->end = dict_lookup_var (default_dict, tokid);
169 dls->end = dict_create_var_assert (default_dict, tokid, 0);
172 else if (token == T_ID)
174 if (lex_match_id ("NOTABLE"))
176 else if (lex_match_id ("TABLE"))
181 if (lex_match_id ("FIXED"))
183 else if (lex_match_id ("FREE"))
185 else if (lex_match_id ("LIST"))
195 msg (SE, _("Only one of FIXED, FREE, or LIST may "
201 if ((dls->type == DLS_FREE || dls->type == DLS_LIST)
204 while (!lex_match (')'))
208 if (lex_match_id ("TAB"))
210 else if (token == T_STRING && tokstr.length == 1)
212 delim = tokstr.string[0];
221 dls->delims = xrealloc (dls->delims, dls->delim_cnt + 1);
222 dls->delims[dls->delim_cnt++] = delim;
236 dls->case_size = dict_get_case_size (default_dict);
240 dls->type = DLS_FIXED;
244 if (dls->type == DLS_FREE)
250 if (dls->type == DLS_FIXED)
252 if (!parse_fixed (dls))
255 dump_fixed_table (dls->first, fh, dls->rec_cnt);
259 if (!parse_free (&dls->first, &dls->last))
262 dump_free_table (dls, fh);
265 dls->reader = dfm_open_reader (fh);
266 if (dls->reader == NULL)
269 if (vfm_source != NULL)
271 struct data_list_pgm *new_pgm;
273 dls->h.proc = data_list_trns_proc;
274 dls->h.free = data_list_trns_free;
276 new_pgm = xmalloc (sizeof *new_pgm);
277 memcpy (new_pgm, &dls, sizeof *new_pgm);
278 add_transformation (&new_pgm->h);
281 vfm_source = create_case_source (&data_list_source_class, dls);
286 destroy_dls_var_spec (dls->first);
292 /* Adds SPEC to the linked list with head at FIRST and tail at
295 append_var_spec (struct dls_var_spec **first, struct dls_var_spec **last,
296 struct dls_var_spec *spec)
303 (*last)->next = spec;
307 /* Fixed-format parsing. */
309 /* Used for chaining together fortran-like format specifiers. */
312 struct fmt_list *next;
315 struct fmt_list *down;
318 /* State of parsing DATA LIST. */
319 struct fixed_parsing_state
321 char **name; /* Variable names. */
322 int name_cnt; /* Number of names. */
324 int recno; /* Index of current record. */
325 int sc; /* 1-based column number of starting column for
326 next field to output. */
329 static int fixed_parse_compatible (struct fixed_parsing_state *,
330 struct dls_var_spec **,
331 struct dls_var_spec **);
332 static int fixed_parse_fortran (struct fixed_parsing_state *,
333 struct dls_var_spec **,
334 struct dls_var_spec **);
336 /* Parses all the variable specifications for DATA LIST FIXED,
337 storing them into DLS. Returns nonzero if successful. */
339 parse_fixed (struct data_list_pgm *dls)
341 struct fixed_parsing_state fx;
349 while (lex_match ('/'))
352 if (lex_is_integer ())
354 if (lex_integer () < fx.recno)
356 msg (SE, _("The record number specified, %ld, is "
357 "before the previous record, %d. Data "
358 "fields must be listed in order of "
359 "increasing record number."),
360 lex_integer (), fx.recno - 1);
364 fx.recno = lex_integer ();
370 if (!parse_DATA_LIST_vars (&fx.name, &fx.name_cnt, PV_NONE))
373 if (lex_is_number ())
375 if (!fixed_parse_compatible (&fx, &dls->first, &dls->last))
378 else if (token == '(')
380 if (!fixed_parse_fortran (&fx, &dls->first, &dls->last))
385 msg (SE, _("SPSS-like or FORTRAN-like format "
386 "specification expected after variable names."));
390 for (i = 0; i < fx.name_cnt; i++)
394 if (dls->first == NULL)
396 msg (SE, _("At least one variable must be specified."));
399 if (dls->rec_cnt && dls->last->rec > dls->rec_cnt)
401 msg (SE, _("Variables are specified on records that "
402 "should not exist according to RECORDS subcommand."));
405 else if (!dls->rec_cnt)
406 dls->rec_cnt = dls->last->rec;
409 lex_error (_("expecting end of command"));
415 for (i = 0; i < fx.name_cnt; i++)
421 /* Parses a variable specification in the form 1-10 (A) based on
422 FX and adds specifications to the linked list with head at
423 FIRST and tail at LAST. */
425 fixed_parse_compatible (struct fixed_parsing_state *fx,
426 struct dls_var_spec **first, struct dls_var_spec **last)
428 struct fmt_spec input;
434 if (!lex_force_int ())
439 msg (SE, _("Column positions for fields must be positive."));
445 lex_negative_to_dash ();
448 if (!lex_force_int ())
453 msg (SE, _("Column positions for fields must be positive."));
458 msg (SE, _("The ending column for a field must be "
459 "greater than the starting column."));
468 /* Divide columns evenly. */
469 input.w = (lc - fc + 1) / fx->name_cnt;
470 if ((lc - fc + 1) % fx->name_cnt)
472 msg (SE, _("The %d columns %d-%d "
473 "can't be evenly divided into %d fields."),
474 lc - fc + 1, fc, lc, fx->name_cnt);
478 /* Format specifier. */
481 struct fmt_desc *fdp;
487 input.type = parse_format_specifier_name (&cp, 0);
488 if (input.type == -1)
492 msg (SE, _("A format specifier on this line "
493 "has extra characters on the end."));
503 if (lex_is_integer ())
505 if (lex_integer () < 1)
507 msg (SE, _("The value for number of decimal places "
508 "must be at least 1."));
512 input.d = lex_integer ();
518 fdp = &formats[input.type];
519 if (fdp->n_args < 2 && input.d)
521 msg (SE, _("Input format %s doesn't accept decimal places."),
529 if (!lex_force_match (')'))
537 if (!check_input_specifier (&input, 1))
540 /* Start column for next specification. */
543 /* Width of variables to create. */
544 if (input.type == FMT_A || input.type == FMT_AHEX)
549 /* Create variables and var specs. */
550 for (i = 0; i < fx->name_cnt; i++)
552 struct dls_var_spec *spec;
555 v = dict_create_var (default_dict, fx->name[i], width);
558 convert_fmt_ItoO (&input, &v->print);
560 if (!case_source_is_complex (vfm_source))
565 v = dict_lookup_var_assert (default_dict, fx->name[i]);
566 if (vfm_source == NULL)
568 msg (SE, _("%s is a duplicate variable name."), fx->name[i]);
571 if ((width != 0) != (v->width != 0))
573 msg (SE, _("There is already a variable %s of a "
578 if (width != 0 && width != v->width)
580 msg (SE, _("There is already a string variable %s of a "
581 "different width."), fx->name[i]);
586 spec = xmalloc (sizeof *spec);
590 spec->rec = fx->recno;
591 spec->fc = fc + input.w * i;
592 spec->lc = spec->fc + input.w - 1;
593 append_var_spec (first, last, spec);
598 /* Destroy format list F and, if RECURSE is nonzero, all its
601 destroy_fmt_list (struct fmt_list *f, int recurse)
603 struct fmt_list *next;
608 if (recurse && f->f.type == FMT_DESCEND)
609 destroy_fmt_list (f->down, 1);
614 /* Takes a hierarchically structured fmt_list F as constructed by
615 fixed_parse_fortran(), and flattens it, adding the variable
616 specifications to the linked list with head FIRST and tail
617 LAST. NAME_IDX is used to take values from the list of names
618 in FX; it should initially point to a value of 0. */
620 dump_fmt_list (struct fixed_parsing_state *fx, struct fmt_list *f,
621 struct dls_var_spec **first, struct dls_var_spec **last,
626 for (; f; f = f->next)
627 if (f->f.type == FMT_X)
629 else if (f->f.type == FMT_T)
631 else if (f->f.type == FMT_NEWREC)
633 fx->recno += f->count;
637 for (i = 0; i < f->count; i++)
638 if (f->f.type == FMT_DESCEND)
640 if (!dump_fmt_list (fx, f->down, first, last, name_idx))
645 struct dls_var_spec *spec;
649 if (formats[f->f.type].cat & FCAT_STRING)
653 if (*name_idx >= fx->name_cnt)
655 msg (SE, _("The number of format "
656 "specifications exceeds the given number of "
661 v = dict_create_var (default_dict, fx->name[(*name_idx)++], width);
664 msg (SE, _("%s is a duplicate variable name."), fx->name[i]);
668 if (!case_source_is_complex (vfm_source))
671 spec = xmalloc (sizeof *spec);
675 spec->rec = fx->recno;
677 spec->lc = fx->sc + f->f.w - 1;
678 append_var_spec (first, last, spec);
680 convert_fmt_ItoO (&spec->input, &v->print);
688 /* Recursively parses a FORTRAN-like format specification into
689 the linked list with head FIRST and tail TAIL. LEVEL is the
690 level of recursion, starting from 0. Returns the parsed
691 specification if successful, or a null pointer on failure. */
692 static struct fmt_list *
693 fixed_parse_fortran_internal (struct fixed_parsing_state *fx,
694 struct dls_var_spec **first,
695 struct dls_var_spec **last)
697 struct fmt_list *head = NULL;
698 struct fmt_list *tail = NULL;
700 lex_force_match ('(');
704 struct fmt_list *new = xmalloc (sizeof *new);
707 /* Append new to list. */
715 if (lex_is_integer ())
717 new->count = lex_integer ();
723 /* Parse format specifier. */
726 new->f.type = FMT_DESCEND;
727 new->down = fixed_parse_fortran_internal (fx, first, last);
728 if (new->down == NULL)
731 else if (lex_match ('/'))
732 new->f.type = FMT_NEWREC;
733 else if (!parse_format_specifier (&new->f, FMTP_ALLOW_XT)
734 || !check_input_specifier (&new->f, 1))
739 lex_force_match (')');
744 destroy_fmt_list (head, 0);
749 /* Parses a FORTRAN-like format specification into the linked
750 list with head FIRST and tail LAST. Returns nonzero if
753 fixed_parse_fortran (struct fixed_parsing_state *fx,
754 struct dls_var_spec **first, struct dls_var_spec **last)
756 struct fmt_list *list;
759 list = fixed_parse_fortran_internal (fx, first, last);
764 dump_fmt_list (fx, list, first, last, &name_idx);
765 destroy_fmt_list (list, 1);
766 if (name_idx < fx->name_cnt)
768 msg (SE, _("There aren't enough format specifications "
769 "to match the number of variable names given."));
776 /* Displays a table giving information on fixed-format variable
777 parsing on DATA LIST. */
778 /* FIXME: The `Columns' column should be divided into three columns,
779 one for the starting column, one for the dash, one for the ending
780 column; then right-justify the starting column and left-justify the
783 dump_fixed_table (const struct dls_var_spec *specs,
784 const struct file_handle *fh, int rec_cnt)
786 const struct dls_var_spec *spec;
790 for (i = 0, spec = specs; spec; spec = spec->next)
792 t = tab_create (4, i + 1, 0);
793 tab_columns (t, TAB_COL_DOWN, 1);
794 tab_headers (t, 0, 0, 1, 0);
795 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Variable"));
796 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Record"));
797 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Columns"));
798 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Format"));
799 tab_box (t, TAL_1, TAL_1, TAL_0, TAL_1, 0, 0, 3, i);
800 tab_hline (t, TAL_2, 0, 3, 1);
801 tab_dim (t, tab_natural_dimensions);
803 for (i = 1, spec = specs; spec; spec = spec->next, i++)
805 tab_text (t, 0, i, TAB_LEFT, spec->v->name);
806 tab_text (t, 1, i, TAT_PRINTF, "%d", spec->rec);
807 tab_text (t, 2, i, TAT_PRINTF, "%3d-%3d",
809 tab_text (t, 3, i, TAB_LEFT | TAT_FIX,
810 fmt_to_string (&spec->input));
814 tab_title (t, 1, ngettext ("Reading %d record from file %s.",
815 "Reading %d records from file %s.", rec_cnt),
816 rec_cnt, handle_get_filename (fh));
818 tab_title (t, 1, ngettext ("Reading %d record from the command file.",
819 "Reading %d records from the command file.",
825 /* Free-format parsing. */
827 /* Parses variable specifications for DATA LIST FREE and adds
828 them to the linked list with head FIRST and tail LAST.
829 Returns nonzero only if successful. */
831 parse_free (struct dls_var_spec **first, struct dls_var_spec **last)
836 struct fmt_spec input, output;
842 if (!parse_DATA_LIST_vars (&name, &name_cnt, PV_NONE))
846 if (!parse_format_specifier (&input, 0)
847 || !check_input_specifier (&input, 1)
848 || !lex_force_match (')'))
850 for (i = 0; i < name_cnt; i++)
855 convert_fmt_ItoO (&input, &output);
863 output = get_format();
866 if (input.type == FMT_A || input.type == FMT_AHEX)
870 for (i = 0; i < name_cnt; i++)
872 struct dls_var_spec *spec;
875 v = dict_create_var (default_dict, name[i], width);
878 msg (SE, _("%s is a duplicate variable name."), name[i]);
881 v->print = v->write = output;
883 if (!case_source_is_complex (vfm_source))
886 spec = xmalloc (sizeof *spec);
890 strcpy (spec->name, name[i]);
891 append_var_spec (first, last, spec);
893 for (i = 0; i < name_cnt; i++)
899 lex_error (_("expecting end of command"));
903 /* Displays a table giving information on free-format variable parsing
906 dump_free_table (const struct data_list_pgm *dls,
907 const struct file_handle *fh)
913 struct dls_var_spec *spec;
914 for (i = 0, spec = dls->first; spec; spec = spec->next)
918 t = tab_create (2, i + 1, 0);
919 tab_columns (t, TAB_COL_DOWN, 1);
920 tab_headers (t, 0, 0, 1, 0);
921 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Variable"));
922 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Format"));
923 tab_box (t, TAL_1, TAL_1, TAL_0, TAL_1, 0, 0, 1, i);
924 tab_hline (t, TAL_2, 0, 1, 1);
925 tab_dim (t, tab_natural_dimensions);
928 struct dls_var_spec *spec;
930 for (i = 1, spec = dls->first; spec; spec = spec->next, i++)
932 tab_text (t, 0, i, TAB_LEFT, spec->v->name);
933 tab_text (t, 1, i, TAB_LEFT | TAT_FIX, fmt_to_string (&spec->input));
938 tab_title (t, 1, _("Reading free-form data from file %s."),
939 handle_get_filename (fh));
941 tab_title (t, 1, _("Reading free-form data from the command file."));
946 /* Input procedure. */
948 /* Extracts a field from the current position in the current
949 record. Fields can be unquoted or quoted with single- or
950 double-quote characters. *FIELD is set to the field content.
951 After parsing the field, sets the current position in the
952 record to just past the field and any trailing delimiter.
953 END_BLANK is used internally; it should be initialized by the
954 caller to 0 and left alone afterward. Returns 0 on failure or
955 a 1-based column number indicating the beginning of the field
958 cut_field (const struct data_list_pgm *dls, struct fixed_string *field,
961 struct fixed_string line;
965 if (dfm_eof (dls->reader))
967 if (dls->delim_cnt == 0)
968 dfm_expand_tabs (dls->reader);
969 dfm_get_record (dls->reader, &line);
971 cp = ls_c_str (&line);
972 if (dls->delim_cnt == 0)
974 /* Skip leading whitespace. */
975 while (cp < ls_end (&line) && isspace ((unsigned char) *cp))
977 if (cp >= ls_end (&line))
980 /* Handle actual data, whether quoted or unquoted. */
981 if (*cp == '\'' || *cp == '"')
985 field->string = ++cp;
986 while (cp < ls_end (&line) && *cp != quote)
988 field->length = cp - field->string;
989 if (cp < ls_end (&line))
992 msg (SW, _("Quoted string missing terminating `%c'."), quote);
997 while (cp < ls_end (&line)
998 && !isspace ((unsigned char) *cp) && *cp != ',')
1000 field->length = cp - field->string;
1003 /* Skip trailing whitespace and a single comma if present. */
1004 while (cp < ls_end (&line) && isspace ((unsigned char) *cp))
1006 if (cp < ls_end (&line) && *cp == ',')
1011 if (cp >= ls_end (&line))
1013 int column = dfm_column_start (dls->reader);
1014 /* A blank line or a line that ends in \t has a
1015 trailing blank field. */
1016 if (column == 1 || (column > 1 && cp[-1] == '\t'))
1018 if (*end_blank == 0)
1021 field->string = ls_end (&line);
1023 dfm_forward_record (dls->reader);
1038 while (cp < ls_end (&line)
1039 && memchr (dls->delims, *cp, dls->delim_cnt) == NULL)
1041 field->length = cp - field->string;
1042 if (cp < ls_end (&line))
1047 dfm_forward_columns (dls->reader, field->string - line.string);
1048 column_start = dfm_column_start (dls->reader);
1050 dfm_forward_columns (dls->reader, cp - field->string);
1052 return column_start;
1055 typedef int data_list_read_func (const struct data_list_pgm *, struct ccase *);
1056 static data_list_read_func read_from_data_list_fixed;
1057 static data_list_read_func read_from_data_list_free;
1058 static data_list_read_func read_from_data_list_list;
1060 /* Returns the proper function to read the kind of DATA LIST
1061 data specified by DLS. */
1062 static data_list_read_func *
1063 get_data_list_read_func (const struct data_list_pgm *dls)
1068 return read_from_data_list_fixed;
1071 return read_from_data_list_free;
1074 return read_from_data_list_list;
1082 /* Reads a case from the data file into C, parsing it according
1083 to fixed-format syntax rules in DLS. Returns -1 on success,
1084 -2 at end of file. */
1086 read_from_data_list_fixed (const struct data_list_pgm *dls,
1089 struct dls_var_spec *var_spec = dls->first;
1092 if (dfm_eof (dls->reader))
1094 for (i = 1; i <= dls->rec_cnt; i++)
1096 struct fixed_string line;
1098 if (dfm_eof (dls->reader))
1100 /* Note that this can't occur on the first record. */
1101 msg (SW, _("Partial case of %d of %d records discarded."),
1102 i - 1, dls->rec_cnt);
1105 dfm_expand_tabs (dls->reader);
1106 dfm_get_record (dls->reader, &line);
1108 for (; var_spec && i == var_spec->rec; var_spec = var_spec->next)
1112 data_in_finite_line (&di, ls_c_str (&line), ls_length (&line),
1113 var_spec->fc, var_spec->lc);
1114 di.v = case_data_rw (c, var_spec->fv);
1115 di.flags = DI_IMPLIED_DECIMALS;
1116 di.f1 = var_spec->fc;
1117 di.format = var_spec->input;
1122 dfm_forward_record (dls->reader);
1128 /* Reads a case from the data file into C, parsing it according
1129 to free-format syntax rules in DLS. Returns -1 on success,
1130 -2 at end of file. */
1132 read_from_data_list_free (const struct data_list_pgm *dls,
1135 struct dls_var_spec *var_spec;
1138 for (var_spec = dls->first; var_spec; var_spec = var_spec->next)
1140 struct fixed_string field;
1143 /* Cut out a field and read in a new record if necessary. */
1146 column = cut_field (dls, &field, &end_blank);
1150 if (!dfm_eof (dls->reader))
1151 dfm_forward_record (dls->reader);
1152 if (dfm_eof (dls->reader))
1154 if (var_spec != dls->first)
1155 msg (SW, _("Partial case discarded. The first variable "
1156 "missing was %s."), var_spec->name);
1164 di.s = ls_c_str (&field);
1165 di.e = ls_end (&field);
1166 di.v = case_data_rw (c, var_spec->fv);
1169 di.format = var_spec->input;
1176 /* Reads a case from the data file and parses it according to
1177 list-format syntax rules. Returns -1 on success, -2 at end of
1180 read_from_data_list_list (const struct data_list_pgm *dls,
1183 struct dls_var_spec *var_spec;
1186 if (dfm_eof (dls->reader))
1189 for (var_spec = dls->first; var_spec; var_spec = var_spec->next)
1191 struct fixed_string field;
1194 /* Cut out a field and check for end-of-line. */
1195 column = cut_field (dls, &field, &end_blank);
1198 if (get_undefined ())
1199 msg (SW, _("Missing value(s) for all variables from %s onward. "
1200 "These will be filled with the system-missing value "
1201 "or blanks, as appropriate."),
1203 for (; var_spec; var_spec = var_spec->next)
1205 int width = get_format_var_width (&var_spec->input);
1207 case_data_rw (c, var_spec->fv)->f = SYSMIS;
1209 memset (case_data_rw (c, var_spec->fv)->s, ' ', width);
1217 di.s = ls_c_str (&field);
1218 di.e = ls_end (&field);
1219 di.v = case_data_rw (c, var_spec->fv);
1222 di.format = var_spec->input;
1227 dfm_forward_record (dls->reader);
1231 /* Destroys SPEC. */
1233 destroy_dls_var_spec (struct dls_var_spec *spec)
1235 struct dls_var_spec *next;
1237 while (spec != NULL)
1245 /* Destroys DATA LIST transformation PGM. */
1247 data_list_trns_free (struct trns_header *pgm)
1249 struct data_list_pgm *dls = (struct data_list_pgm *) pgm;
1251 destroy_dls_var_spec (dls->first);
1252 dfm_close_reader (dls->reader);
1256 /* Handle DATA LIST transformation T, parsing data into C. */
1258 data_list_trns_proc (struct trns_header *t, struct ccase *c,
1259 int case_num UNUSED)
1261 struct data_list_pgm *dls = (struct data_list_pgm *) t;
1262 data_list_read_func *read_func;
1265 dfm_push (dls->reader);
1267 read_func = get_data_list_read_func (dls);
1268 retval = read_func (dls, c);
1270 /* Handle end of file. */
1273 /* If we already encountered end of file then this is an
1277 msg (SE, _("Attempt to read past end of file."));
1279 dfm_pop (dls->reader);
1283 /* Otherwise simply note it. */
1289 /* If there was an END subcommand handle it. */
1290 if (dls->end != NULL)
1294 case_data_rw (c, dls->end->fv)->f = 1.0;
1298 case_data_rw (c, dls->end->fv)->f = 0.0;
1301 dfm_pop (dls->reader);
1306 /* Reads all the records from the data file and passes them to
1309 data_list_source_read (struct case_source *source,
1311 write_case_func *write_case, write_case_data wc_data)
1313 struct data_list_pgm *dls = source->aux;
1314 data_list_read_func *read_func = get_data_list_read_func (dls);
1316 dfm_push (dls->reader);
1317 while (read_func (dls, c) != -2)
1318 if (!write_case (wc_data))
1320 dfm_pop (dls->reader);
1323 /* Destroys the source's internal data. */
1325 data_list_source_destroy (struct case_source *source)
1327 data_list_trns_free (source->aux);
1330 const struct case_source_class data_list_source_class =
1334 data_list_source_read,
1335 data_list_source_destroy,
1338 /* REPEATING DATA. */
1340 /* Represents a number or a variable. */
1341 struct rpd_num_or_var
1343 int num; /* Value, or 0. */
1344 struct variable *var; /* Variable, if number==0. */
1347 /* REPEATING DATA private data structure. */
1348 struct repeating_data_trns
1350 struct trns_header h;
1351 struct dls_var_spec *first, *last; /* Variable parsing specifications. */
1352 struct dfm_reader *reader; /* Input file, never NULL. */
1354 struct rpd_num_or_var starts_beg; /* STARTS=, before the dash. */
1355 struct rpd_num_or_var starts_end; /* STARTS=, after the dash. */
1356 struct rpd_num_or_var occurs; /* OCCURS= subcommand. */
1357 struct rpd_num_or_var length; /* LENGTH= subcommand. */
1358 struct rpd_num_or_var cont_beg; /* CONTINUED=, before the dash. */
1359 struct rpd_num_or_var cont_end; /* CONTINUED=, after the dash. */
1361 /* ID subcommand. */
1362 int id_beg, id_end; /* Beginning & end columns. */
1363 struct variable *id_var; /* DATA LIST variable. */
1364 struct fmt_spec id_spec; /* Input format spec. */
1365 union value *id_value; /* ID value. */
1367 write_case_func *write_case;
1368 write_case_data wc_data;
1371 static trns_free_func repeating_data_trns_free;
1372 static int parse_num_or_var (struct rpd_num_or_var *, const char *);
1373 static int parse_repeating_data (struct dls_var_spec **,
1374 struct dls_var_spec **);
1375 static void find_variable_input_spec (struct variable *v,
1376 struct fmt_spec *spec);
1378 /* Parses the REPEATING DATA command. */
1380 cmd_repeating_data (void)
1382 struct repeating_data_trns *rpd;
1383 int table = 1; /* Print table? */
1384 unsigned seen = 0; /* Mark subcommands as already seen. */
1385 struct file_handle *const fh = default_handle;
1387 assert (case_source_is_complex (vfm_source));
1389 rpd = xmalloc (sizeof *rpd);
1390 rpd->reader = dfm_open_reader (default_handle);
1391 rpd->first = rpd->last = NULL;
1392 rpd->starts_beg.num = 0;
1393 rpd->starts_beg.var = NULL;
1394 rpd->starts_end = rpd->occurs = rpd->length = rpd->cont_beg
1395 = rpd->cont_end = rpd->starts_beg;
1396 rpd->id_beg = rpd->id_end = 0;
1398 rpd->id_value = NULL;
1404 if (lex_match_id ("FILE"))
1406 struct file_handle *file;
1413 msg (SE, _("REPEATING DATA must use the same file as its "
1414 "corresponding DATA LIST or FILE TYPE."));
1418 else if (lex_match_id ("STARTS"))
1423 msg (SE, _("%s subcommand given multiple times."),"STARTS");
1428 if (!parse_num_or_var (&rpd->starts_beg, "STARTS beginning column"))
1431 lex_negative_to_dash ();
1432 if (lex_match ('-'))
1434 if (!parse_num_or_var (&rpd->starts_end, "STARTS ending column"))
1437 /* Otherwise, rpd->starts_end is left uninitialized.
1438 This is okay. We will initialize it later from the
1439 record length of the file. We can't do this now
1440 because we can't be sure that the user has specified
1441 the file handle yet. */
1444 if (rpd->starts_beg.num != 0 && rpd->starts_end.num != 0
1445 && rpd->starts_beg.num > rpd->starts_end.num)
1447 msg (SE, _("STARTS beginning column (%d) exceeds "
1448 "STARTS ending column (%d)."),
1449 rpd->starts_beg.num, rpd->starts_end.num);
1453 else if (lex_match_id ("OCCURS"))
1458 msg (SE, _("%s subcommand given multiple times."),"OCCURS");
1463 if (!parse_num_or_var (&rpd->occurs, "OCCURS"))
1466 else if (lex_match_id ("LENGTH"))
1471 msg (SE, _("%s subcommand given multiple times."),"LENGTH");
1476 if (!parse_num_or_var (&rpd->length, "LENGTH"))
1479 else if (lex_match_id ("CONTINUED"))
1484 msg (SE, _("%s subcommand given multiple times."),"CONTINUED");
1489 if (!lex_match ('/'))
1491 if (!parse_num_or_var (&rpd->cont_beg, "CONTINUED beginning column"))
1494 lex_negative_to_dash ();
1496 && !parse_num_or_var (&rpd->cont_end,
1497 "CONTINUED ending column"))
1500 if (rpd->cont_beg.num != 0 && rpd->cont_end.num != 0
1501 && rpd->cont_beg.num > rpd->cont_end.num)
1503 msg (SE, _("CONTINUED beginning column (%d) exceeds "
1504 "CONTINUED ending column (%d)."),
1505 rpd->cont_beg.num, rpd->cont_end.num);
1510 rpd->cont_beg.num = 1;
1512 else if (lex_match_id ("ID"))
1517 msg (SE, _("%s subcommand given multiple times."),"ID");
1522 if (!lex_force_int ())
1524 if (lex_integer () < 1)
1526 msg (SE, _("ID beginning column (%ld) must be positive."),
1530 rpd->id_beg = lex_integer ();
1533 lex_negative_to_dash ();
1535 if (lex_match ('-'))
1537 if (!lex_force_int ())
1539 if (lex_integer () < 1)
1541 msg (SE, _("ID ending column (%ld) must be positive."),
1545 if (lex_integer () < rpd->id_end)
1547 msg (SE, _("ID ending column (%ld) cannot be less than "
1548 "ID beginning column (%d)."),
1549 lex_integer (), rpd->id_beg);
1553 rpd->id_end = lex_integer ();
1556 else rpd->id_end = rpd->id_beg;
1558 if (!lex_force_match ('='))
1560 rpd->id_var = parse_variable ();
1561 if (rpd->id_var == NULL)
1564 find_variable_input_spec (rpd->id_var, &rpd->id_spec);
1565 rpd->id_value = xmalloc (sizeof *rpd->id_value * rpd->id_var->nv);
1567 else if (lex_match_id ("TABLE"))
1569 else if (lex_match_id ("NOTABLE"))
1571 else if (lex_match_id ("DATA"))
1579 if (!lex_force_match ('/'))
1583 /* Comes here when DATA specification encountered. */
1584 if ((seen & (1 | 2)) != (1 | 2))
1586 if ((seen & 1) == 0)
1587 msg (SE, _("Missing required specification STARTS."));
1588 if ((seen & 2) == 0)
1589 msg (SE, _("Missing required specification OCCURS."));
1593 /* Enforce ID restriction. */
1594 if ((seen & 16) && !(seen & 8))
1596 msg (SE, _("ID specified without CONTINUED."));
1600 /* Calculate starts_end, cont_end if necessary. */
1601 if (rpd->starts_end.num == 0 && rpd->starts_end.var == NULL)
1602 rpd->starts_end.num = handle_get_record_width (fh);
1603 if (rpd->cont_end.num == 0 && rpd->starts_end.var == NULL)
1604 rpd->cont_end.num = handle_get_record_width (fh);
1606 /* Calculate length if possible. */
1607 if ((seen & 4) == 0)
1609 struct dls_var_spec *iter;
1611 for (iter = rpd->first; iter; iter = iter->next)
1613 if (iter->lc > rpd->length.num)
1614 rpd->length.num = iter->lc;
1616 assert (rpd->length.num != 0);
1620 if (!parse_repeating_data (&rpd->first, &rpd->last))
1624 dump_fixed_table (rpd->first, fh, rpd->last->rec);
1627 struct repeating_data_trns *new_trns;
1629 rpd->h.proc = repeating_data_trns_proc;
1630 rpd->h.free = repeating_data_trns_free;
1632 new_trns = xmalloc (sizeof *new_trns);
1633 memcpy (new_trns, &rpd, sizeof *new_trns);
1634 add_transformation ((struct trns_header *) new_trns);
1637 return lex_end_of_command ();
1640 destroy_dls_var_spec (rpd->first);
1641 free (rpd->id_value);
1645 /* Finds the input format specification for variable V and puts
1646 it in SPEC. Because of the way that DATA LIST is structured,
1647 this is nontrivial. */
1649 find_variable_input_spec (struct variable *v, struct fmt_spec *spec)
1653 for (i = 0; i < n_trns; i++)
1655 struct data_list_pgm *pgm = (struct data_list_pgm *) t_trns[i];
1657 if (pgm->h.proc == data_list_trns_proc)
1659 struct dls_var_spec *iter;
1661 for (iter = pgm->first; iter; iter = iter->next)
1664 *spec = iter->input;
1673 /* Parses a number or a variable name from the syntax file and puts
1674 the results in VALUE. Ensures that the number is at least 1; else
1675 emits an error based on MESSAGE. Returns nonzero only if
1678 parse_num_or_var (struct rpd_num_or_var *value, const char *message)
1683 value->var = parse_variable ();
1684 if (value->var == NULL)
1686 if (value->var->type == ALPHA)
1688 msg (SE, _("String variable not allowed here."));
1692 else if (lex_is_integer ())
1694 value->num = lex_integer ();
1698 msg (SE, _("%s (%d) must be at least 1."), message, value->num);
1704 msg (SE, _("Variable or integer expected for %s."), message);
1710 /* Parses data specifications for repeating data groups, adding
1711 them to the linked list with head FIRST and tail LAST.
1712 Returns nonzero only if successful. */
1714 parse_repeating_data (struct dls_var_spec **first, struct dls_var_spec **last)
1716 struct fixed_parsing_state fx;
1722 while (token != '.')
1724 if (!parse_DATA_LIST_vars (&fx.name, &fx.name_cnt, PV_NONE))
1727 if (lex_is_number ())
1729 if (!fixed_parse_compatible (&fx, first, last))
1732 else if (token == '(')
1734 if (!fixed_parse_fortran (&fx, first, last))
1739 msg (SE, _("SPSS-like or FORTRAN-like format "
1740 "specification expected after variable names."));
1744 for (i = 0; i < fx.name_cnt; i++)
1750 lex_error (_("expecting end of command"));
1757 for (i = 0; i < fx.name_cnt; i++)
1763 /* Obtains the real value for rpd_num_or_var N in case C and returns
1764 it. The valid range is nonnegative numbers, but numbers outside
1765 this range can be returned and should be handled by the caller as
1768 realize_value (struct rpd_num_or_var *n, struct ccase *c)
1773 assert (n->num == 0);
1776 double v = case_num (c, n->var->fv);
1778 if (v == SYSMIS || v <= INT_MIN || v >= INT_MAX)
1787 /* Parameter record passed to rpd_parse_record(). */
1788 struct rpd_parse_info
1790 struct repeating_data_trns *trns; /* REPEATING DATA transformation. */
1791 const char *line; /* Line being parsed. */
1792 size_t len; /* Line length. */
1793 int beg, end; /* First and last column of first occurrence. */
1794 int ofs; /* Column offset between repeated occurrences. */
1795 struct ccase *c; /* Case to fill in. */
1796 int verify_id; /* Zero to initialize ID, nonzero to verify it. */
1797 int max_occurs; /* Max number of occurrences to parse. */
1800 /* Parses one record of repeated data and outputs corresponding
1801 cases. Returns number of occurrences parsed up to the
1802 maximum specified in INFO. */
1804 rpd_parse_record (const struct rpd_parse_info *info)
1806 struct repeating_data_trns *t = info->trns;
1807 int cur = info->beg;
1810 /* Handle record ID values. */
1813 union value id_temp[MAX_ELEMS_PER_VALUE];
1815 /* Parse record ID into V. */
1819 data_in_finite_line (&di, info->line, info->len, t->id_beg, t->id_end);
1820 di.v = info->verify_id ? id_temp : t->id_value;
1823 di.format = t->id_spec;
1830 && compare_values (id_temp, t->id_value, t->id_var->width) != 0)
1832 char expected_str [MAX_FORMATTED_LEN + 1];
1833 char actual_str [MAX_FORMATTED_LEN + 1];
1835 data_out (expected_str, &t->id_var->print, t->id_value);
1836 expected_str[t->id_var->print.w] = '\0';
1838 data_out (actual_str, &t->id_var->print, id_temp);
1839 actual_str[t->id_var->print.w] = '\0';
1842 _("Encountered mismatched record ID \"%s\" expecting \"%s\"."),
1843 actual_str, expected_str);
1849 /* Iterate over the set of expected occurrences and record each of
1850 them as a separate case. FIXME: We need to execute any
1851 transformations that follow the current one. */
1855 for (occurrences = 0; occurrences < info->max_occurs; )
1857 if (cur + info->ofs > info->end + 1)
1862 struct dls_var_spec *var_spec = t->first;
1864 for (; var_spec; var_spec = var_spec->next)
1866 int fc = var_spec->fc - 1 + cur;
1867 int lc = var_spec->lc - 1 + cur;
1869 if (fc > info->len && !warned && var_spec->input.type != FMT_A)
1874 _("Variable %s starting in column %d extends "
1875 "beyond physical record length of %d."),
1876 var_spec->v->name, fc, info->len);
1882 data_in_finite_line (&di, info->line, info->len, fc, lc);
1883 di.v = case_data_rw (info->c, var_spec->fv);
1886 di.format = var_spec->input;
1896 if (!t->write_case (t->wc_data))
1904 /* Reads one set of repetitions of the elements in the REPEATING
1905 DATA structure. Returns -1 on success, -2 on end of file or
1908 repeating_data_trns_proc (struct trns_header *trns, struct ccase *c,
1909 int case_num UNUSED)
1911 struct repeating_data_trns *t = (struct repeating_data_trns *) trns;
1913 struct fixed_string line; /* Current record. */
1915 int starts_beg; /* Starting column. */
1916 int starts_end; /* Ending column. */
1917 int occurs; /* Number of repetitions. */
1918 int length; /* Length of each occurrence. */
1919 int cont_beg; /* Starting column for continuation lines. */
1920 int cont_end; /* Ending column for continuation lines. */
1922 int occurs_left; /* Number of occurrences remaining. */
1924 int code; /* Return value from rpd_parse_record(). */
1926 int skip_first_record = 0;
1928 dfm_push (t->reader);
1930 /* Read the current record. */
1931 dfm_reread_record (t->reader, 1);
1932 dfm_expand_tabs (t->reader);
1933 if (dfm_eof (t->reader))
1935 dfm_get_record (t->reader, &line);
1936 dfm_forward_record (t->reader);
1938 /* Calculate occurs, length. */
1939 occurs_left = occurs = realize_value (&t->occurs, c);
1942 tmsg (SE, RPD_ERR, _("Invalid value %d for OCCURS."), occurs);
1945 starts_beg = realize_value (&t->starts_beg, c);
1946 if (starts_beg <= 0)
1948 tmsg (SE, RPD_ERR, _("Beginning column for STARTS (%d) must be "
1953 starts_end = realize_value (&t->starts_end, c);
1954 if (starts_end < starts_beg)
1956 tmsg (SE, RPD_ERR, _("Ending column for STARTS (%d) is less than "
1957 "beginning column (%d)."),
1958 starts_end, starts_beg);
1959 skip_first_record = 1;
1961 length = realize_value (&t->length, c);
1964 tmsg (SE, RPD_ERR, _("Invalid value %d for LENGTH."), length);
1966 occurs = occurs_left = 1;
1968 cont_beg = realize_value (&t->cont_beg, c);
1971 tmsg (SE, RPD_ERR, _("Beginning column for CONTINUED (%d) must be "
1976 cont_end = realize_value (&t->cont_end, c);
1977 if (cont_end < cont_beg)
1979 tmsg (SE, RPD_ERR, _("Ending column for CONTINUED (%d) is less than "
1980 "beginning column (%d)."),
1981 cont_end, cont_beg);
1985 /* Parse the first record. */
1986 if (!skip_first_record)
1988 struct rpd_parse_info info;
1990 info.line = ls_c_str (&line);
1991 info.len = ls_length (&line);
1992 info.beg = starts_beg;
1993 info.end = starts_end;
1997 info.max_occurs = occurs_left;
1998 code = rpd_parse_record (&info);
2001 occurs_left -= code;
2003 else if (cont_beg == 0)
2006 /* Make sure, if some occurrences are left, that we have
2007 continuation records. */
2008 if (occurs_left > 0 && cont_beg == 0)
2011 _("Number of repetitions specified on OCCURS (%d) "
2012 "exceed number of repetitions available in "
2013 "space on STARTS (%d), and CONTINUED not specified."),
2014 occurs, (starts_end - starts_beg + 1) / length);
2018 /* Go on to additional records. */
2019 while (occurs_left != 0)
2021 struct rpd_parse_info info;
2023 assert (occurs_left >= 0);
2025 /* Read in another record. */
2026 if (dfm_eof (t->reader))
2029 _("Unexpected end of file with %d repetitions "
2030 "remaining out of %d."),
2031 occurs_left, occurs);
2034 dfm_expand_tabs (t->reader);
2035 dfm_get_record (t->reader, &line);
2036 dfm_forward_record (t->reader);
2038 /* Parse this record. */
2040 info.line = ls_c_str (&line);
2041 info.len = ls_length (&line);
2042 info.beg = cont_beg;
2043 info.end = cont_end;
2047 info.max_occurs = occurs_left;
2048 code = rpd_parse_record (&info);;
2051 occurs_left -= code;
2054 dfm_pop (t->reader);
2056 /* FIXME: This is a kluge until we've implemented multiplexing of
2061 /* Frees a REPEATING DATA transformation. */
2063 repeating_data_trns_free (struct trns_header *rpd_)
2065 struct repeating_data_trns *rpd = (struct repeating_data_trns *) rpd_;
2067 destroy_dls_var_spec (rpd->first);
2068 dfm_close_reader (rpd->reader);
2069 free (rpd->id_value);
2072 /* Lets repeating_data_trns_proc() know how to write the cases
2073 that it composes. Not elegant. */
2075 repeating_data_set_write_case (struct trns_header *trns,
2076 write_case_func *write_case,
2077 write_case_data wc_data)
2079 struct repeating_data_trns *t = (struct repeating_data_trns *) trns;
2081 assert (trns->proc == repeating_data_trns_proc);
2082 t->write_case = write_case;
2083 t->wc_data = wc_data;