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
27 #include "dictionary.h"
29 #include "file-handle.h"
37 #define _(msgid) gettext (msgid)
39 /* Defines the three types of complex files read by FILE TYPE. */
47 /* Limited variable column specifications. */
50 char name[LONG_NAME_LEN + 1]; /* Variable name. */
51 int fc, nc; /* First column (1-based), # of columns. */
52 int fmt; /* Format type. */
53 struct variable *v; /* Variable. */
56 /* RCT_* record type constants. */
59 RCT_OTHER = 001, /* 1=OTHER. */
60 RCT_SKIP = 002, /* 1=SKIP. */
61 RCT_DUPLICATE = 004, /* DUPLICATE: 0=NOWARN, 1=WARN. */
62 RCT_MISSING = 010, /* MISSING: 0=NOWARN, 1=WARN. */
63 RCT_SPREAD = 020 /* SPREAD: 0=NO, 1=YES. */
66 /* Represents a RECORD TYPE command. */
69 struct record_type *next;
70 unsigned flags; /* RCT_* constants. */
71 union value *v; /* Vector of values for this record type. */
72 int nv; /* Length of vector V. */
73 struct col_spec case_sbc; /* CASE subcommand. */
74 int ft, lt; /* First, last transformation index. */
77 /* Represents a FILE TYPE input program. */
80 int type; /* One of the FTY_* constants. */
81 struct dfm_reader *reader; /* Data file to read. */
82 struct col_spec record; /* RECORD subcommand. */
83 struct col_spec case_sbc; /* CASE subcommand. */
84 int wild; /* 0=NOWARN, 1=WARN. */
85 int duplicate; /* 0=NOWARN, 1=WARN. */
86 int missing; /* 0=NOWARN, 1=WARN, 2=CASE. */
87 int ordered; /* 0=NO, 1=YES. */
88 int had_rec_type; /* 1=Had a RECORD TYPE command.
89 RECORD TYPE must precede the first
91 struct record_type *recs_head; /* List of record types. */
92 struct record_type *recs_tail; /* Last in list of record types. */
93 size_t case_size; /* Case size in bytes. */
96 static int parse_col_spec (struct col_spec *, const char *);
97 static void create_col_var (struct col_spec *c);
99 int cmd_file_type (void);
101 /* Parses FILE TYPE command. */
105 static struct file_type_pgm *fty; /* FIXME: static? WTF? */
106 struct file_handle *fh = NULL;
109 discard_variables ();
111 fty = xmalloc (sizeof *fty);
113 fty->record.name[0] = 0;
114 fty->case_sbc.name[0] = 0;
115 fty->wild = fty->duplicate = fty->missing = fty->ordered = 0;
116 fty->had_rec_type = 0;
117 fty->recs_head = fty->recs_tail = NULL;
119 if (lex_match_id ("MIXED"))
120 fty->type = FTY_MIXED;
121 else if (lex_match_id ("GROUPED"))
123 fty->type = FTY_GROUPED;
129 else if (lex_match_id ("NESTED"))
130 fty->type = FTY_NESTED;
133 msg (SE, _("MIXED, GROUPED, or NESTED expected."));
139 if (lex_match_id ("FILE"))
146 else if (lex_match_id ("RECORD"))
149 if (!parse_col_spec (&fty->record, "####RECD"))
152 else if (lex_match_id ("CASE"))
154 if (fty->type == FTY_MIXED)
156 msg (SE, _("The CASE subcommand is not valid on FILE TYPE "
162 if (!parse_col_spec (&fty->case_sbc, "####CASE"))
165 else if (lex_match_id ("WILD"))
168 if (lex_match_id ("WARN"))
170 else if (lex_match_id ("NOWARN"))
174 msg (SE, _("WARN or NOWARN expected after WILD."));
178 else if (lex_match_id ("DUPLICATE"))
180 if (fty->type == FTY_MIXED)
182 msg (SE, _("The DUPLICATE subcommand is not valid on "
183 "FILE TYPE MIXED."));
188 if (lex_match_id ("WARN"))
190 else if (lex_match_id ("NOWARN"))
192 else if (lex_match_id ("CASE"))
194 if (fty->type != FTY_NESTED)
196 msg (SE, _("DUPLICATE=CASE is only valid on "
197 "FILE TYPE NESTED."));
205 msg (SE, _("WARN%s expected after DUPLICATE."),
206 (fty->type == FTY_NESTED ? _(", NOWARN, or CASE")
211 else if (lex_match_id ("MISSING"))
213 if (fty->type == FTY_MIXED)
215 msg (SE, _("The MISSING subcommand is not valid on "
216 "FILE TYPE MIXED."));
221 if (lex_match_id ("NOWARN"))
223 else if (lex_match_id ("WARN"))
227 msg (SE, _("WARN or NOWARN after MISSING."));
231 else if (lex_match_id ("ORDERED"))
233 if (fty->type != FTY_GROUPED)
235 msg (SE, _("ORDERED is only valid on FILE TYPE GROUPED."));
240 if (lex_match_id ("YES"))
242 else if (lex_match_id ("NO"))
246 msg (SE, _("YES or NO expected after ORDERED."));
252 lex_error (_("while expecting a valid subcommand"));
257 if (fty->record.name[0] == 0)
259 msg (SE, _("The required RECORD subcommand was not present."));
263 if (fty->type == FTY_GROUPED)
265 if (fty->case_sbc.name[0] == 0)
267 msg (SE, _("The required CASE subcommand was not present."));
271 if (!strcasecmp (fty->case_sbc.name, fty->record.name))
273 msg (SE, _("CASE and RECORD must specify different variable "
279 fty->reader = dfm_open_reader (fh);
280 if (fty->reader == NULL)
284 create_col_var (&fty->record);
285 if (fty->case_sbc.name[0])
286 create_col_var (&fty->case_sbc);
287 vfm_source = create_case_source (&file_type_source_class, fty);
296 /* Creates a variable with attributes specified by struct col_spec C, and
297 stores it into C->V. */
299 create_col_var (struct col_spec *c)
303 if (formats[c->fmt].cat & FCAT_STRING)
307 c->v = dict_create_var (default_dict, c->name, width);
310 /* Parses variable, column, type specifications for a variable. */
312 parse_col_spec (struct col_spec *c, const char *def_name)
314 struct fmt_spec spec;
319 strcpy (c->name, tokid);
323 strcpy (c->name, def_name);
326 if (!lex_force_int ())
328 c->fc = lex_integer ();
331 msg (SE, _("Column value must be positive."));
337 lex_negative_to_dash ();
340 if (!lex_force_int ())
342 c->nc = lex_integer ();
347 msg (SE, _("Ending column precedes beginning column."));
356 /* Format specifier. */
360 if (!lex_force_id ())
362 c->fmt = parse_format_specifier_name (&cp, 0);
367 msg (SE, _("Bad format specifier name."));
371 if (!lex_force_match (')'))
380 return check_input_specifier (&spec, 1);
385 /* Parse the RECORD TYPE command. */
387 cmd_record_type (void)
389 struct file_type_pgm *fty;
390 struct record_type *rct;
392 /* Make sure we're inside a FILE TYPE structure. */
393 if (pgm_state != STATE_INPUT
394 || !case_source_is_class (vfm_source, &file_type_source_class))
396 msg (SE, _("This command may only appear within a "
397 "FILE TYPE/END FILE TYPE structure."));
401 fty = vfm_source->aux;
403 /* Initialize the record_type structure. */
404 rct = xmalloc (sizeof *rct);
408 rct->flags |= RCT_DUPLICATE;
410 rct->flags |= RCT_MISSING;
414 if (fty->case_sbc.name[0])
415 rct->case_sbc = fty->case_sbc;
417 if (fty->recs_tail && (fty->recs_tail->flags & RCT_OTHER))
419 msg (SE, _("OTHER may appear only on the last RECORD TYPE command."));
425 fty->recs_tail->lt = n_trns - 1;
426 if (!(fty->recs_tail->flags & RCT_SKIP)
427 && fty->recs_tail->ft == fty->recs_tail->lt)
429 msg (SE, _("No input commands (DATA LIST, REPEATING DATA) "
430 "for above RECORD TYPE."));
435 /* Parse record type values. */
436 if (lex_match_id ("OTHER"))
437 rct->flags |= RCT_OTHER;
442 while (lex_is_number () || token == T_STRING)
447 rct->v = xnrealloc (rct->v, mv, sizeof *rct->v);
450 if (formats[fty->record.fmt].cat & FCAT_STRING)
452 if (!lex_force_string ())
454 rct->v[rct->nv].c = xmalloc (fty->record.nc + 1);
455 buf_copy_str_rpad (rct->v[rct->nv].c, fty->record.nc + 1,
460 if (!lex_force_num ())
462 rct->v[rct->nv].f = tokval;
471 /* Parse the rest of the subcommands. */
474 if (lex_match_id ("SKIP"))
475 rct->flags |= RCT_SKIP;
476 else if (lex_match_id ("CASE"))
478 if (fty->type == FTY_MIXED)
480 msg (SE, _("The CASE subcommand is not allowed on "
481 "the RECORD TYPE command for FILE TYPE MIXED."));
486 if (!parse_col_spec (&rct->case_sbc, ""))
488 if (rct->case_sbc.name[0])
490 msg (SE, _("No variable name may be specified for the "
491 "CASE subcommand on RECORD TYPE."));
495 if ((formats[rct->case_sbc.fmt].cat ^ formats[fty->case_sbc.fmt].cat)
498 msg (SE, _("The CASE column specification on RECORD TYPE "
499 "must give a format specifier that is the "
500 "same type as that of the CASE column "
501 "specification given on FILE TYPE."));
505 else if (lex_match_id ("DUPLICATE"))
508 if (lex_match_id ("WARN"))
509 rct->flags |= RCT_DUPLICATE;
510 else if (lex_match_id ("NOWARN"))
511 rct->flags &= ~RCT_DUPLICATE;
514 msg (SE, _("WARN or NOWARN expected on DUPLICATE "
519 else if (lex_match_id ("MISSING"))
522 if (lex_match_id ("WARN"))
523 rct->flags |= RCT_MISSING;
524 else if (lex_match_id ("NOWARN"))
525 rct->flags &= ~RCT_MISSING;
528 msg (SE, _("WARN or NOWARN expected on MISSING subcommand."));
532 else if (lex_match_id ("SPREAD"))
535 if (lex_match_id ("YES"))
536 rct->flags |= RCT_SPREAD;
537 else if (lex_match_id ("NO"))
538 rct->flags &= ~RCT_SPREAD;
541 msg (SE, _("YES or NO expected on SPREAD subcommand."));
547 lex_error (_("while expecting a valid subcommand"));
553 fty->recs_tail = fty->recs_tail->next = xmalloc (sizeof *fty->recs_tail);
555 fty->recs_head = fty->recs_tail = xmalloc (sizeof *fty->recs_tail);
556 memcpy (fty->recs_tail, &rct, sizeof *fty->recs_tail);
561 if (formats[fty->record.fmt].cat & FCAT_STRING)
565 for (i = 0; i < rct->nv; i++)
576 int cmd_end_file_type (void);
578 cmd_end_file_type (void)
580 struct file_type_pgm *fty;
582 if (pgm_state != STATE_INPUT
583 || case_source_is_class (vfm_source, &file_type_source_class))
585 msg (SE, _("This command may only appear within a "
586 "FILE TYPE/END FILE TYPE structure."));
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 discovering catastrophic error. */
616 discard_variables ();
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. */
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);
721 file_type_source_destroy (struct case_source *source)
723 struct file_type_pgm *fty = source->aux;
724 struct record_type *iter, *next;
726 cancel_transformations ();
727 dfm_close_reader (fty->reader);
728 for (iter = fty->recs_head; iter; iter = next)
735 const struct case_source_class file_type_source_class =
739 file_type_source_read,
740 file_type_source_destroy,