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
29 #include "file-handle.h"
38 /*#define DEBUGGING 1*/
39 #include "debug-print.h"
41 /* A bit-vector of two-bit entries. The array tells INPUT PROGRAM how
42 to initialize each `value'. Modified by envector(), devector(),
43 which are called by create_variable(), also by LEAVE, COMPUTE(!). */
44 unsigned char *inp_init;
46 /* Number of bytes allocated for inp_init. */
49 /* Number of `values' created inside INPUT PROGRAM. */
52 static int end_case_trns_proc (struct trns_header *, struct ccase *);
53 static int end_file_trns_proc (struct trns_header * t, struct ccase * c);
54 static int reread_trns_proc (struct trns_header *, struct ccase *);
55 static void reread_trns_free (struct trns_header *);
58 cmd_input_program (void)
60 lex_match_id ("INPUT");
61 lex_match_id ("PROGRAM");
64 vfm_source = &input_program_source;
69 return lex_end_of_command ();
73 cmd_end_input_program (void)
76 lex_match_id ("INPUT");
77 lex_match_id ("PROGRAM");
79 if (vfm_source != &input_program_source)
81 msg (SE, _("No matching INPUT PROGRAM command."));
85 if (default_dict.nval == 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 and more-mundane
93 /* Mark the boundary between input program `values' and
94 later-created `values'. */
95 inp_nval = default_dict.nval;
97 return lex_end_of_command ();
100 /* Initializes temp_case. Called before the first case is read. */
104 union value *val = temp_case->data;
105 unsigned char *cp = inp_init;
109 /* This code is 2-3X the complexity it might be, but I felt like
110 it. It initializes temp_case union values to 0, or SYSMIS, or
111 blanks, as appropriate. */
112 for (i = 0; i < inp_nval / 4; i++)
115 for (j = 0; j < 4; j++)
117 switch (c & INP_MASK)
119 case INP_NUMERIC | INP_RIGHT:
122 case INP_NUMERIC | INP_LEFT:
125 case INP_STRING | INP_RIGHT:
126 case INP_STRING | INP_LEFT:
127 memset (val++->s, ' ', MAX_SHORT_STRING);
136 for (j = 0; j < inp_nval % 4; j++)
138 switch (c & INP_MASK)
140 case INP_NUMERIC | INP_RIGHT:
143 case INP_NUMERIC | INP_LEFT:
146 case INP_STRING | INP_RIGHT:
147 case INP_STRING | INP_LEFT:
148 memset (val++->s, ' ', MAX_SHORT_STRING);
156 /* Clears temp_case. Called between reading successive records. */
160 union value *val = temp_case->data;
161 unsigned char *cp = inp_init;
165 /* This code is 2-3X the complexity it might be, but I felt like
166 it. It initializes temp_case values to SYSMIS, or
167 blanks, or does nothing, as appropriate. */
168 for (i = 0; i < inp_nval / 4; i++)
171 for (j = 0; j < 4; j++)
176 memset (val->s, ' ', MAX_SHORT_STRING);
188 for (j = 0; j < inp_nval % 4; j++)
193 memset (val->s, ' ', MAX_SHORT_STRING);
203 /* Executes each transformation in turn on a `blank' case. When a
204 transformation fails, returning -2, then that's the end of the
205 file. -1 means go on to the next transformation. Otherwise the
206 return value is the index of the transformation to go to next. */
208 input_program_source_read (void)
212 /* Nonzero if there were any END CASE commands in the set of
216 /* We don't automatically write out cases if the user took over
218 for (i = 0; i < f_trns; i++)
219 if (t_trns[i]->proc == end_case_trns_proc)
225 /* Index of current transformation. */
228 /* Return value of last-called transformation. */
231 debug_printf (("input-program: "));
233 /* Perform transformations on `blank' case. */
234 for (i = 0; i < f_trns;)
238 if (t_trns[i]->proc == end_case_trns_proc)
241 code = t_trns[i]->proc (t_trns[i], temp_case);
262 /* Write the case if appropriate. */
267 /* Blank out the case for the next iteration. */
274 input_program_source_destroy_source (void)
276 cancel_transformations ();
281 struct case_stream input_program_source =
284 input_program_source_read,
287 input_program_source_destroy_source,
295 struct trns_header *t;
297 lex_match_id ("END");
298 lex_match_id ("CASE");
300 if (vfm_source != &input_program_source)
302 msg (SE, _("This command may only be executed between INPUT PROGRAM "
303 "and END INPUT PROGRAM."));
307 t = xmalloc (sizeof *t);
308 t->proc = end_case_trns_proc;
310 add_transformation ((struct trns_header *) t);
312 return lex_end_of_command ();
316 end_case_trns_proc (struct trns_header *t unused, struct ccase * c unused)
319 printf ("END CASE\n");
327 /* REREAD transformation. */
330 struct trns_header h;
332 struct file_handle *handle; /* File to move file pointer back on. */
333 struct expression *column; /* Column to reset file pointer to. */
336 /* Parses REREAD command. */
340 /* File to be re-read. */
341 struct file_handle *h;
343 /* Expression for column to set file pointer to. */
344 struct expression *e;
346 /* Created transformation. */
347 struct reread_trns *t;
349 lex_match_id ("REREAD");
355 if (lex_match_id ("COLUMN"))
361 msg (SE, _("COLUMN subcommand multiply specified."));
366 e = expr_parse (PXP_NUMERIC);
370 else if (lex_match_id ("FILE"))
375 lex_error (_("expecting file handle name"));
379 h = fh_get_handle_by_name (tokid);
394 t = xmalloc (sizeof *t);
395 t->h.proc = reread_trns_proc;
396 t->h.free = reread_trns_free;
399 add_transformation ((struct trns_header *) t);
405 reread_trns_proc (struct trns_header * pt, struct ccase * c)
407 struct reread_trns *t = (struct reread_trns *) pt;
409 if (t->column == NULL)
410 dfm_bkwd_record (t->handle, 1);
415 expr_evaluate (t->column, c, &column);
416 if (!finite (column.f) || column.f < 1)
418 msg (SE, _("REREAD: Column numbers must be positive finite "
419 "numbers. Column set to 1."));
420 dfm_bkwd_record (t->handle, 1);
423 dfm_bkwd_record (t->handle, column.f);
429 reread_trns_free (struct trns_header * t)
431 expr_free (((struct reread_trns *) t)->column);
434 /* Parses END FILE command. */
438 struct trns_header *t;
440 lex_match_id ("END");
441 lex_match_id ("FILE");
443 if (vfm_source != &input_program_source)
445 msg (SE, _("This command may only be executed between INPUT PROGRAM "
446 "and END INPUT PROGRAM."));
450 t = xmalloc (sizeof *t);
451 t->proc = end_file_trns_proc;
453 add_transformation ((struct trns_header *) t);
455 return lex_end_of_command ();
459 end_file_trns_proc (struct trns_header * t unused, struct ccase * c unused)
462 printf ("END FILE\n");