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
27 #include "data-list.h"
29 #include "dictionary.h"
31 #include "expressions/public.h"
32 #include "file-handle.h"
40 #define _(msgid) gettext (msgid)
42 #include "debug-print.h"
44 /* Indicates how a `union value' should be initialized. */
47 INP_NUMERIC = 01, /* Numeric. */
48 INP_STRING = 0, /* String. */
50 INP_INIT_ONCE = 02, /* Initialize only once. */
51 INP_REINIT = 0, /* Reinitialize for each iteration. */
54 struct input_program_pgm
56 enum value_init_type *init; /* How to initialize each `union value'. */
57 size_t init_cnt; /* Number of elements in inp_init. */
58 size_t case_size; /* Size of case in bytes. */
61 static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
62 static trns_free_func reread_trns_free;
65 cmd_input_program (void)
69 /* FIXME: we shouldn't do this here, but I'm afraid that other
70 code will check the class of vfm_source. */
71 vfm_source = create_case_source (&input_program_source_class, NULL);
73 return lex_end_of_command ();
77 cmd_end_input_program (void)
79 struct input_program_pgm *inp;
82 if (!case_source_is_class (vfm_source, &input_program_source_class))
84 msg (SE, _("No matching INPUT PROGRAM command."));
88 if (dict_get_next_value_idx (default_dict) == 0)
89 msg (SW, _("No data-input or transformation commands specified "
90 "between INPUT PROGRAM and END INPUT PROGRAM."));
92 /* Mark the boundary between INPUT PROGRAM transformations and
93 ordinary transformations. */
96 /* Figure out how to initialize each input case. */
97 inp = xmalloc (sizeof *inp);
98 inp->init_cnt = dict_get_next_value_idx (default_dict);
99 inp->init = xmalloc (inp->init_cnt * sizeof *inp->init);
100 for (i = 0; i < inp->init_cnt; i++)
102 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
104 struct variable *var = dict_get_var (default_dict, i);
105 enum value_init_type value_init;
108 value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
109 value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE;
111 for (j = 0; j < var->nv; j++)
112 inp->init[j + var->fv] = value_init;
114 for (i = 0; i < inp->init_cnt; i++)
115 assert (inp->init[i] != -1);
116 inp->case_size = dict_get_case_size (default_dict);
118 /* Put inp into vfm_source for later use. */
119 vfm_source->aux = inp;
121 return lex_end_of_command ();
124 /* Initializes case C. Called before the first case is read. */
126 init_case (const struct input_program_pgm *inp, struct ccase *c)
130 for (i = 0; i < inp->init_cnt; i++)
131 switch (inp->init[i])
133 case INP_NUMERIC | INP_INIT_ONCE:
134 case_data_rw (c, i)->f = 0.0;
136 case INP_NUMERIC | INP_REINIT:
137 case_data_rw (c, i)->f = SYSMIS;
139 case INP_STRING | INP_INIT_ONCE:
140 case INP_STRING | INP_REINIT:
141 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
148 /* Clears case C. Called between reading successive records. */
150 clear_case (const struct input_program_pgm *inp, struct ccase *c)
154 for (i = 0; i < inp->init_cnt; i++)
155 switch (inp->init[i])
157 case INP_NUMERIC | INP_INIT_ONCE:
159 case INP_NUMERIC | INP_REINIT:
160 case_data_rw (c, i)->f = SYSMIS;
162 case INP_STRING | INP_INIT_ONCE:
164 case INP_STRING | INP_REINIT:
165 memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
172 /* Executes each transformation in turn on a `blank' case. When a
173 transformation fails, returning -2, then that's the end of the
174 file. -1 means go on to the next transformation. Otherwise the
175 return value is the index of the transformation to go to next. */
177 input_program_source_read (struct case_source *source,
179 write_case_func *write_case,
180 write_case_data wc_data)
182 struct input_program_pgm *inp = source->aux;
185 /* Nonzero if there were any END CASE commands in the set of
186 transformations. If so, we don't automatically write out
190 /* FIXME? This is the number of cases sent out of the input
191 program, not the number of cases written to the procedure.
192 The difference should only show up in $CASENUM in COMPUTE.
193 We should check behavior against SPSS. */
194 int cases_written = 0;
196 assert (inp != NULL);
198 /* Figure end_case. */
199 for (i = 0; i < f_trns; i++)
200 if (t_trns[i]->proc == end_case_trns_proc)
203 /* FIXME: This is an ugly kluge. */
204 for (i = 0; i < f_trns; i++)
205 if (t_trns[i]->proc == repeating_data_trns_proc)
206 repeating_data_set_write_case (t_trns[i], write_case, wc_data);
211 /* Perform transformations on `blank' case. */
212 for (i = 0; i < f_trns; )
214 int code; /* Return value of last-called transformation. */
216 if (t_trns[i]->proc == end_case_trns_proc)
219 if (!write_case (wc_data))
226 code = t_trns[i]->proc (t_trns[i], c, cases_written + 1);
242 /* Write the case if appropriate. */
246 if (!write_case (wc_data))
250 /* Blank out the case for the next iteration. */
257 /* Destroys an INPUT PROGRAM source. */
259 input_program_source_destroy (struct case_source *source)
261 struct input_program_pgm *inp = source->aux;
263 cancel_transformations ();
272 const struct case_source_class input_program_source_class =
276 input_program_source_read,
277 input_program_source_destroy,
283 struct trns_header *t;
285 if (!case_source_is_class (vfm_source, &input_program_source_class))
287 msg (SE, _("This command may only be executed between INPUT PROGRAM "
288 "and END INPUT PROGRAM."));
292 t = xmalloc (sizeof *t);
293 t->proc = end_case_trns_proc;
295 add_transformation ((struct trns_header *) t);
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 (struct trns_header *t UNUSED, struct ccase * c UNUSED,
310 /* REREAD transformation. */
313 struct trns_header h;
315 struct dfm_reader *reader; /* File to move file pointer back on. */
316 struct expression *column; /* Column to reset file pointer to. */
319 /* Parses REREAD command. */
323 struct file_handle *fh; /* File to be re-read. */
324 struct expression *e; /* Expression for column to set. */
325 struct reread_trns *t; /* Created transformation. */
331 if (lex_match_id ("COLUMN"))
337 msg (SE, _("COLUMN subcommand multiply specified."));
342 e = expr_parse (default_dict, EXPR_NUMBER);
346 else if (lex_match_id ("FILE"))
364 t = xmalloc (sizeof *t);
365 t->h.proc = reread_trns_proc;
366 t->h.free = reread_trns_free;
367 t->reader = dfm_open_reader (fh);
369 add_transformation ((struct trns_header *) t);
374 /* Executes a REREAD transformation. */
376 reread_trns_proc (struct trns_header * pt, struct ccase * c,
379 struct reread_trns *t = (struct reread_trns *) pt;
381 if (t->column == NULL)
382 dfm_reread_record (t->reader, 1);
385 double column = expr_evaluate_num (t->column, c, case_num);
386 if (!finite (column) || column < 1)
388 msg (SE, _("REREAD: Column numbers must be positive finite "
389 "numbers. Column set to 1."));
390 dfm_reread_record (t->reader, 1);
393 dfm_reread_record (t->reader, column);
398 /* Frees a REREAD transformation. */
400 reread_trns_free (struct trns_header *t_)
402 struct reread_trns *t = (struct reread_trns *) t_;
403 expr_free (t->column);
404 dfm_close_reader (t->reader);
407 /* Parses END FILE command. */
411 struct trns_header *t;
413 if (!case_source_is_class (vfm_source, &input_program_source_class))
415 msg (SE, _("This command may only be executed between INPUT PROGRAM "
416 "and END INPUT PROGRAM."));
420 t = xmalloc (sizeof *t);
421 t->proc = end_file_trns_proc;
423 add_transformation ((struct trns_header *) t);
425 return lex_end_of_command ();
428 /* Executes an END FILE transformation. */
430 end_file_trns_proc (struct trns_header * t UNUSED, struct ccase * c UNUSED,