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"
37 #include "debug-print.h"
39 /* A bit-vector of two-bit entries. The array tells INPUT PROGRAM how
40 to initialize each `value'. Modified by envector(), devector(),
41 which are called by create_variable(), also by LEAVE, COMPUTE(!). */
42 unsigned char *inp_init;
44 /* Number of bytes allocated for inp_init. */
47 /* Number of `values' created inside INPUT PROGRAM. */
50 static int end_case_trns_proc (struct trns_header *, struct ccase *);
51 static int end_file_trns_proc (struct trns_header * t, struct ccase * c);
52 static int reread_trns_proc (struct trns_header *, struct ccase *);
53 static void reread_trns_free (struct trns_header *);
56 cmd_input_program (void)
58 lex_match_id ("INPUT");
59 lex_match_id ("PROGRAM");
62 vfm_source = &input_program_source;
67 return lex_end_of_command ();
71 cmd_end_input_program (void)
74 lex_match_id ("INPUT");
75 lex_match_id ("PROGRAM");
77 if (vfm_source != &input_program_source)
79 msg (SE, _("No matching INPUT PROGRAM command."));
83 if (default_dict.nval == 0)
84 msg (SW, _("No data-input or transformation commands specified "
85 "between INPUT PROGRAM and END INPUT PROGRAM."));
87 /* Mark the boundary between INPUT PROGRAM and more-mundane
91 /* Mark the boundary between input program `values' and
92 later-created `values'. */
93 inp_nval = default_dict.nval;
95 return lex_end_of_command ();
98 /* Initializes temp_case. Called before the first case is read. */
102 union value *val = temp_case->data;
103 unsigned char *cp = inp_init;
107 /* This code is 2-3X the complexity it might be, but I felt like
108 it. It initializes temp_case union values to 0, or SYSMIS, or
109 blanks, as appropriate. */
110 for (i = 0; i < inp_nval / 4; i++)
113 for (j = 0; j < 4; j++)
115 switch (c & INP_MASK)
117 case INP_NUMERIC | INP_RIGHT:
120 case INP_NUMERIC | INP_LEFT:
123 case INP_STRING | INP_RIGHT:
124 case INP_STRING | INP_LEFT:
125 memset (val++->s, ' ', MAX_SHORT_STRING);
134 for (j = 0; j < inp_nval % 4; j++)
136 switch (c & INP_MASK)
138 case INP_NUMERIC | INP_RIGHT:
141 case INP_NUMERIC | INP_LEFT:
144 case INP_STRING | INP_RIGHT:
145 case INP_STRING | INP_LEFT:
146 memset (val++->s, ' ', MAX_SHORT_STRING);
154 /* Clears temp_case. Called between reading successive records. */
158 union value *val = temp_case->data;
159 unsigned char *cp = inp_init;
163 /* This code is 2-3X the complexity it might be, but I felt like
164 it. It initializes temp_case values to SYSMIS, or
165 blanks, or does nothing, as appropriate. */
166 for (i = 0; i < inp_nval / 4; i++)
169 for (j = 0; j < 4; j++)
174 memset (val->s, ' ', MAX_SHORT_STRING);
186 for (j = 0; j < inp_nval % 4; j++)
191 memset (val->s, ' ', MAX_SHORT_STRING);
201 /* Executes each transformation in turn on a `blank' case. When a
202 transformation fails, returning -2, then that's the end of the
203 file. -1 means go on to the next transformation. Otherwise the
204 return value is the index of the transformation to go to next. */
206 input_program_source_read (void)
210 /* Nonzero if there were any END CASE commands in the set of
214 /* We don't automatically write out cases if the user took over
216 for (i = 0; i < f_trns; i++)
217 if (t_trns[i]->proc == end_case_trns_proc)
223 /* Index of current transformation. */
226 /* Return value of last-called transformation. */
229 debug_printf (("input-program: "));
231 /* Perform transformations on `blank' case. */
232 for (i = 0; i < f_trns;)
236 if (t_trns[i]->proc == end_case_trns_proc)
239 code = t_trns[i]->proc (t_trns[i], temp_case);
260 /* Write the case if appropriate. */
265 /* Blank out the case for the next iteration. */
272 input_program_source_destroy_source (void)
274 cancel_transformations ();
279 struct case_stream input_program_source =
282 input_program_source_read,
285 input_program_source_destroy_source,
293 struct trns_header *t;
295 lex_match_id ("END");
296 lex_match_id ("CASE");
298 if (vfm_source != &input_program_source)
300 msg (SE, _("This command may only be executed between INPUT PROGRAM "
301 "and END INPUT PROGRAM."));
305 t = xmalloc (sizeof *t);
306 t->proc = end_case_trns_proc;
308 add_transformation ((struct trns_header *) t);
310 return lex_end_of_command ();
314 end_case_trns_proc (struct trns_header *t unused, struct ccase * c unused)
317 printf ("END CASE\n");
325 /* REREAD transformation. */
328 struct trns_header h;
330 struct file_handle *handle; /* File to move file pointer back on. */
331 struct expression *column; /* Column to reset file pointer to. */
334 /* Parses REREAD command. */
338 /* File to be re-read. */
339 struct file_handle *h;
341 /* Expression for column to set file pointer to. */
342 struct expression *e;
344 /* Created transformation. */
345 struct reread_trns *t;
347 lex_match_id ("REREAD");
353 if (lex_match_id ("COLUMN"))
359 msg (SE, _("COLUMN subcommand multiply specified."));
364 e = expr_parse (PXP_NUMERIC);
368 else if (lex_match_id ("FILE"))
373 lex_error (_("expecting file handle name"));
377 h = fh_get_handle_by_name (tokid);
392 t = xmalloc (sizeof *t);
393 t->h.proc = reread_trns_proc;
394 t->h.free = reread_trns_free;
397 add_transformation ((struct trns_header *) t);
403 reread_trns_proc (struct trns_header * pt, struct ccase * c)
405 struct reread_trns *t = (struct reread_trns *) pt;
407 if (t->column == NULL)
408 dfm_bkwd_record (t->handle, 1);
413 expr_evaluate (t->column, c, &column);
414 if (!finite (column.f) || column.f < 1)
416 msg (SE, _("REREAD: Column numbers must be positive finite "
417 "numbers. Column set to 1."));
418 dfm_bkwd_record (t->handle, 1);
421 dfm_bkwd_record (t->handle, column.f);
427 reread_trns_free (struct trns_header * t)
429 expr_free (((struct reread_trns *) t)->column);
432 /* Parses END FILE command. */
436 struct trns_header *t;
438 lex_match_id ("END");
439 lex_match_id ("FILE");
441 if (vfm_source != &input_program_source)
443 msg (SE, _("This command may only be executed between INPUT PROGRAM "
444 "and END INPUT PROGRAM."));
448 t = xmalloc (sizeof *t);
449 t->proc = end_file_trns_proc;
451 add_transformation ((struct trns_header *) t);
453 return lex_end_of_command ();
457 end_file_trns_proc (struct trns_header * t unused, struct ccase * c unused)
460 printf ("END FILE\n");