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 /* Array that tells INPUT PROGRAM how to initialize each `union
51 static enum value_init_type *inp_init;
53 /* Number of bytes allocated for inp_init. */
54 static size_t inp_nval;
56 static int end_case_trns_proc (struct trns_header *, struct ccase *);
57 static int end_file_trns_proc (struct trns_header * t, struct ccase * c);
58 static int reread_trns_proc (struct trns_header *, struct ccase *);
59 static void reread_trns_free (struct trns_header *);
62 cmd_input_program (void)
64 lex_match_id ("INPUT");
65 lex_match_id ("PROGRAM");
68 vfm_source = &input_program_source;
70 return lex_end_of_command ();
74 cmd_end_input_program (void)
79 lex_match_id ("INPUT");
80 lex_match_id ("PROGRAM");
82 if (vfm_source != &input_program_source)
84 msg (SE, _("No matching INPUT PROGRAM command."));
88 if (dict_get_next_value_idx (default_dict) == 0)
89 msg (SW, _("No data-input or transformation commands specified "
90 "between INPUT PROGRAM and END INPUT PROGRAM."));
92 /* Mark the boundary between INPUT PROGRAM and more-mundane
96 /* Figure out how to initialize temp_case. */
97 inp_nval = dict_get_next_value_idx (default_dict);
98 inp_init = xmalloc (inp_nval * sizeof *inp_init);
99 for (i = 0; i < inp_nval; i++)
101 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
103 struct variable *var = dict_get_var (default_dict, i);
104 enum value_init_type value_init;
107 value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
108 value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE;
110 for (j = 0; j < var->nv; j++)
111 inp_init[j + var->fv] = value_init;
113 for (i = 0; i < inp_nval; i++)
114 assert (inp_init[i] != -1);
116 return lex_end_of_command ();
119 /* Initializes temp_case. Called before the first case is read. */
125 for (i = 0; i < inp_nval; i++)
128 case INP_NUMERIC | INP_INIT_ONCE:
129 temp_case->data[i].f = 0.0;
131 case INP_NUMERIC | INP_REINIT:
132 temp_case->data[i].f = SYSMIS;
134 case INP_STRING | INP_INIT_ONCE:
135 case INP_STRING | INP_REINIT:
136 memset (temp_case->data[i].s, ' ', sizeof temp_case->data[i].s);
143 /* Clears temp_case. Called between reading successive records. */
149 for (i = 0; i < inp_nval; i++)
152 case INP_NUMERIC | INP_INIT_ONCE:
154 case INP_NUMERIC | INP_REINIT:
155 temp_case->data[i].f = SYSMIS;
157 case INP_STRING | INP_INIT_ONCE:
159 case INP_STRING | INP_REINIT:
160 memset (temp_case->data[i].s, ' ', sizeof temp_case->data[i].s);
167 /* Executes each transformation in turn on a `blank' case. When a
168 transformation fails, returning -2, then that's the end of the
169 file. -1 means go on to the next transformation. Otherwise the
170 return value is the index of the transformation to go to next. */
172 input_program_source_read (write_case_func *write_case,
173 write_case_data wc_data)
177 /* Nonzero if there were any END CASE commands in the set of
181 /* We don't automatically write out cases if the user took over
183 for (i = 0; i < f_trns; i++)
184 if (t_trns[i]->proc == end_case_trns_proc)
187 /* FIXME: This code should not be necessary. It is an ugly
189 for (i = 0; i < f_trns; i++)
190 if (t_trns[i]->proc == repeating_data_trns_proc)
191 repeating_data_set_write_case (t_trns[i], write_case, wc_data);
196 /* Index of current transformation. */
199 /* Return value of last-called transformation. */
202 debug_printf (("input-program: "));
204 /* Perform transformations on `blank' case. */
205 for (i = 0; i < f_trns;)
209 if (t_trns[i]->proc == end_case_trns_proc)
213 if (t_trns[i]->proc == end_case_trns_proc)
215 if (!write_case (wc_data))
222 code = t_trns[i]->proc (t_trns[i], temp_case);
243 /* Write the case if appropriate. */
245 if (!write_case (wc_data))
248 /* Blank out the case for the next iteration. */
255 input_program_source_destroy_source (void)
257 cancel_transformations ();
262 struct case_stream input_program_source =
265 input_program_source_read,
268 input_program_source_destroy_source,
276 struct trns_header *t;
278 lex_match_id ("END");
279 lex_match_id ("CASE");
281 if (vfm_source != &input_program_source)
283 msg (SE, _("This command may only be executed between INPUT PROGRAM "
284 "and END INPUT PROGRAM."));
288 t = xmalloc (sizeof *t);
289 t->proc = end_case_trns_proc;
291 add_transformation ((struct trns_header *) t);
293 return lex_end_of_command ();
297 end_case_trns_proc (struct trns_header *t UNUSED, struct ccase * c UNUSED)
302 /* REREAD transformation. */
305 struct trns_header h;
307 struct file_handle *handle; /* File to move file pointer back on. */
308 struct expression *column; /* Column to reset file pointer to. */
311 /* Parses REREAD command. */
315 /* File to be re-read. */
316 struct file_handle *h;
318 /* Expression for column to set file pointer to. */
319 struct expression *e;
321 /* Created transformation. */
322 struct reread_trns *t;
324 lex_match_id ("REREAD");
330 if (lex_match_id ("COLUMN"))
336 msg (SE, _("COLUMN subcommand multiply specified."));
341 e = expr_parse (PXP_NUMERIC);
345 else if (lex_match_id ("FILE"))
350 lex_error (_("expecting file handle name"));
354 h = fh_get_handle_by_name (tokid);
369 t = xmalloc (sizeof *t);
370 t->h.proc = reread_trns_proc;
371 t->h.free = reread_trns_free;
374 add_transformation ((struct trns_header *) t);
380 reread_trns_proc (struct trns_header * pt, struct ccase * c)
382 struct reread_trns *t = (struct reread_trns *) pt;
384 if (t->column == NULL)
385 dfm_bkwd_record (t->handle, 1);
390 expr_evaluate (t->column, c, &column);
391 if (!finite (column.f) || column.f < 1)
393 msg (SE, _("REREAD: Column numbers must be positive finite "
394 "numbers. Column set to 1."));
395 dfm_bkwd_record (t->handle, 1);
398 dfm_bkwd_record (t->handle, column.f);
404 reread_trns_free (struct trns_header * t)
406 expr_free (((struct reread_trns *) t)->column);
409 /* Parses END FILE command. */
413 struct trns_header *t;
415 lex_match_id ("END");
416 lex_match_id ("FILE");
418 if (vfm_source != &input_program_source)
420 msg (SE, _("This command may only be executed between INPUT PROGRAM "
421 "and END INPUT PROGRAM."));
425 t = xmalloc (sizeof *t);
426 t->proc = end_file_trns_proc;
428 add_transformation ((struct trns_header *) t);
430 return lex_end_of_command ();
434 end_file_trns_proc (struct trns_header * t UNUSED, struct ccase * c UNUSED)
437 printf ("END FILE\n");