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
29 #include "file-handle.h"
36 #include "debug-print.h"
38 /* Indicates how a `union value' should be initialized. */
41 INP_NUMERIC = 01, /* Numeric. */
42 INP_STRING = 0, /* String. */
44 INP_INIT_ONCE = 02, /* Initialize only once. */
45 INP_REINIT = 0, /* Reinitialize for each iteration. */
48 /* Array that tells INPUT PROGRAM how to initialize each `union
50 static enum value_init_type *inp_init;
52 /* Number of bytes allocated for inp_init. */
53 static size_t inp_nval;
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 = &input_program_source;
69 return lex_end_of_command ();
73 cmd_end_input_program (void)
78 lex_match_id ("INPUT");
79 lex_match_id ("PROGRAM");
81 if (vfm_source != &input_program_source)
83 msg (SE, _("No matching INPUT PROGRAM command."));
87 if (dict_get_value_cnt (default_dict) == 0)
88 msg (SW, _("No data-input or transformation commands specified "
89 "between INPUT PROGRAM and END INPUT PROGRAM."));
91 /* Mark the boundary between INPUT PROGRAM and more-mundane
95 /* Figure out how to initialize temp_case. */
96 inp_nval = dict_get_value_cnt (default_dict);
97 inp_init = xmalloc (inp_nval * sizeof *inp_init);
98 for (i = 0; i < inp_nval; 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->left ? INP_INIT_ONCE : INP_REINIT;
109 for (j = 0; j < var->nv; j++)
110 inp_init[j + var->fv] = value_init;
112 for (i = 0; i < inp_nval; i++)
113 assert (inp_init[i] != -1);
115 return lex_end_of_command ();
118 /* Initializes temp_case. Called before the first case is read. */
124 for (i = 0; i < inp_nval; i++)
127 case INP_NUMERIC | INP_INIT_ONCE:
128 temp_case->data[i].f = 0.0;
130 case INP_NUMERIC | INP_REINIT:
131 temp_case->data[i].f = SYSMIS;
133 case INP_STRING | INP_INIT_ONCE:
134 case INP_STRING | INP_REINIT:
135 memset (temp_case->data[i].s, ' ', sizeof temp_case->data[i].s);
142 /* Clears temp_case. Called between reading successive records. */
148 for (i = 0; i < inp_nval; i++)
151 case INP_NUMERIC | INP_INIT_ONCE:
153 case INP_NUMERIC | INP_REINIT:
154 temp_case->data[i].f = SYSMIS;
156 case INP_STRING | INP_INIT_ONCE:
158 case INP_STRING | INP_REINIT:
159 memset (temp_case->data[i].s, ' ', sizeof temp_case->data[i].s);
166 /* Executes each transformation in turn on a `blank' case. When a
167 transformation fails, returning -2, then that's the end of the
168 file. -1 means go on to the next transformation. Otherwise the
169 return value is the index of the transformation to go to next. */
171 input_program_source_read (void)
175 /* Nonzero if there were any END CASE commands in the set of
179 /* We don't automatically write out cases if the user took over
181 for (i = 0; i < f_trns; i++)
182 if (t_trns[i]->proc == end_case_trns_proc)
188 /* Index of current transformation. */
191 /* Return value of last-called transformation. */
194 debug_printf (("input-program: "));
196 /* Perform transformations on `blank' case. */
197 for (i = 0; i < f_trns;)
201 if (t_trns[i]->proc == end_case_trns_proc)
204 code = t_trns[i]->proc (t_trns[i], temp_case);
225 /* Write the case if appropriate. */
230 /* Blank out the case for the next iteration. */
237 input_program_source_destroy_source (void)
239 cancel_transformations ();
244 struct case_stream input_program_source =
247 input_program_source_read,
250 input_program_source_destroy_source,
258 struct trns_header *t;
260 lex_match_id ("END");
261 lex_match_id ("CASE");
263 if (vfm_source != &input_program_source)
265 msg (SE, _("This command may only be executed between INPUT PROGRAM "
266 "and END INPUT PROGRAM."));
270 t = xmalloc (sizeof *t);
271 t->proc = end_case_trns_proc;
273 add_transformation ((struct trns_header *) t);
275 return lex_end_of_command ();
279 end_case_trns_proc (struct trns_header *t unused, struct ccase * c unused)
282 printf ("END CASE\n");
290 /* REREAD transformation. */
293 struct trns_header h;
295 struct file_handle *handle; /* File to move file pointer back on. */
296 struct expression *column; /* Column to reset file pointer to. */
299 /* Parses REREAD command. */
303 /* File to be re-read. */
304 struct file_handle *h;
306 /* Expression for column to set file pointer to. */
307 struct expression *e;
309 /* Created transformation. */
310 struct reread_trns *t;
312 lex_match_id ("REREAD");
318 if (lex_match_id ("COLUMN"))
324 msg (SE, _("COLUMN subcommand multiply specified."));
329 e = expr_parse (PXP_NUMERIC);
333 else if (lex_match_id ("FILE"))
338 lex_error (_("expecting file handle name"));
342 h = fh_get_handle_by_name (tokid);
357 t = xmalloc (sizeof *t);
358 t->h.proc = reread_trns_proc;
359 t->h.free = reread_trns_free;
362 add_transformation ((struct trns_header *) t);
368 reread_trns_proc (struct trns_header * pt, struct ccase * c)
370 struct reread_trns *t = (struct reread_trns *) pt;
372 if (t->column == NULL)
373 dfm_bkwd_record (t->handle, 1);
378 expr_evaluate (t->column, c, &column);
379 if (!finite (column.f) || column.f < 1)
381 msg (SE, _("REREAD: Column numbers must be positive finite "
382 "numbers. Column set to 1."));
383 dfm_bkwd_record (t->handle, 1);
386 dfm_bkwd_record (t->handle, column.f);
392 reread_trns_free (struct trns_header * t)
394 expr_free (((struct reread_trns *) t)->column);
397 /* Parses END FILE command. */
401 struct trns_header *t;
403 lex_match_id ("END");
404 lex_match_id ("FILE");
406 if (vfm_source != &input_program_source)
408 msg (SE, _("This command may only be executed between INPUT PROGRAM "
409 "and END INPUT PROGRAM."));
413 t = xmalloc (sizeof *t);
414 t->proc = end_file_trns_proc;
416 add_transformation ((struct trns_header *) t);
418 return lex_end_of_command ();
422 end_file_trns_proc (struct trns_header * t unused, struct ccase * c unused)
425 printf ("END FILE\n");