1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <language/data-io/inpt-pgm.h>
26 #include <data/case-source.h>
27 #include <data/case.h>
28 #include <data/case-source.h>
29 #include <data/dictionary.h>
30 #include <data/procedure.h>
31 #include <data/transformations.h>
32 #include <data/variable.h>
33 #include <language/command.h>
34 #include <language/data-io/data-reader.h>
35 #include <language/data-io/file-handle.h>
36 #include <language/expressions/public.h>
37 #include <language/lexer/lexer.h>
38 #include <libpspp/alloc.h>
39 #include <libpspp/assertion.h>
40 #include <libpspp/compiler.h>
41 #include <libpspp/message.h>
42 #include <libpspp/message.h>
43 #include <libpspp/misc.h>
44 #include <libpspp/str.h>
47 #define _(msgid) gettext (msgid)
49 /* Private result codes for use within INPUT PROGRAM. */
50 enum cmd_result_extensions
52 CMD_END_INPUT_PROGRAM = CMD_PRIVATE_FIRST,
56 /* Indicates how a `union value' should be initialized. */
59 INP_NUMERIC = 01, /* Numeric. */
60 INP_STRING = 0, /* String. */
62 INP_INIT_ONCE = 02, /* Initialize only once. */
63 INP_REINIT = 0, /* Reinitialize for each iteration. */
66 struct input_program_pgm
68 struct trns_chain *trns_chain;
69 enum trns_result restart;
71 bool inited_case; /* Did one-time case initialization? */
72 size_t case_nr; /* Incremented by END CASE transformation. */
74 enum value_init_type *init; /* How to initialize each `union value'. */
75 size_t init_cnt; /* Number of elements in inp_init. */
76 size_t case_size; /* Size of case in bytes. */
79 static void destroy_input_program (struct input_program_pgm *);
80 static trns_proc_func end_case_trns_proc;
81 static trns_proc_func reread_trns_proc;
82 static trns_proc_func end_file_trns_proc;
83 static trns_free_func reread_trns_free;
85 static const struct case_source_class input_program_source_class;
87 static bool inside_input_program;
89 /* Returns true if we're parsing the inside of a INPUT
90 PROGRAM...END INPUT PROGRAM construct, false otherwise. */
92 in_input_program (void)
94 return inside_input_program;
97 /* Emits an END CASE transformation for INP. */
99 emit_END_CASE (struct dataset *ds, struct input_program_pgm *inp)
101 add_transformation (ds, end_case_trns_proc, NULL, inp);
105 cmd_input_program (struct lexer *lexer, struct dataset *ds)
107 struct input_program_pgm *inp;
109 bool saw_END_CASE = false;
111 discard_variables (ds);
112 if (lex_token (lexer) != '.')
113 return lex_end_of_command (lexer);
115 inp = xmalloc (sizeof *inp);
116 inp->trns_chain = NULL;
119 inside_input_program = true;
122 enum cmd_result result = cmd_parse_in_state (lexer, ds, CMD_STATE_INPUT_PROGRAM);
123 if (result == CMD_END_INPUT_PROGRAM)
125 else if (result == CMD_END_CASE)
127 emit_END_CASE (ds, inp);
130 else if (cmd_result_is_failure (result) && result != CMD_FAILURE)
132 if (result == CMD_EOF)
133 msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
134 inside_input_program = false;
135 discard_variables (ds);
136 destroy_input_program (inp);
141 emit_END_CASE (ds, inp);
142 inside_input_program = false;
144 if (dict_get_next_value_idx (dataset_dict (ds)) == 0)
146 msg (SE, _("Input program did not create any variables."));
147 discard_variables (ds);
148 destroy_input_program (inp);
152 inp->trns_chain = proc_capture_transformations (ds);
153 trns_chain_finalize (inp->trns_chain);
155 inp->restart = TRNS_CONTINUE;
156 inp->inited_case = false;
159 /* Figure out how to initialize each input case. */
160 inp->init_cnt = dict_get_next_value_idx (dataset_dict (ds));
161 inp->init = xnmalloc (inp->init_cnt, sizeof *inp->init);
162 for (i = 0; i < inp->init_cnt; i++)
164 for (i = 0; i < dict_get_var_cnt (dataset_dict (ds)); i++)
166 struct variable *var = dict_get_var (dataset_dict (ds), i);
167 size_t value_cnt = var_get_value_cnt (var);
168 enum value_init_type value_init;
171 value_init = var_is_numeric (var) ? INP_NUMERIC : INP_STRING;
172 value_init |= var_get_leave (var) ? INP_INIT_ONCE : INP_REINIT;
174 for (j = 0; j < value_cnt; j++)
175 inp->init[j + var_get_case_index (var)] = value_init;
177 for (i = 0; i < inp->init_cnt; i++)
178 assert (inp->init[i] != -1);
179 inp->case_size = dict_get_case_size (dataset_dict (ds));
182 create_case_source (&input_program_source_class, inp));
188 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
190 assert (in_input_program ());
191 return CMD_END_INPUT_PROGRAM;
194 /* Initializes case C. Called before the first case is read. */
196 init_case (const struct input_program_pgm *inp, struct ccase *c)
200 for (i = 0; i < inp->init_cnt; i++)
201 switch (inp->init[i])
203 case INP_NUMERIC | INP_INIT_ONCE:
204 case_data_rw_idx (c, i)->f = 0.0;
206 case INP_NUMERIC | INP_REINIT:
207 case_data_rw_idx (c, i)->f = SYSMIS;
209 case INP_STRING | INP_INIT_ONCE:
210 case INP_STRING | INP_REINIT:
211 memset (case_data_rw_idx (c, i)->s, ' ',
212 sizeof case_data_rw_idx (c, i)->s);
219 /* Clears case C. Called between reading successive records. */
221 clear_case (const struct input_program_pgm *inp, struct ccase *c)
225 for (i = 0; i < inp->init_cnt; i++)
226 switch (inp->init[i])
228 case INP_NUMERIC | INP_INIT_ONCE:
230 case INP_NUMERIC | INP_REINIT:
231 case_data_rw_idx (c, i)->f = SYSMIS;
233 case INP_STRING | INP_INIT_ONCE:
235 case INP_STRING | INP_REINIT:
236 memset (case_data_rw_idx (c, i)->s, ' ',
237 sizeof case_data_rw_idx (c, i)->s);
244 /* Returns true if STATE is valid given the transformations that
245 are allowed within INPUT PROGRAM. */
247 is_valid_state (enum trns_result state)
249 return (state == TRNS_CONTINUE
250 || state == TRNS_ERROR
251 || state == TRNS_END_FILE
255 /* Reads one case into C.
256 Returns true if successful, false at end of file or if an
257 I/O error occurred. */
259 input_program_source_read (struct case_source *source, struct ccase *c)
261 struct input_program_pgm *inp = source->aux;
263 if (!inp->inited_case)
266 inp->inited_case = true;
271 assert (is_valid_state (inp->restart));
272 if (inp->restart == TRNS_ERROR || inp->restart == TRNS_END_FILE)
276 inp->restart = trns_chain_execute (inp->trns_chain, inp->restart,
278 assert (is_valid_state (inp->restart));
280 while (inp->restart < 0);
286 destroy_input_program (struct input_program_pgm *pgm)
290 trns_chain_destroy (pgm->trns_chain);
296 /* Destroys the source.
297 Returns true if successful read, false if an I/O occurred
298 during destruction or previously. */
300 input_program_source_destroy (struct case_source *source)
302 struct input_program_pgm *inp = source->aux;
303 bool ok = inp->restart != TRNS_ERROR;
304 destroy_input_program (inp);
308 static const struct case_source_class input_program_source_class =
312 input_program_source_read,
313 input_program_source_destroy,
317 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
319 assert (in_input_program ());
320 if (lex_token (lexer) == '.')
322 return lex_end_of_command (lexer);
325 /* Sends the current case as the source's output. */
327 end_case_trns_proc (void *inp_, struct ccase *c UNUSED,
328 casenumber case_nr UNUSED)
330 struct input_program_pgm *inp = inp_;
332 return TRNS_END_CASE;
335 /* REREAD transformation. */
338 struct dfm_reader *reader; /* File to move file pointer back on. */
339 struct expression *column; /* Column to reset file pointer to. */
342 /* Parses REREAD command. */
344 cmd_reread (struct lexer *lexer, struct dataset *ds)
346 struct file_handle *fh; /* File to be re-read. */
347 struct expression *e; /* Expression for column to set. */
348 struct reread_trns *t; /* Created transformation. */
350 fh = fh_get_default_handle ();
352 while (lex_token (lexer) != '.')
354 if (lex_match_id (lexer, "COLUMN"))
356 lex_match (lexer, '=');
360 msg (SE, _("COLUMN subcommand multiply specified."));
362 return CMD_CASCADING_FAILURE;
365 e = expr_parse (lexer, ds, EXPR_NUMBER);
367 return CMD_CASCADING_FAILURE;
369 else if (lex_match_id (lexer, "FILE"))
371 lex_match (lexer, '=');
372 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
376 return CMD_CASCADING_FAILURE;
381 lex_error (lexer, NULL);
383 return CMD_CASCADING_FAILURE;
387 t = xmalloc (sizeof *t);
388 t->reader = dfm_open_reader (fh, lexer);
390 add_transformation (ds, reread_trns_proc, reread_trns_free, t);
395 /* Executes a REREAD transformation. */
397 reread_trns_proc (void *t_, struct ccase *c, casenumber case_num)
399 struct reread_trns *t = t_;
401 if (t->column == NULL)
402 dfm_reread_record (t->reader, 1);
405 double column = expr_evaluate_num (t->column, c, case_num);
406 if (!finite (column) || column < 1)
408 msg (SE, _("REREAD: Column numbers must be positive finite "
409 "numbers. Column set to 1."));
410 dfm_reread_record (t->reader, 1);
413 dfm_reread_record (t->reader, column);
415 return TRNS_CONTINUE;
418 /* Frees a REREAD transformation.
419 Returns true if successful, false if an I/O error occurred. */
421 reread_trns_free (void *t_)
423 struct reread_trns *t = t_;
424 expr_free (t->column);
425 dfm_close_reader (t->reader);
429 /* Parses END FILE command. */
431 cmd_end_file (struct lexer *lexer, struct dataset *ds)
433 assert (in_input_program ());
435 add_transformation (ds, end_file_trns_proc, NULL, NULL);
437 return lex_end_of_command (lexer);
440 /* Executes an END FILE transformation. */
442 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
443 casenumber case_num UNUSED)
445 return TRNS_END_FILE;