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., 51 Franklin Street, Fifth Floor, Boston, MA
28 #include "data-list.h"
29 #include "data-reader.h"
30 #include "dictionary.h"
32 #include "expressions/public.h"
33 #include "file-handle.h"
38 #include "procedure.h"
41 #define _(msgid) gettext (msgid)
43 #include "debug-print.h"
45 /* Indicates how a `union value' should be initialized. */
48 INP_NUMERIC = 01, /* Numeric. */
49 INP_STRING = 0, /* String. */
51 INP_INIT_ONCE = 02, /* Initialize only once. */
52 INP_REINIT = 0, /* Reinitialize for each iteration. */
55 struct input_program_pgm
57 enum value_init_type *init; /* How to initialize each `union value'. */
58 size_t init_cnt; /* Number of elements in inp_init. */
59 size_t case_size; /* Size of case in bytes. */
62 static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
63 static trns_free_func reread_trns_free;
66 cmd_input_program (void)
70 /* FIXME: we shouldn't do this here, but I'm afraid that other
71 code will check the class of vfm_source. */
72 vfm_source = create_case_source (&input_program_source_class, NULL);
74 return lex_end_of_command ();
78 cmd_end_input_program (void)
80 struct input_program_pgm *inp;
83 if (!case_source_is_class (vfm_source, &input_program_source_class))
85 msg (SE, _("No matching INPUT PROGRAM command."));
86 return CMD_CASCADING_FAILURE;
89 if (dict_get_next_value_idx (default_dict) == 0)
90 msg (SW, _("No data-input or transformation commands specified "
91 "between INPUT PROGRAM and END INPUT PROGRAM."));
93 /* Mark the boundary between INPUT PROGRAM transformations and
94 ordinary transformations. */
97 /* Figure out how to initialize each input case. */
98 inp = xmalloc (sizeof *inp);
99 inp->init_cnt = dict_get_next_value_idx (default_dict);
100 inp->init = xnmalloc (inp->init_cnt, sizeof *inp->init);
101 for (i = 0; i < inp->init_cnt; i++)
103 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
105 struct variable *var = dict_get_var (default_dict, i);
106 enum value_init_type value_init;
109 value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
110 value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE;
112 for (j = 0; j < var->nv; j++)
113 inp->init[j + var->fv] = value_init;
115 for (i = 0; i < inp->init_cnt; i++)
116 assert (inp->init[i] != -1);
117 inp->case_size = dict_get_case_size (default_dict);
119 /* Put inp into vfm_source for later use. */
120 vfm_source->aux = inp;
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.
174 Returns true if successful, false if an I/O error occurred. */
176 input_program_source_read (struct case_source *source,
178 write_case_func *write_case,
179 write_case_data wc_data)
181 struct input_program_pgm *inp = source->aux;
184 /* Nonzero if there were any END CASE commands in the set of
185 transformations. If so, we don't automatically write out
189 /* FIXME? This is the number of cases sent out of the input
190 program, not the number of cases written to the procedure.
191 The difference should only show up in $CASENUM in COMPUTE.
192 We should check behavior against SPSS. */
193 int cases_written = 0;
195 assert (inp != NULL);
197 /* Figure end_case. */
198 for (i = 0; i < f_trns; i++)
199 if (t_trns[i].proc == end_case_trns_proc)
202 /* FIXME: This is an ugly kluge. */
203 for (i = 0; i < f_trns; i++)
204 if (t_trns[i].proc == repeating_data_trns_proc)
205 repeating_data_set_write_case (t_trns[i].private, write_case, wc_data);
210 /* Perform transformations on `blank' case. */
211 for (i = 0; i < f_trns; )
215 if (t_trns[i].proc == end_case_trns_proc)
218 if (!write_case (wc_data))
225 code = t_trns[i].proc (t_trns[i].private, c, cases_written + 1);
250 /* Write the case if appropriate. */
254 if (!write_case (wc_data))
258 /* Blank out the case for the next iteration. */
264 /* Destroys an INPUT PROGRAM source. */
266 input_program_source_destroy (struct case_source *source)
268 struct input_program_pgm *inp = source->aux;
270 cancel_transformations ();
279 const struct case_source_class input_program_source_class =
283 input_program_source_read,
284 input_program_source_destroy,
290 if (!case_source_is_class (vfm_source, &input_program_source_class))
292 msg (SE, _("This command may only be executed between INPUT PROGRAM "
293 "and END INPUT PROGRAM."));
294 return CMD_CASCADING_FAILURE;
297 add_transformation (end_case_trns_proc, NULL, NULL);
299 return lex_end_of_command ();
302 /* Should never be called, because this is handled in
303 input_program_source_read(). */
305 end_case_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
311 /* REREAD transformation. */
314 struct dfm_reader *reader; /* File to move file pointer back on. */
315 struct expression *column; /* Column to reset file pointer to. */
318 /* Parses REREAD command. */
322 struct file_handle *fh; /* File to be re-read. */
323 struct expression *e; /* Expression for column to set. */
324 struct reread_trns *t; /* Created transformation. */
326 fh = fh_get_default_handle ();
330 if (lex_match_id ("COLUMN"))
336 msg (SE, _("COLUMN subcommand multiply specified."));
338 return CMD_CASCADING_FAILURE;
341 e = expr_parse (default_dict, EXPR_NUMBER);
343 return CMD_CASCADING_FAILURE;
345 else if (lex_match_id ("FILE"))
348 fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
352 return CMD_CASCADING_FAILURE;
363 t = xmalloc (sizeof *t);
364 t->reader = dfm_open_reader (fh);
366 add_transformation (reread_trns_proc, reread_trns_free, t);
371 /* Executes a REREAD transformation. */
373 reread_trns_proc (void *t_, struct ccase *c, int case_num)
375 struct reread_trns *t = t_;
377 if (t->column == NULL)
378 dfm_reread_record (t->reader, 1);
381 double column = expr_evaluate_num (t->column, c, case_num);
382 if (!finite (column) || column < 1)
384 msg (SE, _("REREAD: Column numbers must be positive finite "
385 "numbers. Column set to 1."));
386 dfm_reread_record (t->reader, 1);
389 dfm_reread_record (t->reader, column);
391 return TRNS_CONTINUE;
394 /* Frees a REREAD transformation.
395 Returns true if successful, false if an I/O error occurred. */
397 reread_trns_free (void *t_)
399 struct reread_trns *t = t_;
400 expr_free (t->column);
401 dfm_close_reader (t->reader);
405 /* Parses END FILE command. */
409 if (!case_source_is_class (vfm_source, &input_program_source_class))
411 msg (SE, _("This command may only be executed between INPUT PROGRAM "
412 "and END INPUT PROGRAM."));
413 return CMD_CASCADING_FAILURE;
416 add_transformation (end_file_trns_proc, NULL, NULL);
418 return lex_end_of_command ();
421 /* Executes an END FILE transformation. */
423 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
426 return TRNS_END_FILE;