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 int end_case_trns_proc (struct trns_header *, struct ccase *);
56 static int end_file_trns_proc (struct trns_header * t, struct ccase * c);
57 static int reread_trns_proc (struct trns_header *, struct ccase *);
58 static void reread_trns_free (struct trns_header *);
61 cmd_input_program (void)
63 lex_match_id ("INPUT");
64 lex_match_id ("PROGRAM");
67 vfm_source = create_case_source (&input_program_source_class, NULL);
69 return lex_end_of_command ();
73 cmd_end_input_program (void)
75 struct input_program_pgm *inp;
79 lex_match_id ("INPUT");
80 lex_match_id ("PROGRAM");
82 if (!case_source_is_class (vfm_source, &input_program_source_class))
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 transformations and
93 ordinary transformations. */
96 /* Figure out how to initialize temp_case. */
97 inp = xmalloc (sizeof *inp);
98 inp->init_cnt = dict_get_next_value_idx (default_dict);
99 inp->init = xmalloc (inp->init_cnt * sizeof *inp->init);
100 for (i = 0; i < inp->init_cnt; i++)
102 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
104 struct variable *var = dict_get_var (default_dict, i);
105 enum value_init_type value_init;
108 value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
109 value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE;
111 for (j = 0; j < var->nv; j++)
112 inp->init[j + var->fv] = value_init;
114 for (i = 0; i < inp->init_cnt; i++)
115 assert (inp->init[i] != -1);
117 /* Put inp into vfm_source for later use. */
118 vfm_source->aux = inp;
120 return lex_end_of_command ();
123 /* Initializes temp_case. Called before the first case is read. */
125 init_case (struct input_program_pgm *inp)
129 for (i = 0; i < inp->init_cnt; i++)
130 switch (inp->init[i])
132 case INP_NUMERIC | INP_INIT_ONCE:
133 temp_case->data[i].f = 0.0;
135 case INP_NUMERIC | INP_REINIT:
136 temp_case->data[i].f = SYSMIS;
138 case INP_STRING | INP_INIT_ONCE:
139 case INP_STRING | INP_REINIT:
140 memset (temp_case->data[i].s, ' ', sizeof temp_case->data[i].s);
147 /* Clears temp_case. Called between reading successive records. */
149 clear_case (struct input_program_pgm *inp)
153 for (i = 0; i < inp->init_cnt; i++)
154 switch (inp->init[i])
156 case INP_NUMERIC | INP_INIT_ONCE:
158 case INP_NUMERIC | INP_REINIT:
159 temp_case->data[i].f = SYSMIS;
161 case INP_STRING | INP_INIT_ONCE:
163 case INP_STRING | INP_REINIT:
164 memset (temp_case->data[i].s, ' ', sizeof temp_case->data[i].s);
171 /* Executes each transformation in turn on a `blank' case. When a
172 transformation fails, returning -2, then that's the end of the
173 file. -1 means go on to the next transformation. Otherwise the
174 return value is the index of the transformation to go to next. */
176 input_program_source_read (struct case_source *source,
177 write_case_func *write_case,
178 write_case_data wc_data)
180 struct input_program_pgm *inp = source->aux;
183 /* Nonzero if there were any END CASE commands in the set of
184 transformations. If so, we don't automatically write out
188 assert (inp != NULL);
190 /* Figure end_case. */
191 for (i = 0; i < f_trns; i++)
192 if (t_trns[i]->proc == end_case_trns_proc)
195 /* FIXME: This code should not be necessary. It is an ugly
197 for (i = 0; i < f_trns; i++)
198 if (t_trns[i]->proc == repeating_data_trns_proc)
199 repeating_data_set_write_case (t_trns[i], write_case, wc_data);
204 /* Index of current transformation. */
207 /* Return value of last-called transformation. */
210 debug_printf (("input-program: "));
212 /* Perform transformations on `blank' case. */
213 for (i = 0; i < f_trns;)
217 if (t_trns[i]->proc == end_case_trns_proc)
221 if (t_trns[i]->proc == end_case_trns_proc)
223 if (!write_case (wc_data))
230 code = t_trns[i]->proc (t_trns[i], temp_case);
251 /* Write the case if appropriate. */
253 if (!write_case (wc_data))
256 /* Blank out the case for the next iteration. */
263 input_program_source_destroy (struct case_source *source)
265 struct input_program_pgm *inp = source->aux;
267 cancel_transformations ();
276 const struct case_source_class input_program_source_class =
279 input_program_source_read,
280 input_program_source_destroy,
286 struct trns_header *t;
288 lex_match_id ("END");
289 lex_match_id ("CASE");
291 if (!case_source_is_class (vfm_source, &input_program_source_class))
293 msg (SE, _("This command may only be executed between INPUT PROGRAM "
294 "and END INPUT PROGRAM."));
298 t = xmalloc (sizeof *t);
299 t->proc = end_case_trns_proc;
301 add_transformation ((struct trns_header *) t);
303 return lex_end_of_command ();
307 end_case_trns_proc (struct trns_header *t UNUSED, struct ccase * c UNUSED)
312 /* REREAD transformation. */
315 struct trns_header h;
317 struct file_handle *handle; /* File to move file pointer back on. */
318 struct expression *column; /* Column to reset file pointer to. */
321 /* Parses REREAD command. */
325 /* File to be re-read. */
326 struct file_handle *h;
328 /* Expression for column to set file pointer to. */
329 struct expression *e;
331 /* Created transformation. */
332 struct reread_trns *t;
334 lex_match_id ("REREAD");
340 if (lex_match_id ("COLUMN"))
346 msg (SE, _("COLUMN subcommand multiply specified."));
351 e = expr_parse (PXP_NUMERIC);
355 else if (lex_match_id ("FILE"))
360 lex_error (_("expecting file handle name"));
364 h = fh_get_handle_by_name (tokid);
379 t = xmalloc (sizeof *t);
380 t->h.proc = reread_trns_proc;
381 t->h.free = reread_trns_free;
384 add_transformation ((struct trns_header *) t);
390 reread_trns_proc (struct trns_header * pt, struct ccase * c)
392 struct reread_trns *t = (struct reread_trns *) pt;
394 if (t->column == NULL)
395 dfm_bkwd_record (t->handle, 1);
400 expr_evaluate (t->column, c, &column);
401 if (!finite (column.f) || column.f < 1)
403 msg (SE, _("REREAD: Column numbers must be positive finite "
404 "numbers. Column set to 1."));
405 dfm_bkwd_record (t->handle, 1);
408 dfm_bkwd_record (t->handle, column.f);
414 reread_trns_free (struct trns_header * t)
416 expr_free (((struct reread_trns *) t)->column);
419 /* Parses END FILE command. */
423 struct trns_header *t;
425 lex_match_id ("END");
426 lex_match_id ("FILE");
428 if (!case_source_is_class (vfm_source, &input_program_source_class))
430 msg (SE, _("This command may only be executed between INPUT PROGRAM "
431 "and END INPUT PROGRAM."));
435 t = xmalloc (sizeof *t);
436 t->proc = end_file_trns_proc;
438 add_transformation ((struct trns_header *) t);
440 return lex_end_of_command ();
444 end_file_trns_proc (struct trns_header * t UNUSED, struct ccase * c UNUSED)
447 printf ("END FILE\n");