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
28 #include "file-handle.h"
35 /* Defines the three types of complex files read by FILE TYPE. */
43 /* Limited variable column specifications. */
46 char name[9]; /* Variable name. */
47 int fc, nc; /* First column (1-based), # of columns. */
48 int fmt; /* Format type. */
49 struct variable *v; /* Variable. */
52 /* RCT_* record type constants. */
55 RCT_OTHER = 001, /* 1=OTHER. */
56 RCT_SKIP = 002, /* 1=SKIP. */
57 RCT_DUPLICATE = 004, /* DUPLICATE: 0=NOWARN, 1=WARN. */
58 RCT_MISSING = 010, /* MISSING: 0=NOWARN, 1=WARN. */
59 RCT_SPREAD = 020 /* SPREAD: 0=NO, 1=YES. */
62 /* Represents a RECORD TYPE command. */
65 struct record_type *next;
66 unsigned flags; /* RCT_* constants. */
67 union value *v; /* Vector of values for this record type. */
68 int nv; /* Length of vector V. */
69 struct col_spec case_sbc; /* CASE subcommand. */
70 int ft, lt; /* First, last transformation index. */
73 /* Represents a FILE TYPE input program. Does not contain a
74 trns_header because it's never submitted as a transformation. */
77 int type; /* One of the FTY_* constants. */
78 struct file_handle *handle; /* File handle of input file. */
79 struct col_spec record; /* RECORD subcommand. */
80 struct col_spec case_sbc; /* CASE subcommand. */
81 int wild; /* 0=NOWARN, 1=WARN. */
82 int duplicate; /* 0=NOWARN, 1=WARN. */
83 int missing; /* 0=NOWARN, 1=WARN, 2=CASE. */
84 int ordered; /* 0=NO, 1=YES. */
85 int had_rec_type; /* 1=Had a RECORD TYPE command.
86 RECORD TYPE must precede the first
88 struct record_type *recs_head; /* List of record types. */
89 struct record_type *recs_tail; /* Last in list of record types. */
92 /* Current FILE TYPE input program. */
93 static struct file_type_pgm fty;
95 static int parse_col_spec (struct col_spec *, const char *);
96 static void create_col_var (struct col_spec *c);
98 /* Parses FILE TYPE command. */
103 discard_variables ();
104 fty.handle = inline_file;
105 fty.record.name[0] = 0;
106 fty.case_sbc.name[0] = 0;
107 fty.wild = fty.duplicate = fty.missing = fty.ordered = 0;
108 fty.had_rec_type = 0;
109 fty.recs_head = fty.recs_tail = NULL;
111 lex_match_id ("TYPE");
112 if (lex_match_id ("MIXED"))
113 fty.type = FTY_MIXED;
114 else if (lex_match_id ("GROUPED"))
116 fty.type = FTY_GROUPED;
122 else if (lex_match_id ("NESTED"))
123 fty.type = FTY_NESTED;
126 msg (SE, _("MIXED, GROUPED, or NESTED expected."));
132 if (lex_match_id ("FILE"))
135 fty.handle = fh_parse_file_handle ();
139 else if (lex_match_id ("RECORD"))
142 if (!parse_col_spec (&fty.record, "####RECD"))
145 else if (lex_match_id ("CASE"))
147 if (fty.type == FTY_MIXED)
149 msg (SE, _("The CASE subcommand is not valid on FILE TYPE "
155 if (!parse_col_spec (&fty.case_sbc, "####CASE"))
158 else if (lex_match_id ("WILD"))
161 if (lex_match_id ("WARN"))
163 else if (lex_match_id ("NOWARN"))
167 msg (SE, _("WARN or NOWARN expected after WILD."));
171 else if (lex_match_id ("DUPLICATE"))
173 if (fty.type == FTY_MIXED)
175 msg (SE, _("The DUPLICATE subcommand is not valid on "
176 "FILE TYPE MIXED."));
181 if (lex_match_id ("WARN"))
183 else if (lex_match_id ("NOWARN"))
185 else if (lex_match_id ("CASE"))
187 if (fty.type != FTY_NESTED)
189 msg (SE, _("DUPLICATE=CASE is only valid on "
190 "FILE TYPE NESTED."));
198 msg (SE, _("WARN%s expected after DUPLICATE."),
199 (fty.type == FTY_NESTED ? _(", NOWARN, or CASE")
204 else if (lex_match_id ("MISSING"))
206 if (fty.type == FTY_MIXED)
208 msg (SE, _("The MISSING subcommand is not valid on "
209 "FILE TYPE MIXED."));
214 if (lex_match_id ("NOWARN"))
216 else if (lex_match_id ("WARN"))
220 msg (SE, _("WARN or NOWARN after MISSING."));
224 else if (lex_match_id ("ORDERED"))
226 if (fty.type != FTY_GROUPED)
228 msg (SE, _("ORDERED is only valid on FILE TYPE GROUPED."));
233 if (lex_match_id ("YES"))
235 else if (lex_match_id ("NO"))
239 msg (SE, _("YES or NO expected after ORDERED."));
245 lex_error (_("while expecting a valid subcommand"));
250 if (fty.record.name[0] == 0)
252 msg (SE, _("The required RECORD subcommand was not present."));
256 if (fty.type == FTY_GROUPED)
258 if (fty.case_sbc.name[0] == 0)
260 msg (SE, _("The required CASE subcommand was not present."));
264 if (!strcmp (fty.case_sbc.name, fty.record.name))
266 msg (SE, _("CASE and RECORD must specify different variable "
272 default_handle = fty.handle;
274 vfm_source = &file_type_source;
275 create_col_var (&fty.record);
276 if (fty.case_sbc.name[0])
277 create_col_var (&fty.case_sbc);
282 /* Creates a variable with attributes specified by struct col_spec C, and
283 stores it into C->V. */
285 create_col_var (struct col_spec *c)
289 if (formats[c->fmt].cat & FCAT_STRING)
293 c->v = dict_create_var (default_dict, c->name, width);
296 /* Parses variable, column, type specifications for a variable. */
298 parse_col_spec (struct col_spec *c, const char *def_name)
300 struct fmt_spec spec;
304 strcpy (c->name, tokid);
308 strcpy (c->name, def_name);
310 if (!lex_force_int ())
312 c->fc = lex_integer ();
315 msg (SE, _("Column value must be positive."));
320 lex_negative_to_dash ();
323 if (!lex_force_int ())
325 c->nc = lex_integer ();
330 msg (SE, _("Ending column precedes beginning column."));
342 if (!lex_force_id ())
344 c->fmt = parse_format_specifier_name (&cp, 0);
349 msg (SE, _("Bad format specifier name."));
353 if (!lex_force_match (')'))
362 return check_input_specifier (&spec);
367 /* Structure being filled in by internal_cmd_record_type. */
368 static struct record_type rct;
370 static int internal_cmd_record_type (void);
372 /* Parse the RECORD TYPE command. */
374 cmd_record_type (void)
376 int result = internal_cmd_record_type ();
378 if (result == CMD_FAILURE)
382 if (formats[fty.record.fmt].cat & FCAT_STRING)
383 for (i = 0; i < rct.nv; i++)
392 internal_cmd_record_type (void)
394 /* Initialize the record_type structure. */
398 rct.flags |= RCT_DUPLICATE;
400 rct.flags |= RCT_MISSING;
404 if (fty.case_sbc.name[0])
405 rct.case_sbc = fty.case_sbc;
407 /* Make sure we're inside a FILE TYPE structure. */
408 if (pgm_state != STATE_INPUT || vfm_source != &file_type_source)
410 msg (SE, _("This command may only appear within a "
411 "FILE TYPE/END FILE TYPE structure."));
415 if (fty.recs_tail && (fty.recs_tail->flags & RCT_OTHER))
417 msg (SE, _("OTHER may appear only on the last RECORD TYPE command."));
423 fty.recs_tail->lt = n_trns - 1;
424 if (!(fty.recs_tail->flags & RCT_SKIP)
425 && fty.recs_tail->ft == fty.recs_tail->lt)
427 msg (SE, _("No input commands (DATA LIST, REPEATING DATA) "
428 "for above RECORD TYPE."));
433 lex_match_id ("RECORD");
434 lex_match_id ("TYPE");
436 /* Parse record type values. */
437 if (lex_match_id ("OTHER"))
438 rct.flags |= RCT_OTHER;
443 while (token == T_NUM || token == T_STRING)
448 rct.v = xrealloc (rct.v, mv * sizeof *rct.v);
451 if (formats[fty.record.fmt].cat & FCAT_STRING)
453 if (!lex_force_string ())
455 rct.v[rct.nv].c = xmalloc (fty.record.nc + 1);
456 st_bare_pad_copy (rct.v[rct.nv].c, ds_value (&tokstr),
461 if (!lex_force_num ())
463 rct.v[rct.nv].f = tokval;
472 /* Parse the rest of the subcommands. */
475 if (lex_match_id ("SKIP"))
476 rct.flags |= RCT_SKIP;
477 else if (lex_match_id ("CASE"))
479 if (fty.type == FTY_MIXED)
481 msg (SE, _("The CASE subcommand is not allowed on "
482 "the RECORD TYPE command for FILE TYPE MIXED."));
487 if (!parse_col_spec (&rct.case_sbc, ""))
489 if (rct.case_sbc.name[0])
491 msg (SE, _("No variable name may be specified for the "
492 "CASE subcommand on RECORD TYPE."));
496 if ((formats[rct.case_sbc.fmt].cat ^ formats[fty.case_sbc.fmt].cat)
499 msg (SE, _("The CASE column specification on RECORD TYPE "
500 "must give a format specifier that is the "
501 "same type as that of the CASE column "
502 "specification given on FILE TYPE."));
506 else if (lex_match_id ("DUPLICATE"))
509 if (lex_match_id ("WARN"))
510 rct.flags |= RCT_DUPLICATE;
511 else if (lex_match_id ("NOWARN"))
512 rct.flags &= ~RCT_DUPLICATE;
515 msg (SE, _("WARN or NOWARN expected on DUPLICATE "
520 else if (lex_match_id ("MISSING"))
523 if (lex_match_id ("WARN"))
524 rct.flags |= RCT_MISSING;
525 else if (lex_match_id ("NOWARN"))
526 rct.flags &= ~RCT_MISSING;
529 msg (SE, _("WARN or NOWARN expected on MISSING subcommand."));
533 else if (lex_match_id ("SPREAD"))
536 if (lex_match_id ("YES"))
537 rct.flags |= RCT_SPREAD;
538 else if (lex_match_id ("NO"))
539 rct.flags &= ~RCT_SPREAD;
542 msg (SE, _("YES or NO expected on SPREAD subcommand."));
548 lex_error (_("while expecting a valid subcommand"));
554 fty.recs_tail = fty.recs_tail->next = xmalloc (sizeof *fty.recs_tail);
556 fty.recs_head = fty.recs_tail = xmalloc (sizeof *fty.recs_tail);
557 memcpy (fty.recs_tail, &rct, sizeof *fty.recs_tail);
565 cmd_end_file_type (void)
567 if (pgm_state != STATE_INPUT || vfm_source != &file_type_source)
569 msg (SE, _("This command may only appear within a "
570 "FILE TYPE/END FILE TYPE structure."));
574 lex_match_id ("TYPE");
578 fty.recs_tail->lt = n_trns - 1;
579 if (!(fty.recs_tail->flags & RCT_SKIP)
580 && fty.recs_tail->ft == fty.recs_tail->lt)
582 msg (SE, _("No input commands (DATA LIST, REPEATING DATA) "
583 "on above RECORD TYPE."));
589 msg (SE, _("No commands between FILE TYPE and END FILE TYPE."));
595 return lex_end_of_command ();
598 /* Come here on discovering catastrophic error. */
600 discard_variables ();
604 /* FILE TYPE runtime. */
606 /*static void read_from_file_type_mixed(void);
607 static void read_from_file_type_grouped(void);
608 static void read_from_file_type_nested(void); */
610 /* Reads any number of cases into temp_case and calls write_case() for
611 each one. Compare data-list.c:read_from_data_list. */
613 file_type_source_read (void)
618 struct fmt_spec format;
620 dfm_push (fty.handle);
622 format.type = fty.record.fmt;
623 format.w = fty.record.nc;
625 while (NULL != (line = dfm_get_record (fty.handle, &len)))
627 struct record_type *iter;
631 if (formats[fty.record.fmt].cat & FCAT_STRING)
635 v.c = temp_case->data[fty.record.v->fv].s;
637 data_in_finite_line (&di, line, len,
638 fty.record.fc, fty.record.fc + fty.record.nc);
639 di.v = (union value *) v.c;
641 di.f1 = fty.record.fc;
645 for (iter = fty.recs_head; iter; iter = iter->next)
647 if (iter->flags & RCT_OTHER)
649 for (i = 0; i < iter->nv; i++)
650 if (!memcmp (iter->v[i].c, v.c, fty.record.nc))
654 msg (SW, _("Unknown record type \"%.*s\"."), fty.record.nc, v.c);
660 data_in_finite_line (&di, line, len,
661 fty.record.fc, fty.record.fc + fty.record.nc);
664 di.f1 = fty.record.fc;
668 memcpy (&temp_case->data[fty.record.v->fv].f, &v.f, sizeof v.f);
669 for (iter = fty.recs_head; iter; iter = iter->next)
671 if (iter->flags & RCT_OTHER)
673 for (i = 0; i < iter->nv; i++)
674 if (approx_eq (iter->v[i].f, v.f))
678 msg (SW, _("Unknown record type %g."), v.f);
680 dfm_fwd_record (fty.handle);
684 /* Arrive here if there is a matching record_type, which is in
686 dfm_fwd_record (fty.handle);
691 case FTY_MIXED: read_from_file_type_mixed(); break;
692 case FTY_GROUPED: read_from_file_type_grouped(); break;
693 case FTY_NESTED: read_from_file_type_nested(); break;
697 dfm_pop (fty.handle);
701 file_type_source_destroy_source (void)
703 struct record_type *iter, *next;
705 cancel_transformations ();
706 for (iter = fty.recs_head; iter; iter = next)
713 struct case_stream file_type_source =
716 file_type_source_read,
719 file_type_source_destroy_source,