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.h>
27 #include <data/caseinit.h>
28 #include <data/casereader-provider.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 size_t case_nr; /* Incremented by END CASE transformation. */
73 struct caseinit *init;
77 static void destroy_input_program (struct input_program_pgm *);
78 static trns_proc_func end_case_trns_proc;
79 static trns_proc_func reread_trns_proc;
80 static trns_proc_func end_file_trns_proc;
81 static trns_free_func reread_trns_free;
83 static const struct casereader_class input_program_casereader_class;
85 static bool inside_input_program;
87 /* Returns true if we're parsing the inside of a INPUT
88 PROGRAM...END INPUT PROGRAM construct, false otherwise. */
90 in_input_program (void)
92 return inside_input_program;
95 /* Emits an END CASE transformation for INP. */
97 emit_END_CASE (struct dataset *ds, struct input_program_pgm *inp)
99 add_transformation (ds, end_case_trns_proc, NULL, inp);
103 cmd_input_program (struct lexer *lexer, struct dataset *ds)
105 struct input_program_pgm *inp;
106 bool saw_END_CASE = false;
108 proc_discard_active_file (ds);
109 if (lex_token (lexer) != '.')
110 return lex_end_of_command (lexer);
112 inp = xmalloc (sizeof *inp);
113 inp->trns_chain = NULL;
116 inside_input_program = true;
119 enum cmd_result result = cmd_parse_in_state (lexer, ds, CMD_STATE_INPUT_PROGRAM);
120 if (result == CMD_END_INPUT_PROGRAM)
122 else if (result == CMD_END_CASE)
124 emit_END_CASE (ds, inp);
127 else if (cmd_result_is_failure (result) && result != CMD_FAILURE)
129 if (result == CMD_EOF)
130 msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
131 inside_input_program = false;
132 proc_discard_active_file (ds);
133 destroy_input_program (inp);
138 emit_END_CASE (ds, inp);
139 inside_input_program = false;
141 if (dict_get_next_value_idx (dataset_dict (ds)) == 0)
143 msg (SE, _("Input program did not create any variables."));
144 proc_discard_active_file (ds);
145 destroy_input_program (inp);
149 inp->trns_chain = proc_capture_transformations (ds);
150 trns_chain_finalize (inp->trns_chain);
152 inp->restart = TRNS_CONTINUE;
154 /* Figure out how to initialize each input case. */
155 inp->init = caseinit_create ();
156 caseinit_mark_for_init (inp->init, dataset_dict (ds));
157 inp->value_cnt = dict_get_next_value_idx (dataset_dict (ds));
159 proc_set_active_file_data (
160 ds, casereader_create_sequential (NULL, inp->value_cnt, CASENUMBER_MAX,
161 &input_program_casereader_class, inp));
167 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
169 assert (in_input_program ());
170 return CMD_END_INPUT_PROGRAM;
173 /* Returns true if STATE is valid given the transformations that
174 are allowed within INPUT PROGRAM. */
176 is_valid_state (enum trns_result state)
178 return (state == TRNS_CONTINUE
179 || state == TRNS_ERROR
180 || state == TRNS_END_FILE
184 /* Reads one case into C.
185 Returns true if successful, false at end of file or if an
186 I/O error occurred. */
188 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_,
191 struct input_program_pgm *inp = inp_;
193 case_create (c, inp->value_cnt);
197 assert (is_valid_state (inp->restart));
198 if (inp->restart == TRNS_ERROR || inp->restart == TRNS_END_FILE)
204 caseinit_init_reinit_vars (inp->init, c);
205 caseinit_init_left_vars (inp->init, c);
206 inp->restart = trns_chain_execute (inp->trns_chain, inp->restart,
208 assert (is_valid_state (inp->restart));
209 caseinit_update_left_vars (inp->init, c);
211 while (inp->restart < 0);
217 destroy_input_program (struct input_program_pgm *pgm)
221 trns_chain_destroy (pgm->trns_chain);
222 caseinit_destroy (pgm->init);
227 /* Destroys the casereader. */
229 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
231 struct input_program_pgm *inp = inp_;
232 if (inp->restart == TRNS_ERROR)
233 casereader_force_error (reader);
234 destroy_input_program (inp);
237 static const struct casereader_class input_program_casereader_class =
239 input_program_casereader_read,
240 input_program_casereader_destroy,
246 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
248 assert (in_input_program ());
249 if (lex_token (lexer) == '.')
251 return lex_end_of_command (lexer);
254 /* Outputs the current case */
256 end_case_trns_proc (void *inp_, struct ccase *c UNUSED,
257 casenumber case_nr UNUSED)
259 struct input_program_pgm *inp = inp_;
261 return TRNS_END_CASE;
264 /* REREAD transformation. */
267 struct dfm_reader *reader; /* File to move file pointer back on. */
268 struct expression *column; /* Column to reset file pointer to. */
271 /* Parses REREAD command. */
273 cmd_reread (struct lexer *lexer, struct dataset *ds)
275 struct file_handle *fh; /* File to be re-read. */
276 struct expression *e; /* Expression for column to set. */
277 struct reread_trns *t; /* Created transformation. */
279 fh = fh_get_default_handle ();
281 while (lex_token (lexer) != '.')
283 if (lex_match_id (lexer, "COLUMN"))
285 lex_match (lexer, '=');
289 msg (SE, _("COLUMN subcommand multiply specified."));
291 return CMD_CASCADING_FAILURE;
294 e = expr_parse (lexer, ds, EXPR_NUMBER);
296 return CMD_CASCADING_FAILURE;
298 else if (lex_match_id (lexer, "FILE"))
300 lex_match (lexer, '=');
301 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
305 return CMD_CASCADING_FAILURE;
310 lex_error (lexer, NULL);
312 return CMD_CASCADING_FAILURE;
316 t = xmalloc (sizeof *t);
317 t->reader = dfm_open_reader (fh, lexer);
319 add_transformation (ds, reread_trns_proc, reread_trns_free, t);
324 /* Executes a REREAD transformation. */
326 reread_trns_proc (void *t_, struct ccase *c, casenumber case_num)
328 struct reread_trns *t = t_;
330 if (t->column == NULL)
331 dfm_reread_record (t->reader, 1);
334 double column = expr_evaluate_num (t->column, c, case_num);
335 if (!finite (column) || column < 1)
337 msg (SE, _("REREAD: Column numbers must be positive finite "
338 "numbers. Column set to 1."));
339 dfm_reread_record (t->reader, 1);
342 dfm_reread_record (t->reader, column);
344 return TRNS_CONTINUE;
347 /* Frees a REREAD transformation.
348 Returns true if successful, false if an I/O error occurred. */
350 reread_trns_free (void *t_)
352 struct reread_trns *t = t_;
353 expr_free (t->column);
354 dfm_close_reader (t->reader);
358 /* Parses END FILE command. */
360 cmd_end_file (struct lexer *lexer, struct dataset *ds)
362 assert (in_input_program ());
364 add_transformation (ds, end_file_trns_proc, NULL, NULL);
366 return lex_end_of_command (lexer);
369 /* Executes an END FILE transformation. */
371 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
372 casenumber case_num UNUSED)
374 return TRNS_END_FILE;