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
26 #include "data-list.h"
30 #include "file-handle.h"
37 #include "debug-print.h"
39 /* Indicates how a `union value' should be initialized. */
42 INP_NUMERIC = 01, /* Numeric. */
43 INP_STRING = 0, /* String. */
45 INP_INIT_ONCE = 02, /* Initialize only once. */
46 INP_REINIT = 0, /* Reinitialize for each iteration. */
49 struct input_program_pgm
51 enum value_init_type *init; /* How to initialize each `union value'. */
52 size_t init_cnt; /* Number of elements in inp_init. */
55 static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
56 static trns_free_func reread_trns_free;
59 cmd_input_program (void)
61 lex_match_id ("INPUT");
62 lex_match_id ("PROGRAM");
65 vfm_source = create_case_source (&input_program_source_class, NULL);
67 return lex_end_of_command ();
71 cmd_end_input_program (void)
73 struct input_program_pgm *inp;
77 lex_match_id ("INPUT");
78 lex_match_id ("PROGRAM");
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 temp_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);
115 /* Put inp into vfm_source for later use. */
116 vfm_source->aux = inp;
118 return lex_end_of_command ();
121 /* Initializes temp_case. Called before the first case is read. */
123 init_case (struct input_program_pgm *inp)
127 for (i = 0; i < inp->init_cnt; i++)
128 switch (inp->init[i])
130 case INP_NUMERIC | INP_INIT_ONCE:
131 temp_case->data[i].f = 0.0;
133 case INP_NUMERIC | INP_REINIT:
134 temp_case->data[i].f = SYSMIS;
136 case INP_STRING | INP_INIT_ONCE:
137 case INP_STRING | INP_REINIT:
138 memset (temp_case->data[i].s, ' ', sizeof temp_case->data[i].s);
145 /* Clears temp_case. Called between reading successive records. */
147 clear_case (struct input_program_pgm *inp)
151 for (i = 0; i < inp->init_cnt; i++)
152 switch (inp->init[i])
154 case INP_NUMERIC | INP_INIT_ONCE:
156 case INP_NUMERIC | INP_REINIT:
157 temp_case->data[i].f = SYSMIS;
159 case INP_STRING | INP_INIT_ONCE:
161 case INP_STRING | INP_REINIT:
162 memset (temp_case->data[i].s, ' ', sizeof temp_case->data[i].s);
169 /* Executes each transformation in turn on a `blank' case. When a
170 transformation fails, returning -2, then that's the end of the
171 file. -1 means go on to the next transformation. Otherwise the
172 return value is the index of the transformation to go to next. */
174 input_program_source_read (struct case_source *source,
175 write_case_func *write_case,
176 write_case_data wc_data)
178 struct input_program_pgm *inp = source->aux;
181 /* Nonzero if there were any END CASE commands in the set of
182 transformations. If so, we don't automatically write out
186 /* FIXME? This is the number of cases sent out of the input
187 program, not the number of cases written to the procedure.
188 The difference should only show up in $CASENUM in COMPUTE.
189 We should check behavior against SPSS. */
190 int cases_written = 0;
192 assert (inp != NULL);
194 /* Figure end_case. */
195 for (i = 0; i < f_trns; i++)
196 if (t_trns[i]->proc == end_case_trns_proc)
199 /* FIXME: This code should not be necessary. It is an ugly
201 for (i = 0; i < f_trns; i++)
202 if (t_trns[i]->proc == repeating_data_trns_proc)
203 repeating_data_set_write_case (t_trns[i], write_case, wc_data);
208 /* Index of current transformation. */
211 /* Return value of last-called transformation. */
214 debug_printf (("input-program: "));
216 /* Perform transformations on `blank' case. */
217 for (i = 0; i < f_trns;)
221 if (t_trns[i]->proc == end_case_trns_proc)
225 if (t_trns[i]->proc == end_case_trns_proc)
228 if (!write_case (wc_data))
235 code = t_trns[i]->proc (t_trns[i], temp_case, cases_written + 1);
256 /* Write the case if appropriate. */
258 if (!write_case (wc_data))
261 /* Blank out the case for the next iteration. */
268 input_program_source_destroy (struct case_source *source)
270 struct input_program_pgm *inp = source->aux;
272 cancel_transformations ();
281 const struct case_source_class input_program_source_class =
285 input_program_source_read,
286 input_program_source_destroy,
292 struct trns_header *t;
294 lex_match_id ("END");
295 lex_match_id ("CASE");
297 if (!case_source_is_class (vfm_source, &input_program_source_class))
299 msg (SE, _("This command may only be executed between INPUT PROGRAM "
300 "and END INPUT PROGRAM."));
304 t = xmalloc (sizeof *t);
305 t->proc = end_case_trns_proc;
307 add_transformation ((struct trns_header *) t);
309 return lex_end_of_command ();
313 end_case_trns_proc (struct trns_header *t UNUSED, struct ccase * c UNUSED,
319 /* REREAD transformation. */
322 struct trns_header h;
324 struct file_handle *handle; /* File to move file pointer back on. */
325 struct expression *column; /* Column to reset file pointer to. */
328 /* Parses REREAD command. */
332 /* File to be re-read. */
333 struct file_handle *h;
335 /* Expression for column to set file pointer to. */
336 struct expression *e;
338 /* Created transformation. */
339 struct reread_trns *t;
341 lex_match_id ("REREAD");
347 if (lex_match_id ("COLUMN"))
353 msg (SE, _("COLUMN subcommand multiply specified."));
358 e = expr_parse (PXP_NUMERIC);
362 else if (lex_match_id ("FILE"))
367 lex_error (_("expecting file handle name"));
371 h = fh_get_handle_by_name (tokid);
386 t = xmalloc (sizeof *t);
387 t->h.proc = reread_trns_proc;
388 t->h.free = reread_trns_free;
391 add_transformation ((struct trns_header *) t);
397 reread_trns_proc (struct trns_header * pt, struct ccase * c,
400 struct reread_trns *t = (struct reread_trns *) pt;
402 if (t->column == NULL)
403 dfm_bkwd_record (t->handle, 1);
408 expr_evaluate (t->column, c, case_num, &column);
409 if (!finite (column.f) || column.f < 1)
411 msg (SE, _("REREAD: Column numbers must be positive finite "
412 "numbers. Column set to 1."));
413 dfm_bkwd_record (t->handle, 1);
416 dfm_bkwd_record (t->handle, column.f);
422 reread_trns_free (struct trns_header * t)
424 expr_free (((struct reread_trns *) t)->column);
427 /* Parses END FILE command. */
431 struct trns_header *t;
433 lex_match_id ("END");
434 lex_match_id ("FILE");
436 if (!case_source_is_class (vfm_source, &input_program_source_class))
438 msg (SE, _("This command may only be executed between INPUT PROGRAM "
439 "and END INPUT PROGRAM."));
443 t = xmalloc (sizeof *t);
444 t->proc = end_file_trns_proc;
446 add_transformation ((struct trns_header *) t);
448 return lex_end_of_command ();
452 end_file_trns_proc (struct trns_header * t UNUSED, struct ccase * c UNUSED,
456 printf ("END FILE\n");