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.h>
27 #include <data/data-in.h>
28 #include <data/dictionary.h>
29 #include <data/format.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/data-io/data-reader.h>
33 #include <language/data-io/file-handle.h>
34 #include <language/lexer/lexer.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/compiler.h>
37 #include <libpspp/message.h>
38 #include <libpspp/str.h>
39 #include <procedure.h>
42 #define _(msgid) gettext (msgid)
44 /* Defines the three types of complex files read by FILE TYPE. */
52 /* Limited variable column specifications. */
55 char name[LONG_NAME_LEN + 1]; /* Variable name. */
56 int fc, nc; /* First column (1-based), # of columns. */
57 int fmt; /* Format type. */
58 struct variable *v; /* Variable. */
61 /* RCT_* record type constants. */
64 RCT_OTHER = 001, /* 1=OTHER. */
65 RCT_SKIP = 002, /* 1=SKIP. */
66 RCT_DUPLICATE = 004, /* DUPLICATE: 0=NOWARN, 1=WARN. */
67 RCT_MISSING = 010, /* MISSING: 0=NOWARN, 1=WARN. */
68 RCT_SPREAD = 020 /* SPREAD: 0=NO, 1=YES. */
71 /* Represents a RECORD TYPE command. */
74 struct record_type *next;
75 unsigned flags; /* RCT_* constants. */
76 union value *v; /* Vector of values for this record type. */
77 int nv; /* Length of vector V. */
78 struct col_spec case_sbc; /* CASE subcommand. */
79 int ft, lt; /* First, last transformation index. */
82 /* Represents a FILE TYPE input program. */
85 int type; /* One of the FTY_* constants. */
86 struct dfm_reader *reader; /* Data file to read. */
87 struct col_spec record; /* RECORD subcommand. */
88 struct col_spec case_sbc; /* CASE subcommand. */
89 int wild; /* 0=NOWARN, 1=WARN. */
90 int duplicate; /* 0=NOWARN, 1=WARN. */
91 int missing; /* 0=NOWARN, 1=WARN, 2=CASE. */
92 int ordered; /* 0=NO, 1=YES. */
93 int had_rec_type; /* 1=Had a RECORD TYPE command.
94 RECORD TYPE must precede the first
96 struct record_type *recs_head; /* List of record types. */
97 struct record_type *recs_tail; /* Last in list of record types. */
98 size_t case_size; /* Case size in bytes. */
101 static int parse_col_spec (struct col_spec *, const char *);
102 static void create_col_var (struct col_spec *c);
103 static const struct case_source_class file_type_source_class;
105 static bool inside_file_type;
107 /* Returns true if we're parsing the inside of a FILE TYPE...END
108 FILE TYPE construct, false otherwise. */
112 return inside_file_type;
115 int cmd_file_type (void);
117 /* Parses FILE TYPE command. */
121 static struct file_type_pgm *fty; /* FIXME: static? WTF? */
122 struct file_handle *fh = fh_inline_file ();
125 discard_variables ();
127 fty = xmalloc (sizeof *fty);
129 fty->record.name[0] = 0;
130 fty->case_sbc.name[0] = 0;
131 fty->wild = fty->duplicate = fty->missing = fty->ordered = 0;
132 fty->had_rec_type = 0;
133 fty->recs_head = fty->recs_tail = NULL;
135 if (lex_match_id ("MIXED"))
136 fty->type = FTY_MIXED;
137 else if (lex_match_id ("GROUPED"))
139 fty->type = FTY_GROUPED;
145 else if (lex_match_id ("NESTED"))
146 fty->type = FTY_NESTED;
149 msg (SE, _("MIXED, GROUPED, or NESTED expected."));
155 if (lex_match_id ("FILE"))
158 fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
162 else if (lex_match_id ("RECORD"))
165 if (!parse_col_spec (&fty->record, "####RECD"))
168 else if (lex_match_id ("CASE"))
170 if (fty->type == FTY_MIXED)
172 msg (SE, _("The CASE subcommand is not valid on FILE TYPE "
178 if (!parse_col_spec (&fty->case_sbc, "####CASE"))
181 else if (lex_match_id ("WILD"))
184 if (lex_match_id ("WARN"))
186 else if (lex_match_id ("NOWARN"))
190 msg (SE, _("WARN or NOWARN expected after WILD."));
194 else if (lex_match_id ("DUPLICATE"))
196 if (fty->type == FTY_MIXED)
198 msg (SE, _("The DUPLICATE subcommand is not valid on "
199 "FILE TYPE MIXED."));
204 if (lex_match_id ("WARN"))
206 else if (lex_match_id ("NOWARN"))
208 else if (lex_match_id ("CASE"))
210 if (fty->type != FTY_NESTED)
212 msg (SE, _("DUPLICATE=CASE is only valid on "
213 "FILE TYPE NESTED."));
221 msg (SE, _("WARN%s expected after DUPLICATE."),
222 (fty->type == FTY_NESTED ? _(", NOWARN, or CASE")
227 else if (lex_match_id ("MISSING"))
229 if (fty->type == FTY_MIXED)
231 msg (SE, _("The MISSING subcommand is not valid on "
232 "FILE TYPE MIXED."));
237 if (lex_match_id ("NOWARN"))
239 else if (lex_match_id ("WARN"))
243 msg (SE, _("WARN or NOWARN after MISSING."));
247 else if (lex_match_id ("ORDERED"))
249 if (fty->type != FTY_GROUPED)
251 msg (SE, _("ORDERED is only valid on FILE TYPE GROUPED."));
256 if (lex_match_id ("YES"))
258 else if (lex_match_id ("NO"))
262 msg (SE, _("YES or NO expected after ORDERED."));
268 lex_error (_("while expecting a valid subcommand"));
273 if (fty->record.name[0] == 0)
275 msg (SE, _("The required RECORD subcommand was not present."));
279 if (fty->type == FTY_GROUPED)
281 if (fty->case_sbc.name[0] == 0)
283 msg (SE, _("The required CASE subcommand was not present."));
287 if (!strcasecmp (fty->case_sbc.name, fty->record.name))
289 msg (SE, _("CASE and RECORD must specify different variable "
295 fty->reader = dfm_open_reader (fh);
296 if (fty->reader == NULL)
298 fh_set_default_handle (fh);
300 create_col_var (&fty->record);
301 if (fty->case_sbc.name[0])
302 create_col_var (&fty->case_sbc);
303 vfm_source = create_case_source (&file_type_source_class, fty);
309 return CMD_CASCADING_FAILURE;
312 /* Creates a variable with attributes specified by struct col_spec C, and
313 stores it into C->V. */
315 create_col_var (struct col_spec *c)
319 if (formats[c->fmt].cat & FCAT_STRING)
323 c->v = dict_create_var (default_dict, c->name, width);
326 /* Parses variable, column, type specifications for a variable. */
328 parse_col_spec (struct col_spec *c, const char *def_name)
330 struct fmt_spec spec;
335 strcpy (c->name, tokid);
339 strcpy (c->name, def_name);
342 if (!lex_force_int ())
344 c->fc = lex_integer ();
347 msg (SE, _("Column value must be positive."));
353 lex_negative_to_dash ();
356 if (!lex_force_int ())
358 c->nc = lex_integer ();
363 msg (SE, _("Ending column precedes beginning column."));
372 /* Format specifier. */
376 if (!lex_force_id ())
378 c->fmt = parse_format_specifier_name (&cp, 0);
383 msg (SE, _("Bad format specifier name."));
387 if (!lex_force_match (')'))
396 return check_input_specifier (&spec, 1);
401 /* Parse the RECORD TYPE command. */
403 cmd_record_type (void)
405 struct file_type_pgm *fty;
406 struct record_type *rct;
408 fty = vfm_source->aux;
410 /* Initialize the record_type structure. */
411 rct = xmalloc (sizeof *rct);
415 rct->flags |= RCT_DUPLICATE;
417 rct->flags |= RCT_MISSING;
421 if (fty->case_sbc.name[0])
422 rct->case_sbc = fty->case_sbc;
424 if (fty->recs_tail && (fty->recs_tail->flags & RCT_OTHER))
426 msg (SE, _("OTHER may appear only on the last RECORD TYPE command."));
432 fty->recs_tail->lt = n_trns - 1;
433 if (!(fty->recs_tail->flags & RCT_SKIP)
434 && fty->recs_tail->ft == fty->recs_tail->lt)
436 msg (SE, _("No input commands (DATA LIST, REPEATING DATA) "
437 "for above RECORD TYPE."));
442 /* Parse record type values. */
443 if (lex_match_id ("OTHER"))
444 rct->flags |= RCT_OTHER;
449 while (lex_is_number () || token == T_STRING)
454 rct->v = xnrealloc (rct->v, mv, sizeof *rct->v);
457 if (formats[fty->record.fmt].cat & FCAT_STRING)
459 if (!lex_force_string ())
461 rct->v[rct->nv].c = xmalloc (fty->record.nc + 1);
462 buf_copy_str_rpad (rct->v[rct->nv].c, fty->record.nc + 1,
467 if (!lex_force_num ())
469 rct->v[rct->nv].f = tokval;
478 /* Parse the rest of the subcommands. */
481 if (lex_match_id ("SKIP"))
482 rct->flags |= RCT_SKIP;
483 else if (lex_match_id ("CASE"))
485 if (fty->type == FTY_MIXED)
487 msg (SE, _("The CASE subcommand is not allowed on "
488 "the RECORD TYPE command for FILE TYPE MIXED."));
493 if (!parse_col_spec (&rct->case_sbc, ""))
495 if (rct->case_sbc.name[0])
497 msg (SE, _("No variable name may be specified for the "
498 "CASE subcommand on RECORD TYPE."));
502 if ((formats[rct->case_sbc.fmt].cat ^ formats[fty->case_sbc.fmt].cat)
505 msg (SE, _("The CASE column specification on RECORD TYPE "
506 "must give a format specifier that is the "
507 "same type as that of the CASE column "
508 "specification given on FILE TYPE."));
512 else if (lex_match_id ("DUPLICATE"))
515 if (lex_match_id ("WARN"))
516 rct->flags |= RCT_DUPLICATE;
517 else if (lex_match_id ("NOWARN"))
518 rct->flags &= ~RCT_DUPLICATE;
521 msg (SE, _("WARN or NOWARN expected on DUPLICATE "
526 else if (lex_match_id ("MISSING"))
529 if (lex_match_id ("WARN"))
530 rct->flags |= RCT_MISSING;
531 else if (lex_match_id ("NOWARN"))
532 rct->flags &= ~RCT_MISSING;
535 msg (SE, _("WARN or NOWARN expected on MISSING subcommand."));
539 else if (lex_match_id ("SPREAD"))
542 if (lex_match_id ("YES"))
543 rct->flags |= RCT_SPREAD;
544 else if (lex_match_id ("NO"))
545 rct->flags &= ~RCT_SPREAD;
548 msg (SE, _("YES or NO expected on SPREAD subcommand."));
554 lex_error (_("while expecting a valid subcommand"));
560 fty->recs_tail = fty->recs_tail->next = xmalloc (sizeof *fty->recs_tail);
562 fty->recs_head = fty->recs_tail = xmalloc (sizeof *fty->recs_tail);
563 memcpy (fty->recs_tail, &rct, sizeof *fty->recs_tail);
568 if (formats[fty->record.fmt].cat & FCAT_STRING)
572 for (i = 0; i < rct->nv; i++)
578 return CMD_CASCADING_FAILURE;
583 int cmd_end_file_type (void);
585 cmd_end_file_type (void)
587 struct file_type_pgm *fty;
589 fty = vfm_source->aux;
590 fty->case_size = dict_get_case_size (default_dict);
594 fty->recs_tail->lt = n_trns - 1;
595 if (!(fty->recs_tail->flags & RCT_SKIP)
596 && fty->recs_tail->ft == fty->recs_tail->lt)
598 msg (SE, _("No input commands (DATA LIST, REPEATING DATA) "
599 "on above RECORD TYPE."));
605 msg (SE, _("No commands between FILE TYPE and END FILE TYPE."));
611 return lex_end_of_command ();
614 /* Come here on I/O error. */
615 discard_variables ();
616 return CMD_CASCADING_FAILURE;
619 /* FILE TYPE runtime. */
621 /*static void read_from_file_type_mixed(void);
622 static void read_from_file_type_grouped(void);
623 static void read_from_file_type_nested(void); */
625 /* Reads any number of cases into case C and calls write_case()
626 for each one. Compare data-list.c:read_from_data_list.
627 Returns true if successful, false if an I/O error occurred. */
629 file_type_source_read (struct case_source *source,
631 write_case_func *write_case UNUSED,
632 write_case_data wc_data UNUSED)
634 struct file_type_pgm *fty = source->aux;
635 struct fmt_spec format;
637 dfm_push (fty->reader);
639 format.type = fty->record.fmt;
640 format.w = fty->record.nc;
642 while (!dfm_eof (fty->reader))
644 struct fixed_string line;
645 struct record_type *iter;
649 dfm_expand_tabs (fty->reader);
650 dfm_get_record (fty->reader, &line);
651 if (formats[fty->record.fmt].cat & FCAT_STRING)
655 v.c = case_data_rw (c, fty->record.v->fv)->s;
657 data_in_finite_line (&di, ls_c_str (&line), ls_length (&line),
658 fty->record.fc, fty->record.fc + fty->record.nc);
659 di.v = (union value *) v.c;
661 di.f1 = fty->record.fc;
665 for (iter = fty->recs_head; iter; iter = iter->next)
667 if (iter->flags & RCT_OTHER)
669 for (i = 0; i < iter->nv; i++)
670 if (!memcmp (iter->v[i].c, v.c, fty->record.nc))
674 msg (SW, _("Unknown record type \"%.*s\"."), fty->record.nc, v.c);
680 data_in_finite_line (&di, ls_c_str (&line), ls_length (&line),
681 fty->record.fc, fty->record.fc + fty->record.nc);
684 di.f1 = fty->record.fc;
688 case_data_rw (c, fty->record.v->fv)->f = v.f;
689 for (iter = fty->recs_head; iter; iter = iter->next)
691 if (iter->flags & RCT_OTHER)
693 for (i = 0; i < iter->nv; i++)
694 if (iter->v[i].f == v.f)
698 msg (SW, _("Unknown record type %g."), v.f);
700 dfm_forward_record (fty->reader);
704 /* Arrive here if there is a matching record_type, which is in
706 dfm_forward_record (fty->reader);
711 case FTY_MIXED: read_from_file_type_mixed(); break;
712 case FTY_GROUPED: read_from_file_type_grouped(); break;
713 case FTY_NESTED: read_from_file_type_nested(); break;
717 dfm_pop (fty->reader);
719 return !dfm_reader_error (fty->reader);
723 file_type_source_destroy (struct case_source *source)
725 struct file_type_pgm *fty = source->aux;
726 struct record_type *iter, *next;
728 cancel_transformations ();
729 dfm_close_reader (fty->reader);
730 for (iter = fty->recs_head; iter; iter = next)
737 static const struct case_source_class file_type_source_class =
741 file_type_source_read,
742 file_type_source_destroy,