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/procedure.h>
32 #include <data/transformations.h>
33 #include <data/variable.h>
34 #include <language/command.h>
35 #include <language/data-io/data-reader.h>
36 #include <language/data-io/file-handle.h>
37 #include <language/expressions/public.h>
38 #include <language/lexer/lexer.h>
39 #include <libpspp/alloc.h>
40 #include <libpspp/assertion.h>
41 #include <libpspp/compiler.h>
42 #include <libpspp/message.h>
43 #include <libpspp/message.h>
44 #include <libpspp/misc.h>
45 #include <libpspp/str.h>
48 #define _(msgid) gettext (msgid)
50 /* Private result codes for use within INPUT PROGRAM. */
51 enum cmd_result_extensions
53 CMD_END_INPUT_PROGRAM = CMD_PRIVATE_FIRST,
57 /* Indicates how a `union value' should be initialized. */
60 INP_NUMERIC = 01, /* Numeric. */
61 INP_STRING = 0, /* String. */
63 INP_INIT_ONCE = 02, /* Initialize only once. */
64 INP_REINIT = 0, /* Reinitialize for each iteration. */
67 struct input_program_pgm
69 struct trns_chain *trns_chain;
71 size_t case_nr; /* Incremented by END CASE transformation. */
72 write_case_func *write_case;/* Called by END CASE. */
73 write_case_data wc_data; /* Aux data used by END CASE. */
75 enum value_init_type *init; /* How to initialize each `union value'. */
76 size_t init_cnt; /* Number of elements in inp_init. */
77 size_t case_size; /* Size of case in bytes. */
80 static void destroy_input_program (struct input_program_pgm *);
81 static trns_proc_func end_case_trns_proc;
82 static trns_proc_func reread_trns_proc;
83 static trns_proc_func end_file_trns_proc;
84 static trns_free_func reread_trns_free;
86 static const struct case_source_class input_program_source_class;
88 static bool inside_input_program;
90 /* Returns true if we're parsing the inside of a INPUT
91 PROGRAM...END INPUT PROGRAM construct, false otherwise. */
93 in_input_program (void)
95 return inside_input_program;
98 /* Emits an END CASE transformation for INP. */
100 emit_END_CASE (struct dataset *ds, struct input_program_pgm *inp)
102 add_transformation (ds, end_case_trns_proc, NULL, inp);
106 cmd_input_program (struct lexer *lexer, struct dataset *ds)
108 struct input_program_pgm *inp;
110 bool saw_END_CASE = false;
112 discard_variables (ds);
113 if (lex_token (lexer) != '.')
114 return lex_end_of_command (lexer);
116 inp = xmalloc (sizeof *inp);
117 inp->trns_chain = NULL;
120 inside_input_program = true;
123 enum cmd_result result = cmd_parse (lexer, ds, CMD_STATE_INPUT_PROGRAM);
124 if (result == CMD_END_INPUT_PROGRAM)
126 else if (result == CMD_END_CASE)
128 emit_END_CASE (ds, inp);
131 else if (cmd_result_is_failure (result) && result != CMD_FAILURE)
133 if (result == CMD_EOF)
134 msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
135 inside_input_program = false;
136 discard_variables (ds);
137 destroy_input_program (inp);
142 emit_END_CASE (ds, inp);
143 inside_input_program = false;
145 if (dict_get_next_value_idx (dataset_dict (ds)) == 0)
147 msg (SE, _("Input program did not create any variables."));
148 discard_variables (ds);
149 destroy_input_program (inp);
153 inp->trns_chain = proc_capture_transformations (ds);
154 trns_chain_finalize (inp->trns_chain);
156 /* Figure out how to initialize each input case. */
157 inp->init_cnt = dict_get_next_value_idx (dataset_dict (ds));
158 inp->init = xnmalloc (inp->init_cnt, sizeof *inp->init);
159 for (i = 0; i < inp->init_cnt; i++)
161 for (i = 0; i < dict_get_var_cnt (dataset_dict (ds)); i++)
163 struct variable *var = dict_get_var (dataset_dict (ds), i);
164 size_t value_cnt = var_get_value_cnt (var);
165 enum value_init_type value_init;
168 value_init = var_is_numeric (var) ? INP_NUMERIC : INP_STRING;
169 value_init |= var_get_leave (var) ? INP_INIT_ONCE : INP_REINIT;
171 for (j = 0; j < value_cnt; j++)
172 inp->init[j + var->fv] = value_init;
174 for (i = 0; i < inp->init_cnt; i++)
175 assert (inp->init[i] != -1);
176 inp->case_size = dict_get_case_size (dataset_dict (ds));
179 create_case_source (&input_program_source_class, inp));
185 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
187 assert (in_input_program ());
188 return CMD_END_INPUT_PROGRAM;
191 /* Initializes case C. Called before the first case is read. */
193 init_case (const struct input_program_pgm *inp, struct ccase *c)
197 for (i = 0; i < inp->init_cnt; i++)
198 switch (inp->init[i])
200 case INP_NUMERIC | INP_INIT_ONCE:
201 case_data_rw (c, i)->f = 0.0;
203 case INP_NUMERIC | INP_REINIT:
204 case_data_rw (c, i)->f = SYSMIS;
206 case INP_STRING | INP_INIT_ONCE:
207 case INP_STRING | INP_REINIT:
208 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
215 /* Clears case C. Called between reading successive records. */
217 clear_case (const struct input_program_pgm *inp, struct ccase *c)
221 for (i = 0; i < inp->init_cnt; i++)
222 switch (inp->init[i])
224 case INP_NUMERIC | INP_INIT_ONCE:
226 case INP_NUMERIC | INP_REINIT:
227 case_data_rw (c, i)->f = SYSMIS;
229 case INP_STRING | INP_INIT_ONCE:
231 case INP_STRING | INP_REINIT:
232 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
239 /* Executes each transformation in turn on a `blank' case.
240 Returns true if successful, false if an I/O error occurred. */
242 input_program_source_read (struct case_source *source,
244 write_case_func *write_case,
245 write_case_data wc_data)
247 struct input_program_pgm *inp = source->aux;
250 inp->write_case = write_case;
251 inp->wc_data = wc_data;
252 for (init_case (inp, c); ; clear_case (inp, c))
254 enum trns_result result = trns_chain_execute (inp->trns_chain, c,
256 if (result == TRNS_ERROR)
258 else if (result == TRNS_END_FILE)
264 destroy_input_program (struct input_program_pgm *pgm)
268 trns_chain_destroy (pgm->trns_chain);
274 /* Destroys an INPUT PROGRAM source. */
276 input_program_source_destroy (struct case_source *source)
278 struct input_program_pgm *inp = source->aux;
280 destroy_input_program (inp);
283 static const struct case_source_class input_program_source_class =
287 input_program_source_read,
288 input_program_source_destroy,
292 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
294 assert (in_input_program ());
295 if (lex_token (lexer) == '.')
297 return lex_end_of_command (lexer);
300 /* Sends the current case as the source's output. */
302 end_case_trns_proc (void *inp_, struct ccase *c, casenumber case_nr UNUSED)
304 struct input_program_pgm *inp = inp_;
306 if (!inp->write_case (inp->wc_data))
311 return TRNS_CONTINUE;
314 /* REREAD transformation. */
317 struct dfm_reader *reader; /* File to move file pointer back on. */
318 struct expression *column; /* Column to reset file pointer to. */
321 /* Parses REREAD command. */
323 cmd_reread (struct lexer *lexer, struct dataset *ds)
325 struct file_handle *fh; /* File to be re-read. */
326 struct expression *e; /* Expression for column to set. */
327 struct reread_trns *t; /* Created transformation. */
329 fh = fh_get_default_handle ();
331 while (lex_token (lexer) != '.')
333 if (lex_match_id (lexer, "COLUMN"))
335 lex_match (lexer, '=');
339 msg (SE, _("COLUMN subcommand multiply specified."));
341 return CMD_CASCADING_FAILURE;
344 e = expr_parse (lexer, ds, EXPR_NUMBER);
346 return CMD_CASCADING_FAILURE;
348 else if (lex_match_id (lexer, "FILE"))
350 lex_match (lexer, '=');
351 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
355 return CMD_CASCADING_FAILURE;
360 lex_error (lexer, NULL);
362 return CMD_CASCADING_FAILURE;
366 t = xmalloc (sizeof *t);
367 t->reader = dfm_open_reader (fh, lexer);
369 add_transformation (ds, reread_trns_proc, reread_trns_free, t);
374 /* Executes a REREAD transformation. */
376 reread_trns_proc (void *t_, struct ccase *c, casenumber case_num)
378 struct reread_trns *t = t_;
380 if (t->column == NULL)
381 dfm_reread_record (t->reader, 1);
384 double column = expr_evaluate_num (t->column, c, case_num);
385 if (!finite (column) || column < 1)
387 msg (SE, _("REREAD: Column numbers must be positive finite "
388 "numbers. Column set to 1."));
389 dfm_reread_record (t->reader, 1);
392 dfm_reread_record (t->reader, column);
394 return TRNS_CONTINUE;
397 /* Frees a REREAD transformation.
398 Returns true if successful, false if an I/O error occurred. */
400 reread_trns_free (void *t_)
402 struct reread_trns *t = t_;
403 expr_free (t->column);
404 dfm_close_reader (t->reader);
408 /* Parses END FILE command. */
410 cmd_end_file (struct lexer *lexer, struct dataset *ds)
412 assert (in_input_program ());
414 add_transformation (ds, end_file_trns_proc, NULL, NULL);
416 return lex_end_of_command (lexer);
419 /* Executes an END FILE transformation. */
421 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
422 casenumber case_num UNUSED)
424 return TRNS_END_FILE;