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
21 #include <libpspp/message.h>
24 #include <libpspp/alloc.h>
25 #include <data/case.h>
26 #include <language/command.h>
27 #include <libpspp/compiler.h>
28 #include <language/data-io/data-list.h>
29 #include <language/data-io/data-reader.h>
30 #include <data/dictionary.h>
31 #include <libpspp/message.h>
32 #include <language/expressions/public.h>
33 #include <language/data-io/file-handle.h>
34 #include <language/lexer/lexer.h>
35 #include <libpspp/misc.h>
36 #include <libpspp/str.h>
37 #include <data/variable.h>
38 #include <procedure.h>
41 #define _(msgid) gettext (msgid)
43 /* Indicates how a `union value' should be initialized. */
46 INP_NUMERIC = 01, /* Numeric. */
47 INP_STRING = 0, /* String. */
49 INP_INIT_ONCE = 02, /* Initialize only once. */
50 INP_REINIT = 0, /* Reinitialize for each iteration. */
53 struct input_program_pgm
55 enum value_init_type *init; /* How to initialize each `union value'. */
56 size_t init_cnt; /* Number of elements in inp_init. */
57 size_t case_size; /* Size of case in bytes. */
60 static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
61 static trns_free_func reread_trns_free;
64 cmd_input_program (void)
68 /* FIXME: we shouldn't do this here, but I'm afraid that other
69 code will check the class of vfm_source. */
70 vfm_source = create_case_source (&input_program_source_class, NULL);
72 return lex_end_of_command ();
76 cmd_end_input_program (void)
78 struct input_program_pgm *inp;
81 if (!case_source_is_class (vfm_source, &input_program_source_class))
83 msg (SE, _("No matching INPUT PROGRAM command."));
84 return CMD_CASCADING_FAILURE;
87 if (dict_get_next_value_idx (default_dict) == 0)
88 msg (SW, _("No data-input or transformation commands specified "
89 "between INPUT PROGRAM and END INPUT PROGRAM."));
91 /* Mark the boundary between INPUT PROGRAM transformations and
92 ordinary transformations. */
95 /* Figure out how to initialize each input case. */
96 inp = xmalloc (sizeof *inp);
97 inp->init_cnt = dict_get_next_value_idx (default_dict);
98 inp->init = xnmalloc (inp->init_cnt, sizeof *inp->init);
99 for (i = 0; i < inp->init_cnt; i++)
101 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
103 struct variable *var = dict_get_var (default_dict, i);
104 enum value_init_type value_init;
107 value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
108 value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE;
110 for (j = 0; j < var->nv; j++)
111 inp->init[j + var->fv] = value_init;
113 for (i = 0; i < inp->init_cnt; i++)
114 assert (inp->init[i] != -1);
115 inp->case_size = dict_get_case_size (default_dict);
117 /* Put inp into vfm_source for later use. */
118 vfm_source->aux = inp;
120 return lex_end_of_command ();
123 /* Initializes case C. Called before the first case is read. */
125 init_case (const struct input_program_pgm *inp, struct ccase *c)
129 for (i = 0; i < inp->init_cnt; i++)
130 switch (inp->init[i])
132 case INP_NUMERIC | INP_INIT_ONCE:
133 case_data_rw (c, i)->f = 0.0;
135 case INP_NUMERIC | INP_REINIT:
136 case_data_rw (c, i)->f = SYSMIS;
138 case INP_STRING | INP_INIT_ONCE:
139 case INP_STRING | INP_REINIT:
140 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
147 /* Clears case C. Called between reading successive records. */
149 clear_case (const struct input_program_pgm *inp, struct ccase *c)
153 for (i = 0; i < inp->init_cnt; i++)
154 switch (inp->init[i])
156 case INP_NUMERIC | INP_INIT_ONCE:
158 case INP_NUMERIC | INP_REINIT:
159 case_data_rw (c, i)->f = SYSMIS;
161 case INP_STRING | INP_INIT_ONCE:
163 case INP_STRING | INP_REINIT:
164 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
171 /* Executes each transformation in turn on a `blank' case.
172 Returns true if successful, false if an I/O error occurred. */
174 input_program_source_read (struct case_source *source,
176 write_case_func *write_case,
177 write_case_data wc_data)
179 struct input_program_pgm *inp = source->aux;
182 /* Nonzero if there were any END CASE commands in the set of
183 transformations. If so, we don't automatically write out
187 /* FIXME? This is the number of cases sent out of the input
188 program, not the number of cases written to the procedure.
189 The difference should only show up in $CASENUM in COMPUTE.
190 We should check behavior against SPSS. */
191 int cases_written = 0;
193 assert (inp != NULL);
195 /* Figure end_case. */
196 for (i = 0; i < f_trns; i++)
197 if (t_trns[i].proc == end_case_trns_proc)
200 /* FIXME: This is an ugly kluge. */
201 for (i = 0; i < f_trns; i++)
202 if (t_trns[i].proc == repeating_data_trns_proc)
203 repeating_data_set_write_case (t_trns[i].private, write_case, wc_data);
208 /* Perform transformations on `blank' case. */
209 for (i = 0; i < f_trns; )
213 if (t_trns[i].proc == end_case_trns_proc)
216 if (!write_case (wc_data))
223 code = t_trns[i].proc (t_trns[i].private, c, cases_written + 1);
248 /* Write the case if appropriate. */
252 if (!write_case (wc_data))
256 /* Blank out the case for the next iteration. */
262 /* Destroys an INPUT PROGRAM source. */
264 input_program_source_destroy (struct case_source *source)
266 struct input_program_pgm *inp = source->aux;
268 cancel_transformations ();
277 const struct case_source_class input_program_source_class =
281 input_program_source_read,
282 input_program_source_destroy,
288 if (!case_source_is_class (vfm_source, &input_program_source_class))
290 msg (SE, _("This command may only be executed between INPUT PROGRAM "
291 "and END INPUT PROGRAM."));
292 return CMD_CASCADING_FAILURE;
295 add_transformation (end_case_trns_proc, NULL, NULL);
297 return lex_end_of_command ();
300 /* Should never be called, because this is handled in
301 input_program_source_read(). */
303 end_case_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
309 /* REREAD transformation. */
312 struct dfm_reader *reader; /* File to move file pointer back on. */
313 struct expression *column; /* Column to reset file pointer to. */
316 /* Parses REREAD command. */
320 struct file_handle *fh; /* File to be re-read. */
321 struct expression *e; /* Expression for column to set. */
322 struct reread_trns *t; /* Created transformation. */
324 fh = fh_get_default_handle ();
328 if (lex_match_id ("COLUMN"))
334 msg (SE, _("COLUMN subcommand multiply specified."));
336 return CMD_CASCADING_FAILURE;
339 e = expr_parse (default_dict, EXPR_NUMBER);
341 return CMD_CASCADING_FAILURE;
343 else if (lex_match_id ("FILE"))
346 fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
350 return CMD_CASCADING_FAILURE;
361 t = xmalloc (sizeof *t);
362 t->reader = dfm_open_reader (fh);
364 add_transformation (reread_trns_proc, reread_trns_free, t);
369 /* Executes a REREAD transformation. */
371 reread_trns_proc (void *t_, struct ccase *c, int case_num)
373 struct reread_trns *t = t_;
375 if (t->column == NULL)
376 dfm_reread_record (t->reader, 1);
379 double column = expr_evaluate_num (t->column, c, case_num);
380 if (!finite (column) || column < 1)
382 msg (SE, _("REREAD: Column numbers must be positive finite "
383 "numbers. Column set to 1."));
384 dfm_reread_record (t->reader, 1);
387 dfm_reread_record (t->reader, column);
389 return TRNS_CONTINUE;
392 /* Frees a REREAD transformation.
393 Returns true if successful, false if an I/O error occurred. */
395 reread_trns_free (void *t_)
397 struct reread_trns *t = t_;
398 expr_free (t->column);
399 dfm_close_reader (t->reader);
403 /* Parses END FILE command. */
407 if (!case_source_is_class (vfm_source, &input_program_source_class))
409 msg (SE, _("This command may only be executed between INPUT PROGRAM "
410 "and END INPUT PROGRAM."));
411 return CMD_CASCADING_FAILURE;
414 add_transformation (end_file_trns_proc, NULL, NULL);
416 return lex_end_of_command ();
419 /* Executes an END FILE transformation. */
421 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
424 return TRNS_END_FILE;