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., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <language/data-io/file-type.h>
26 #include <data/case-source.h>
27 #include <data/case.h>
28 #include <data/data-in.h>
29 #include <data/dictionary.h>
30 #include <data/format.h>
31 #include <data/variable.h>
32 #include <language/command.h>
33 #include <language/data-io/data-reader.h>
34 #include <language/data-io/file-handle.h>
35 #include <language/lexer/lexer.h>
36 #include <libpspp/alloc.h>
37 #include <libpspp/compiler.h>
38 #include <libpspp/message.h>
39 #include <libpspp/str.h>
40 #include <procedure.h>
43 #define _(msgid) gettext (msgid)
45 /* Defines the three types of complex files read by FILE TYPE. */
53 /* Limited variable column specifications. */
56 char name[LONG_NAME_LEN + 1]; /* Variable name. */
57 int fc, nc; /* First column (1-based), # of columns. */
58 int fmt; /* Format type. */
59 struct variable *v; /* Variable. */
62 /* RCT_* record type constants. */
65 RCT_OTHER = 001, /* 1=OTHER. */
66 RCT_SKIP = 002, /* 1=SKIP. */
67 RCT_DUPLICATE = 004, /* DUPLICATE: 0=NOWARN, 1=WARN. */
68 RCT_MISSING = 010, /* MISSING: 0=NOWARN, 1=WARN. */
69 RCT_SPREAD = 020 /* SPREAD: 0=NO, 1=YES. */
72 /* Represents a RECORD TYPE command. */
75 struct record_type *next;
76 unsigned flags; /* RCT_* constants. */
77 union value *v; /* Vector of values for this record type. */
78 int nv; /* Length of vector V. */
79 struct col_spec case_sbc; /* CASE subcommand. */
80 int ft, lt; /* First, last transformation index. */
83 /* Represents a FILE TYPE input program. */
86 int type; /* One of the FTY_* constants. */
87 struct dfm_reader *reader; /* Data file to read. */
88 struct col_spec record; /* RECORD subcommand. */
89 struct col_spec case_sbc; /* CASE subcommand. */
90 int wild; /* 0=NOWARN, 1=WARN. */
91 int duplicate; /* 0=NOWARN, 1=WARN. */
92 int missing; /* 0=NOWARN, 1=WARN, 2=CASE. */
93 int ordered; /* 0=NO, 1=YES. */
94 int had_rec_type; /* 1=Had a RECORD TYPE command.
95 RECORD TYPE must precede the first
97 struct record_type *recs_head; /* List of record types. */
98 struct record_type *recs_tail; /* Last in list of record types. */
99 size_t case_size; /* Case size in bytes. */
102 static int parse_col_spec (struct col_spec *, const char *);
103 static void create_col_var (struct col_spec *c);
104 static const struct case_source_class file_type_source_class;
106 static bool inside_file_type;
108 /* Returns true if we're parsing the inside of a FILE TYPE...END
109 FILE TYPE construct, false otherwise. */
113 return inside_file_type;
116 int cmd_file_type (void);
118 /* Parses FILE TYPE command. */
122 static struct file_type_pgm *fty; /* FIXME: static? WTF? */
123 struct file_handle *fh = fh_inline_file ();
126 discard_variables ();
128 fty = xmalloc (sizeof *fty);
130 fty->record.name[0] = 0;
131 fty->case_sbc.name[0] = 0;
132 fty->wild = fty->duplicate = fty->missing = fty->ordered = 0;
133 fty->had_rec_type = 0;
134 fty->recs_head = fty->recs_tail = NULL;
136 if (lex_match_id ("MIXED"))
137 fty->type = FTY_MIXED;
138 else if (lex_match_id ("GROUPED"))
140 fty->type = FTY_GROUPED;
146 else if (lex_match_id ("NESTED"))
147 fty->type = FTY_NESTED;
150 msg (SE, _("MIXED, GROUPED, or NESTED expected."));
156 if (lex_match_id ("FILE"))
159 fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
163 else if (lex_match_id ("RECORD"))
166 if (!parse_col_spec (&fty->record, "####RECD"))
169 else if (lex_match_id ("CASE"))
171 if (fty->type == FTY_MIXED)
173 msg (SE, _("The CASE subcommand is not valid on FILE TYPE "
179 if (!parse_col_spec (&fty->case_sbc, "####CASE"))
182 else if (lex_match_id ("WILD"))
185 if (lex_match_id ("WARN"))
187 else if (lex_match_id ("NOWARN"))
191 msg (SE, _("WARN or NOWARN expected after WILD."));
195 else if (lex_match_id ("DUPLICATE"))
197 if (fty->type == FTY_MIXED)
199 msg (SE, _("The DUPLICATE subcommand is not valid on "
200 "FILE TYPE MIXED."));
205 if (lex_match_id ("WARN"))
207 else if (lex_match_id ("NOWARN"))
209 else if (lex_match_id ("CASE"))
211 if (fty->type != FTY_NESTED)
213 msg (SE, _("DUPLICATE=CASE is only valid on "
214 "FILE TYPE NESTED."));
222 msg (SE, _("WARN%s expected after DUPLICATE."),
223 (fty->type == FTY_NESTED ? _(", NOWARN, or CASE")
228 else if (lex_match_id ("MISSING"))
230 if (fty->type == FTY_MIXED)
232 msg (SE, _("The MISSING subcommand is not valid on "
233 "FILE TYPE MIXED."));
238 if (lex_match_id ("NOWARN"))
240 else if (lex_match_id ("WARN"))
244 msg (SE, _("WARN or NOWARN after MISSING."));
248 else if (lex_match_id ("ORDERED"))
250 if (fty->type != FTY_GROUPED)
252 msg (SE, _("ORDERED is only valid on FILE TYPE GROUPED."));
257 if (lex_match_id ("YES"))
259 else if (lex_match_id ("NO"))
263 msg (SE, _("YES or NO expected after ORDERED."));
269 lex_error (_("while expecting a valid subcommand"));
274 if (fty->record.name[0] == 0)
276 msg (SE, _("The required RECORD subcommand was not present."));
280 if (fty->type == FTY_GROUPED)
282 if (fty->case_sbc.name[0] == 0)
284 msg (SE, _("The required CASE subcommand was not present."));
288 if (!strcasecmp (fty->case_sbc.name, fty->record.name))
290 msg (SE, _("CASE and RECORD must specify different variable "
296 fty->reader = dfm_open_reader (fh);
297 if (fty->reader == NULL)
299 fh_set_default_handle (fh);
301 create_col_var (&fty->record);
302 if (fty->case_sbc.name[0])
303 create_col_var (&fty->case_sbc);
304 vfm_source = create_case_source (&file_type_source_class, fty);
310 return CMD_CASCADING_FAILURE;
313 /* Creates a variable with attributes specified by struct col_spec C, and
314 stores it into C->V. */
316 create_col_var (struct col_spec *c)
320 if (formats[c->fmt].cat & FCAT_STRING)
324 c->v = dict_create_var (default_dict, c->name, width);
327 /* Parses variable, column, type specifications for a variable. */
329 parse_col_spec (struct col_spec *c, const char *def_name)
331 struct fmt_spec spec;
336 strcpy (c->name, tokid);
340 strcpy (c->name, def_name);
343 if (!lex_force_int ())
345 c->fc = lex_integer ();
348 msg (SE, _("Column value must be positive."));
354 lex_negative_to_dash ();
357 if (!lex_force_int ())
359 c->nc = lex_integer ();
364 msg (SE, _("Ending column precedes beginning column."));
373 /* Format specifier. */
377 if (!lex_force_id ())
379 c->fmt = parse_format_specifier_name (&cp, 0);
384 msg (SE, _("Bad format specifier name."));
388 if (!lex_force_match (')'))
397 return check_input_specifier (&spec, 1);
402 /* Parse the RECORD TYPE command. */
404 cmd_record_type (void)
406 struct file_type_pgm *fty;
407 struct record_type *rct;
409 fty = vfm_source->aux;
411 /* Initialize the record_type structure. */
412 rct = xmalloc (sizeof *rct);
416 rct->flags |= RCT_DUPLICATE;
418 rct->flags |= RCT_MISSING;
422 if (fty->case_sbc.name[0])
423 rct->case_sbc = fty->case_sbc;
425 if (fty->recs_tail && (fty->recs_tail->flags & RCT_OTHER))
427 msg (SE, _("OTHER may appear only on the last RECORD TYPE command."));
433 fty->recs_tail->lt = n_trns - 1;
434 if (!(fty->recs_tail->flags & RCT_SKIP)
435 && fty->recs_tail->ft == fty->recs_tail->lt)
437 msg (SE, _("No input commands (DATA LIST, REPEATING DATA) "
438 "for above RECORD TYPE."));
443 /* Parse record type values. */
444 if (lex_match_id ("OTHER"))
445 rct->flags |= RCT_OTHER;
450 while (lex_is_number () || token == T_STRING)
455 rct->v = xnrealloc (rct->v, mv, sizeof *rct->v);
458 if (formats[fty->record.fmt].cat & FCAT_STRING)
460 if (!lex_force_string ())
462 rct->v[rct->nv].c = xmalloc (fty->record.nc + 1);
463 buf_copy_str_rpad (rct->v[rct->nv].c, fty->record.nc + 1,
468 if (!lex_force_num ())
470 rct->v[rct->nv].f = tokval;
479 /* Parse the rest of the subcommands. */
482 if (lex_match_id ("SKIP"))
483 rct->flags |= RCT_SKIP;
484 else if (lex_match_id ("CASE"))
486 if (fty->type == FTY_MIXED)
488 msg (SE, _("The CASE subcommand is not allowed on "
489 "the RECORD TYPE command for FILE TYPE MIXED."));
494 if (!parse_col_spec (&rct->case_sbc, ""))
496 if (rct->case_sbc.name[0])
498 msg (SE, _("No variable name may be specified for the "
499 "CASE subcommand on RECORD TYPE."));
503 if ((formats[rct->case_sbc.fmt].cat ^ formats[fty->case_sbc.fmt].cat)
506 msg (SE, _("The CASE column specification on RECORD TYPE "
507 "must give a format specifier that is the "
508 "same type as that of the CASE column "
509 "specification given on FILE TYPE."));
513 else if (lex_match_id ("DUPLICATE"))
516 if (lex_match_id ("WARN"))
517 rct->flags |= RCT_DUPLICATE;
518 else if (lex_match_id ("NOWARN"))
519 rct->flags &= ~RCT_DUPLICATE;
522 msg (SE, _("WARN or NOWARN expected on DUPLICATE "
527 else if (lex_match_id ("MISSING"))
530 if (lex_match_id ("WARN"))
531 rct->flags |= RCT_MISSING;
532 else if (lex_match_id ("NOWARN"))
533 rct->flags &= ~RCT_MISSING;
536 msg (SE, _("WARN or NOWARN expected on MISSING subcommand."));
540 else if (lex_match_id ("SPREAD"))
543 if (lex_match_id ("YES"))
544 rct->flags |= RCT_SPREAD;
545 else if (lex_match_id ("NO"))
546 rct->flags &= ~RCT_SPREAD;
549 msg (SE, _("YES or NO expected on SPREAD subcommand."));
555 lex_error (_("while expecting a valid subcommand"));
561 fty->recs_tail = fty->recs_tail->next = xmalloc (sizeof *fty->recs_tail);
563 fty->recs_head = fty->recs_tail = xmalloc (sizeof *fty->recs_tail);
564 memcpy (fty->recs_tail, &rct, sizeof *fty->recs_tail);
569 if (formats[fty->record.fmt].cat & FCAT_STRING)
573 for (i = 0; i < rct->nv; i++)
579 return CMD_CASCADING_FAILURE;
584 int cmd_end_file_type (void);
586 cmd_end_file_type (void)
588 struct file_type_pgm *fty;
590 fty = vfm_source->aux;
591 fty->case_size = dict_get_case_size (default_dict);
595 fty->recs_tail->lt = n_trns - 1;
596 if (!(fty->recs_tail->flags & RCT_SKIP)
597 && fty->recs_tail->ft == fty->recs_tail->lt)
599 msg (SE, _("No input commands (DATA LIST, REPEATING DATA) "
600 "on above RECORD TYPE."));
606 msg (SE, _("No commands between FILE TYPE and END FILE TYPE."));
612 return lex_end_of_command ();
615 /* Come here on I/O error. */
616 discard_variables ();
617 return CMD_CASCADING_FAILURE;
620 /* FILE TYPE runtime. */
622 /*static void read_from_file_type_mixed(void);
623 static void read_from_file_type_grouped(void);
624 static void read_from_file_type_nested(void); */
626 /* Reads any number of cases into case C and calls write_case()
627 for each one. Compare data-list.c:read_from_data_list.
628 Returns true if successful, false if an I/O error occurred. */
630 file_type_source_read (struct case_source *source,
632 write_case_func *write_case UNUSED,
633 write_case_data wc_data UNUSED)
635 struct file_type_pgm *fty = source->aux;
636 struct fmt_spec format;
638 dfm_push (fty->reader);
640 format.type = fty->record.fmt;
641 format.w = fty->record.nc;
643 while (!dfm_eof (fty->reader))
645 struct fixed_string line;
646 struct record_type *iter;
650 dfm_expand_tabs (fty->reader);
651 dfm_get_record (fty->reader, &line);
652 if (formats[fty->record.fmt].cat & FCAT_STRING)
656 v.c = case_data_rw (c, fty->record.v->fv)->s;
658 data_in_finite_line (&di, ls_c_str (&line), ls_length (&line),
659 fty->record.fc, fty->record.fc + fty->record.nc);
660 di.v = (union value *) v.c;
662 di.f1 = fty->record.fc;
666 for (iter = fty->recs_head; iter; iter = iter->next)
668 if (iter->flags & RCT_OTHER)
670 for (i = 0; i < iter->nv; i++)
671 if (!memcmp (iter->v[i].c, v.c, fty->record.nc))
675 msg (SW, _("Unknown record type \"%.*s\"."), fty->record.nc, v.c);
681 data_in_finite_line (&di, ls_c_str (&line), ls_length (&line),
682 fty->record.fc, fty->record.fc + fty->record.nc);
685 di.f1 = fty->record.fc;
689 case_data_rw (c, fty->record.v->fv)->f = v.f;
690 for (iter = fty->recs_head; iter; iter = iter->next)
692 if (iter->flags & RCT_OTHER)
694 for (i = 0; i < iter->nv; i++)
695 if (iter->v[i].f == v.f)
699 msg (SW, _("Unknown record type %g."), v.f);
701 dfm_forward_record (fty->reader);
705 /* Arrive here if there is a matching record_type, which is in
707 dfm_forward_record (fty->reader);
712 case FTY_MIXED: read_from_file_type_mixed(); break;
713 case FTY_GROUPED: read_from_file_type_grouped(); break;
714 case FTY_NESTED: read_from_file_type_nested(); break;
718 dfm_pop (fty->reader);
720 return !dfm_reader_error (fty->reader);
724 file_type_source_destroy (struct case_source *source)
726 struct file_type_pgm *fty = source->aux;
727 struct record_type *iter, *next;
729 cancel_transformations ();
730 dfm_close_reader (fty->reader);
731 for (iter = fty->recs_head; iter; iter = next)
738 static const struct case_source_class file_type_source_class =
742 file_type_source_read,
743 file_type_source_destroy,