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., 59 Temple Place - Suite 330, Boston, MA
27 #include "data-list.h"
29 #include "dictionary.h"
31 #include "expressions/public.h"
32 #include "file-handle.h"
39 #include "debug-print.h"
41 /* Indicates how a `union value' should be initialized. */
44 INP_NUMERIC = 01, /* Numeric. */
45 INP_STRING = 0, /* String. */
47 INP_INIT_ONCE = 02, /* Initialize only once. */
48 INP_REINIT = 0, /* Reinitialize for each iteration. */
51 struct input_program_pgm
53 enum value_init_type *init; /* How to initialize each `union value'. */
54 size_t init_cnt; /* Number of elements in inp_init. */
55 size_t case_size; /* Size of case in bytes. */
58 static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
59 static trns_free_func reread_trns_free;
62 cmd_input_program (void)
66 /* FIXME: we shouldn't do this here, but I'm afraid that other
67 code will check the class of vfm_source. */
68 vfm_source = create_case_source (&input_program_source_class, NULL);
70 return lex_end_of_command ();
74 cmd_end_input_program (void)
76 struct input_program_pgm *inp;
79 if (!case_source_is_class (vfm_source, &input_program_source_class))
81 msg (SE, _("No matching INPUT PROGRAM command."));
85 if (dict_get_next_value_idx (default_dict) == 0)
86 msg (SW, _("No data-input or transformation commands specified "
87 "between INPUT PROGRAM and END INPUT PROGRAM."));
89 /* Mark the boundary between INPUT PROGRAM transformations and
90 ordinary transformations. */
93 /* Figure out how to initialize each input case. */
94 inp = xmalloc (sizeof *inp);
95 inp->init_cnt = dict_get_next_value_idx (default_dict);
96 inp->init = xmalloc (inp->init_cnt * sizeof *inp->init);
97 for (i = 0; i < inp->init_cnt; i++)
99 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
101 struct variable *var = dict_get_var (default_dict, i);
102 enum value_init_type value_init;
105 value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
106 value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE;
108 for (j = 0; j < var->nv; j++)
109 inp->init[j + var->fv] = value_init;
111 for (i = 0; i < inp->init_cnt; i++)
112 assert (inp->init[i] != -1);
113 inp->case_size = dict_get_case_size (default_dict);
115 /* Put inp into vfm_source for later use. */
116 vfm_source->aux = inp;
118 return lex_end_of_command ();
121 /* Initializes case C. Called before the first case is read. */
123 init_case (const struct input_program_pgm *inp, struct ccase *c)
127 for (i = 0; i < inp->init_cnt; i++)
128 switch (inp->init[i])
130 case INP_NUMERIC | INP_INIT_ONCE:
131 case_data_rw (c, i)->f = 0.0;
133 case INP_NUMERIC | INP_REINIT:
134 case_data_rw (c, i)->f = SYSMIS;
136 case INP_STRING | INP_INIT_ONCE:
137 case INP_STRING | INP_REINIT:
138 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
145 /* Clears case C. Called between reading successive records. */
147 clear_case (const struct input_program_pgm *inp, struct ccase *c)
151 for (i = 0; i < inp->init_cnt; i++)
152 switch (inp->init[i])
154 case INP_NUMERIC | INP_INIT_ONCE:
156 case INP_NUMERIC | INP_REINIT:
157 case_data_rw (c, i)->f = SYSMIS;
159 case INP_STRING | INP_INIT_ONCE:
161 case INP_STRING | INP_REINIT:
162 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
169 /* Executes each transformation in turn on a `blank' case. When a
170 transformation fails, returning -2, then that's the end of the
171 file. -1 means go on to the next transformation. Otherwise the
172 return value is the index of the transformation to go to next. */
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], write_case, wc_data);
208 /* Perform transformations on `blank' case. */
209 for (i = 0; i < f_trns; )
211 int code; /* Return value of last-called transformation. */
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], c, cases_written + 1);
239 /* Write the case if appropriate. */
243 if (!write_case (wc_data))
247 /* Blank out the case for the next iteration. */
254 /* Destroys an INPUT PROGRAM source. */
256 input_program_source_destroy (struct case_source *source)
258 struct input_program_pgm *inp = source->aux;
260 cancel_transformations ();
269 const struct case_source_class input_program_source_class =
273 input_program_source_read,
274 input_program_source_destroy,
280 struct trns_header *t;
282 if (!case_source_is_class (vfm_source, &input_program_source_class))
284 msg (SE, _("This command may only be executed between INPUT PROGRAM "
285 "and END INPUT PROGRAM."));
289 t = xmalloc (sizeof *t);
290 t->proc = end_case_trns_proc;
292 add_transformation ((struct trns_header *) t);
294 return lex_end_of_command ();
297 /* Should never be called, because this is handled in
298 input_program_source_read(). */
300 end_case_trns_proc (struct trns_header *t UNUSED, struct ccase * c UNUSED,
307 /* REREAD transformation. */
310 struct trns_header h;
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. */
328 if (lex_match_id ("COLUMN"))
334 msg (SE, _("COLUMN subcommand multiply specified."));
339 e = expr_parse (default_dict, EXPR_NUMBER);
343 else if (lex_match_id ("FILE"))
361 t = xmalloc (sizeof *t);
362 t->h.proc = reread_trns_proc;
363 t->h.free = reread_trns_free;
364 t->reader = dfm_open_reader (fh);
366 add_transformation ((struct trns_header *) t);
371 /* Executes a REREAD transformation. */
373 reread_trns_proc (struct trns_header * pt, struct ccase * c,
376 struct reread_trns *t = (struct reread_trns *) pt;
378 if (t->column == NULL)
379 dfm_reread_record (t->reader, 1);
382 double column = expr_evaluate_num (t->column, c, case_num);
383 if (!finite (column) || column < 1)
385 msg (SE, _("REREAD: Column numbers must be positive finite "
386 "numbers. Column set to 1."));
387 dfm_reread_record (t->reader, 1);
390 dfm_reread_record (t->reader, column);
395 /* Frees a REREAD transformation. */
397 reread_trns_free (struct trns_header *t_)
399 struct reread_trns *t = (struct reread_trns *) t_;
400 expr_free (t->column);
401 dfm_close_reader (t->reader);
404 /* Parses END FILE command. */
408 struct trns_header *t;
410 if (!case_source_is_class (vfm_source, &input_program_source_class))
412 msg (SE, _("This command may only be executed between INPUT PROGRAM "
413 "and END INPUT PROGRAM."));
417 t = xmalloc (sizeof *t);
418 t->proc = end_file_trns_proc;
420 add_transformation ((struct trns_header *) t);
422 return lex_end_of_command ();
425 /* Executes an END FILE transformation. */
427 end_file_trns_proc (struct trns_header * t UNUSED, struct ccase * c UNUSED,