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
27 #include "data-list.h"
29 #include "dictionary.h"
32 #include "file-handle.h"
39 #include "debug-print.h"
41 /* Indicates how a `union value' should be initialized. */
44 INP_NUMERIC = 01, /* Numeric. */
45 INP_STRING = 0, /* String. */
47 INP_INIT_ONCE = 02, /* Initialize only once. */
48 INP_REINIT = 0, /* Reinitialize for each iteration. */
51 struct input_program_pgm
53 enum value_init_type *init; /* How to initialize each `union value'. */
54 size_t init_cnt; /* Number of elements in inp_init. */
55 size_t case_size; /* Size of case in bytes. */
58 static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
59 static trns_free_func reread_trns_free;
62 cmd_input_program (void)
66 /* FIXME: we shouldn't do this here, but I'm afraid that other
67 code will check the class of vfm_source. */
68 vfm_source = create_case_source (&input_program_source_class,
71 return lex_end_of_command ();
75 cmd_end_input_program (void)
77 struct input_program_pgm *inp;
80 if (!case_source_is_class (vfm_source, &input_program_source_class))
82 msg (SE, _("No matching INPUT PROGRAM command."));
86 if (dict_get_next_value_idx (default_dict) == 0)
87 msg (SW, _("No data-input or transformation commands specified "
88 "between INPUT PROGRAM and END INPUT PROGRAM."));
90 /* Mark the boundary between INPUT PROGRAM transformations and
91 ordinary transformations. */
94 /* Figure out how to initialize each input case. */
95 inp = xmalloc (sizeof *inp);
96 inp->init_cnt = dict_get_next_value_idx (default_dict);
97 inp->init = xmalloc (inp->init_cnt * sizeof *inp->init);
98 for (i = 0; i < inp->init_cnt; i++)
100 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
102 struct variable *var = dict_get_var (default_dict, i);
103 enum value_init_type value_init;
106 value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
107 value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE;
109 for (j = 0; j < var->nv; j++)
110 inp->init[j + var->fv] = value_init;
112 for (i = 0; i < inp->init_cnt; i++)
113 assert (inp->init[i] != -1);
114 inp->case_size = dict_get_case_size (default_dict);
116 /* Put inp into vfm_source for later use. */
117 vfm_source->aux = inp;
119 /* FIXME: we should use create_case_source() here. */
120 vfm_source->value_cnt = dict_get_next_value_idx (default_dict);
122 return lex_end_of_command ();
125 /* Initializes case C. Called before the first case is read. */
127 init_case (const struct input_program_pgm *inp, struct ccase *c)
131 for (i = 0; i < inp->init_cnt; i++)
132 switch (inp->init[i])
134 case INP_NUMERIC | INP_INIT_ONCE:
135 case_data_rw (c, i)->f = 0.0;
137 case INP_NUMERIC | INP_REINIT:
138 case_data_rw (c, i)->f = SYSMIS;
140 case INP_STRING | INP_INIT_ONCE:
141 case INP_STRING | INP_REINIT:
142 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
149 /* Clears case C. Called between reading successive records. */
151 clear_case (const struct input_program_pgm *inp, struct ccase *c)
155 for (i = 0; i < inp->init_cnt; i++)
156 switch (inp->init[i])
158 case INP_NUMERIC | INP_INIT_ONCE:
160 case INP_NUMERIC | INP_REINIT:
161 case_data_rw (c, i)->f = SYSMIS;
163 case INP_STRING | INP_INIT_ONCE:
165 case INP_STRING | INP_REINIT:
166 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
173 /* Executes each transformation in turn on a `blank' case. When a
174 transformation fails, returning -2, then that's the end of the
175 file. -1 means go on to the next transformation. Otherwise the
176 return value is the index of the transformation to go to next. */
178 input_program_source_read (struct case_source *source,
180 write_case_func *write_case,
181 write_case_data wc_data)
183 struct input_program_pgm *inp = source->aux;
186 /* Nonzero if there were any END CASE commands in the set of
187 transformations. If so, we don't automatically write out
191 /* FIXME? This is the number of cases sent out of the input
192 program, not the number of cases written to the procedure.
193 The difference should only show up in $CASENUM in COMPUTE.
194 We should check behavior against SPSS. */
195 int cases_written = 0;
197 assert (inp != NULL);
199 /* Figure end_case. */
200 for (i = 0; i < f_trns; i++)
201 if (t_trns[i]->proc == end_case_trns_proc)
204 /* FIXME: This is an ugly kluge. */
205 for (i = 0; i < f_trns; i++)
206 if (t_trns[i]->proc == repeating_data_trns_proc)
207 repeating_data_set_write_case (t_trns[i], write_case, wc_data);
212 /* Perform transformations on `blank' case. */
213 for (i = 0; i < f_trns; )
215 int code; /* Return value of last-called transformation. */
217 if (t_trns[i]->proc == end_case_trns_proc)
220 if (!write_case (wc_data))
227 code = t_trns[i]->proc (t_trns[i], c, cases_written + 1);
243 /* Write the case if appropriate. */
247 if (!write_case (wc_data))
251 /* Blank out the case for the next iteration. */
258 /* Destroys an INPUT PROGRAM source. */
260 input_program_source_destroy (struct case_source *source)
262 struct input_program_pgm *inp = source->aux;
264 cancel_transformations ();
273 const struct case_source_class input_program_source_class =
277 input_program_source_read,
278 input_program_source_destroy,
284 struct trns_header *t;
286 if (!case_source_is_class (vfm_source, &input_program_source_class))
288 msg (SE, _("This command may only be executed between INPUT PROGRAM "
289 "and END INPUT PROGRAM."));
293 t = xmalloc (sizeof *t);
294 t->proc = end_case_trns_proc;
296 add_transformation ((struct trns_header *) t);
298 return lex_end_of_command ();
301 /* Should never be called, because this is handled in
302 input_program_source_read(). */
304 end_case_trns_proc (struct trns_header *t UNUSED, struct ccase * c UNUSED,
311 /* REREAD transformation. */
314 struct trns_header h;
316 struct dfm_reader *reader; /* File to move file pointer back on. */
317 struct expression *column; /* Column to reset file pointer to. */
320 /* Parses REREAD command. */
324 struct file_handle *fh; /* File to be re-read. */
325 struct expression *e; /* Expression for column to set. */
326 struct reread_trns *t; /* Created transformation. */
332 if (lex_match_id ("COLUMN"))
338 msg (SE, _("COLUMN subcommand multiply specified."));
343 e = expr_parse (EXPR_NUMERIC);
347 else if (lex_match_id ("FILE"))
365 t = xmalloc (sizeof *t);
366 t->h.proc = reread_trns_proc;
367 t->h.free = reread_trns_free;
368 t->reader = dfm_open_reader (fh);
370 add_transformation ((struct trns_header *) t);
375 /* Executes a REREAD transformation. */
377 reread_trns_proc (struct trns_header * pt, struct ccase * c,
380 struct reread_trns *t = (struct reread_trns *) pt;
382 if (t->column == NULL)
383 dfm_reread_record (t->reader, 1);
388 expr_evaluate (t->column, c, case_num, &column);
389 if (!finite (column.f) || column.f < 1)
391 msg (SE, _("REREAD: Column numbers must be positive finite "
392 "numbers. Column set to 1."));
393 dfm_reread_record (t->reader, 1);
396 dfm_reread_record (t->reader, column.f);
401 /* Frees a REREAD transformation. */
403 reread_trns_free (struct trns_header *t_)
405 struct reread_trns *t = (struct reread_trns *) t_;
406 expr_free (t->column);
407 dfm_close_reader (t->reader);
410 /* Parses END FILE command. */
414 struct trns_header *t;
416 if (!case_source_is_class (vfm_source, &input_program_source_class))
418 msg (SE, _("This command may only be executed between INPUT PROGRAM "
419 "and END INPUT PROGRAM."));
423 t = xmalloc (sizeof *t);
424 t->proc = end_file_trns_proc;
426 add_transformation ((struct trns_header *) t);
428 return lex_end_of_command ();
431 /* Executes an END FILE transformation. */
433 end_file_trns_proc (struct trns_header * t UNUSED, struct ccase * c UNUSED,