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
22 #include <language/data-io/inpt-pgm.h>
27 #include <data/case-source.h>
28 #include <data/case.h>
29 #include <data/case-source.h>
30 #include <data/dictionary.h>
31 #include <data/variable.h>
32 #include <language/command.h>
33 #include <language/data-io/data-reader.h>
34 #include <language/data-io/file-handle.h>
35 #include <language/expressions/public.h>
36 #include <language/lexer/lexer.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/compiler.h>
39 #include <libpspp/message.h>
40 #include <libpspp/message.h>
41 #include <libpspp/misc.h>
42 #include <libpspp/str.h>
43 #include <procedure.h>
46 #define _(msgid) gettext (msgid)
48 /* Private result codes for use within INPUT PROGRAM. */
49 enum cmd_result_extensions
51 CMD_END_INPUT_PROGRAM = CMD_PRIVATE_FIRST,
55 /* Indicates how a `union value' should be initialized. */
58 INP_NUMERIC = 01, /* Numeric. */
59 INP_STRING = 0, /* String. */
61 INP_INIT_ONCE = 02, /* Initialize only once. */
62 INP_REINIT = 0, /* Reinitialize for each iteration. */
65 struct input_program_pgm
67 size_t case_nr; /* Incremented by END CASE transformation. */
68 write_case_func *write_case;/* Called by END CASE. */
69 write_case_data wc_data; /* Aux data used by END CASE. */
71 enum value_init_type *init; /* How to initialize each `union value'. */
72 size_t init_cnt; /* Number of elements in inp_init. */
73 size_t case_size; /* Size of case in bytes. */
76 static void destroy_input_program (struct input_program_pgm *);
77 static trns_proc_func end_case_trns_proc;
78 static trns_proc_func reread_trns_proc;
79 static trns_proc_func end_file_trns_proc;
80 static trns_free_func reread_trns_free;
82 static const struct case_source_class input_program_source_class;
84 static bool inside_input_program;
86 /* Returns true if we're parsing the inside of a INPUT
87 PROGRAM...END INPUT PROGRAM construct, false otherwise. */
89 in_input_program (void)
91 return inside_input_program;
94 /* Emits an END CASE transformation for INP. */
96 emit_END_CASE (struct input_program_pgm *inp)
98 add_transformation (end_case_trns_proc, NULL, inp);
102 cmd_input_program (void)
104 struct input_program_pgm *inp;
106 bool saw_END_CASE = false;
108 discard_variables ();
110 return lex_end_of_command ();
112 inp = xmalloc (sizeof *inp);
115 inside_input_program = true;
118 enum cmd_result result;
120 result = cmd_parse (CMD_STATE_INPUT_PROGRAM);
121 if (result == CMD_END_INPUT_PROGRAM)
123 else if (result == CMD_END_CASE)
128 else if (cmd_result_is_failure (result) && result != CMD_FAILURE)
130 if (result == CMD_EOF)
131 msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
132 inside_input_program = false;
133 discard_variables ();
134 destroy_input_program (inp);
140 inside_input_program = false;
142 if (dict_get_next_value_idx (default_dict) == 0)
144 msg (SE, _("Input program did not create any variables."));
145 discard_variables ();
146 destroy_input_program (inp);
150 /* Mark the boundary between INPUT PROGRAM transformations and
151 ordinary transformations. */
154 /* Figure out how to initialize each input case. */
155 inp->init_cnt = dict_get_next_value_idx (default_dict);
156 inp->init = xnmalloc (inp->init_cnt, sizeof *inp->init);
157 for (i = 0; i < inp->init_cnt; i++)
159 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
161 struct variable *var = dict_get_var (default_dict, i);
162 enum value_init_type value_init;
165 value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
166 value_init |= var->leave ? INP_INIT_ONCE : INP_REINIT;
168 for (j = 0; j < var->nv; j++)
169 inp->init[j + var->fv] = value_init;
171 for (i = 0; i < inp->init_cnt; i++)
172 assert (inp->init[i] != -1);
173 inp->case_size = dict_get_case_size (default_dict);
175 /* Create vfm_source. */
176 vfm_source = create_case_source (&input_program_source_class, inp);
182 cmd_end_input_program (void)
184 assert (in_input_program ());
185 return CMD_END_INPUT_PROGRAM;
188 /* Initializes case C. Called before the first case is read. */
190 init_case (const struct input_program_pgm *inp, struct ccase *c)
194 for (i = 0; i < inp->init_cnt; i++)
195 switch (inp->init[i])
197 case INP_NUMERIC | INP_INIT_ONCE:
198 case_data_rw (c, i)->f = 0.0;
200 case INP_NUMERIC | INP_REINIT:
201 case_data_rw (c, i)->f = SYSMIS;
203 case INP_STRING | INP_INIT_ONCE:
204 case INP_STRING | INP_REINIT:
205 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
212 /* Clears case C. Called between reading successive records. */
214 clear_case (const struct input_program_pgm *inp, struct ccase *c)
218 for (i = 0; i < inp->init_cnt; i++)
219 switch (inp->init[i])
221 case INP_NUMERIC | INP_INIT_ONCE:
223 case INP_NUMERIC | INP_REINIT:
224 case_data_rw (c, i)->f = SYSMIS;
226 case INP_STRING | INP_INIT_ONCE:
228 case INP_STRING | INP_REINIT:
229 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
236 /* Executes each transformation in turn on a `blank' case.
237 Returns true if successful, false if an I/O error occurred. */
239 input_program_source_read (struct case_source *source,
241 write_case_func *write_case,
242 write_case_data wc_data)
244 struct input_program_pgm *inp = source->aux;
247 inp->write_case = write_case;
248 inp->wc_data = wc_data;
249 for (init_case (inp, c); ; clear_case (inp, c))
253 /* Perform transformations on `blank' case. */
254 for (i = 0; i < f_trns; )
258 code = t_trns[i].proc (t_trns[i].private, c, inp->case_nr);
287 destroy_input_program (struct input_program_pgm *pgm)
296 /* Destroys an INPUT PROGRAM source. */
298 input_program_source_destroy (struct case_source *source)
300 struct input_program_pgm *inp = source->aux;
302 destroy_input_program (inp);
305 static const struct case_source_class input_program_source_class =
309 input_program_source_read,
310 input_program_source_destroy,
316 assert (in_input_program ());
319 return lex_end_of_command ();
322 /* Sends the current case as the source's output. */
324 end_case_trns_proc (void *inp_, struct ccase *c, int case_nr UNUSED)
326 struct input_program_pgm *inp = inp_;
328 if (!inp->write_case (inp->wc_data))
333 return TRNS_CONTINUE;
336 /* REREAD transformation. */
339 struct dfm_reader *reader; /* File to move file pointer back on. */
340 struct expression *column; /* Column to reset file pointer to. */
343 /* Parses REREAD command. */
347 struct file_handle *fh; /* File to be re-read. */
348 struct expression *e; /* Expression for column to set. */
349 struct reread_trns *t; /* Created transformation. */
351 fh = fh_get_default_handle ();
355 if (lex_match_id ("COLUMN"))
361 msg (SE, _("COLUMN subcommand multiply specified."));
363 return CMD_CASCADING_FAILURE;
366 e = expr_parse (default_dict, EXPR_NUMBER);
368 return CMD_CASCADING_FAILURE;
370 else if (lex_match_id ("FILE"))
373 fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
377 return CMD_CASCADING_FAILURE;
388 t = xmalloc (sizeof *t);
389 t->reader = dfm_open_reader (fh);
391 add_transformation (reread_trns_proc, reread_trns_free, t);
396 /* Executes a REREAD transformation. */
398 reread_trns_proc (void *t_, struct ccase *c, int case_num)
400 struct reread_trns *t = t_;
402 if (t->column == NULL)
403 dfm_reread_record (t->reader, 1);
406 double column = expr_evaluate_num (t->column, c, case_num);
407 if (!finite (column) || column < 1)
409 msg (SE, _("REREAD: Column numbers must be positive finite "
410 "numbers. Column set to 1."));
411 dfm_reread_record (t->reader, 1);
414 dfm_reread_record (t->reader, column);
416 return TRNS_CONTINUE;
419 /* Frees a REREAD transformation.
420 Returns true if successful, false if an I/O error occurred. */
422 reread_trns_free (void *t_)
424 struct reread_trns *t = t_;
425 expr_free (t->column);
426 dfm_close_reader (t->reader);
430 /* Parses END FILE command. */
434 assert (in_input_program ());
436 add_transformation (end_file_trns_proc, NULL, NULL);
438 return lex_end_of_command ();
441 /* Executes an END FILE transformation. */
443 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
446 return TRNS_END_FILE;