0581249a841e6edd4ba8a15257a2ab2a29979ed3
[pspp-builds.git] / src / language / data-io / inpt-pgm.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <language/data-io/inpt-pgm.h>
23
24 #include <float.h>
25 #include <stdlib.h>
26
27 #include <data/case-source.h>
28 #include <data/case.h>
29 #include <data/case-source.h>
30 #include <data/dictionary.h>
31 #include <data/variable.h>
32 #include <language/command.h>
33 #include <language/data-io/data-reader.h>
34 #include <language/data-io/file-handle.h>
35 #include <language/expressions/public.h>
36 #include <language/lexer/lexer.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/compiler.h>
39 #include <libpspp/message.h>
40 #include <libpspp/message.h>
41 #include <libpspp/misc.h>
42 #include <libpspp/str.h>
43 #include <procedure.h>
44
45 #include "gettext.h"
46 #define _(msgid) gettext (msgid)
47
48 /* Private result codes for use within INPUT PROGRAM. */
49 enum cmd_result_extensions 
50   {
51     CMD_END_INPUT_PROGRAM = CMD_PRIVATE_FIRST,
52     CMD_END_CASE
53   };
54
55 /* Indicates how a `union value' should be initialized. */
56 enum value_init_type
57   {
58     INP_NUMERIC = 01,           /* Numeric. */
59     INP_STRING = 0,             /* String. */
60     
61     INP_INIT_ONCE = 02,         /* Initialize only once. */
62     INP_REINIT = 0,             /* Reinitialize for each iteration. */
63   };
64
65 struct input_program_pgm 
66   {
67     size_t case_nr;             /* Incremented by END CASE transformation. */
68     write_case_func *write_case;/* Called by END CASE. */
69     write_case_data wc_data;    /* Aux data used by END CASE. */
70
71     enum value_init_type *init; /* How to initialize each `union value'. */
72     size_t init_cnt;            /* Number of elements in inp_init. */
73     size_t case_size;           /* Size of case in bytes. */
74   };
75
76 static void destroy_input_program (struct input_program_pgm *);
77 static trns_proc_func end_case_trns_proc;
78 static trns_proc_func reread_trns_proc;
79 static trns_proc_func end_file_trns_proc;
80 static trns_free_func reread_trns_free;
81
82 static const struct case_source_class input_program_source_class;
83
84 static bool inside_input_program;
85
86 /* Returns true if we're parsing the inside of a INPUT
87    PROGRAM...END INPUT PROGRAM construct, false otherwise. */
88 bool
89 in_input_program (void) 
90 {
91   return inside_input_program;
92 }
93
94 /* Emits an END CASE transformation for INP. */
95 static void
96 emit_END_CASE (struct input_program_pgm *inp) 
97 {
98   add_transformation (end_case_trns_proc, NULL, inp);
99 }
100
101 int
102 cmd_input_program (void)
103 {
104   struct input_program_pgm *inp;
105   size_t i;
106   bool saw_END_CASE = false;
107
108   discard_variables ();
109   if (token != '.')
110     return lex_end_of_command ();
111
112   inp = xmalloc (sizeof *inp);
113   inp->init = NULL;
114   
115   inside_input_program = true;
116   for (;;) 
117     {
118       enum cmd_result result;
119       lex_get ();
120       result = cmd_parse (CMD_STATE_INPUT_PROGRAM);
121       if (result == CMD_END_INPUT_PROGRAM)
122         break;
123       else if (result == CMD_END_CASE) 
124         {
125           emit_END_CASE (inp);
126           saw_END_CASE = true; 
127         }
128       else if (cmd_result_is_failure (result) && result != CMD_FAILURE)
129         {
130           if (result == CMD_EOF)
131             msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
132           inside_input_program = false;
133           discard_variables ();
134           destroy_input_program (inp);
135           return result;
136         }
137     }
138   if (!saw_END_CASE)
139     emit_END_CASE (inp);
140   inside_input_program = false;
141
142   if (dict_get_next_value_idx (default_dict) == 0) 
143     {
144       msg (SE, _("Input program did not create any variables."));
145       discard_variables ();
146       destroy_input_program (inp);
147       return CMD_FAILURE;
148     }
149   
150   /* Mark the boundary between INPUT PROGRAM transformations and
151      ordinary transformations. */
152   f_trns = n_trns;
153
154   /* Figure out how to initialize each input case. */
155   inp->init_cnt = dict_get_next_value_idx (default_dict);
156   inp->init = xnmalloc (inp->init_cnt, sizeof *inp->init);
157   for (i = 0; i < inp->init_cnt; i++)
158     inp->init[i] = -1;
159   for (i = 0; i < dict_get_var_cnt (default_dict); i++)
160     {
161       struct variable *var = dict_get_var (default_dict, i);
162       enum value_init_type value_init;
163       size_t j;
164       
165       value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
166       value_init |= var->leave ? INP_INIT_ONCE : INP_REINIT;
167
168       for (j = 0; j < var->nv; j++)
169         inp->init[j + var->fv] = value_init;
170     }
171   for (i = 0; i < inp->init_cnt; i++)
172     assert (inp->init[i] != -1);
173   inp->case_size = dict_get_case_size (default_dict);
174
175   /* Create vfm_source. */
176   vfm_source = create_case_source (&input_program_source_class, inp);
177
178   return CMD_SUCCESS;
179 }
180
181 int
182 cmd_end_input_program (void)
183 {
184   assert (in_input_program ());
185   return CMD_END_INPUT_PROGRAM; 
186 }
187
188 /* Initializes case C.  Called before the first case is read. */
189 static void
190 init_case (const struct input_program_pgm *inp, struct ccase *c)
191 {
192   size_t i;
193
194   for (i = 0; i < inp->init_cnt; i++)
195     switch (inp->init[i]) 
196       {
197       case INP_NUMERIC | INP_INIT_ONCE:
198         case_data_rw (c, i)->f = 0.0;
199         break;
200       case INP_NUMERIC | INP_REINIT:
201         case_data_rw (c, i)->f = SYSMIS;
202         break;
203       case INP_STRING | INP_INIT_ONCE:
204       case INP_STRING | INP_REINIT:
205         memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
206         break;
207       default:
208         assert (0);
209       }
210 }
211
212 /* Clears case C.  Called between reading successive records. */
213 static void
214 clear_case (const struct input_program_pgm *inp, struct ccase *c)
215 {
216   size_t i;
217
218   for (i = 0; i < inp->init_cnt; i++)
219     switch (inp->init[i]) 
220       {
221       case INP_NUMERIC | INP_INIT_ONCE:
222         break;
223       case INP_NUMERIC | INP_REINIT:
224         case_data_rw (c, i)->f = SYSMIS;
225         break;
226       case INP_STRING | INP_INIT_ONCE:
227         break;
228       case INP_STRING | INP_REINIT:
229         memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
230         break;
231       default:
232         assert (0);
233       }
234 }
235
236 /* Executes each transformation in turn on a `blank' case.
237    Returns true if successful, false if an I/O error occurred. */
238 static bool
239 input_program_source_read (struct case_source *source,
240                            struct ccase *c,
241                            write_case_func *write_case,
242                            write_case_data wc_data)
243 {
244   struct input_program_pgm *inp = source->aux;
245
246   inp->case_nr = 1;
247   inp->write_case = write_case;
248   inp->wc_data = wc_data;
249   for (init_case (inp, c); ; clear_case (inp, c))
250     {
251       int i;
252       
253       /* Perform transformations on `blank' case. */
254       for (i = 0; i < f_trns; )
255         {
256           int code;
257
258           code = t_trns[i].proc (t_trns[i].private, c, inp->case_nr);
259           switch (code)
260             {
261             case TRNS_CONTINUE:
262               i++;
263               break;
264
265             case TRNS_DROP_CASE:
266               break;
267
268             case TRNS_ERROR:
269               return false;
270
271             case TRNS_NEXT_CASE:
272               goto next_case;
273
274             case TRNS_END_FILE:
275               return true;
276
277             default:
278               i = code;
279               break;
280             }
281         }
282     next_case: ;
283     }
284 }
285
286 static void
287 destroy_input_program (struct input_program_pgm *pgm) 
288 {
289   if (pgm != NULL) 
290     {
291       free (pgm->init);
292       free (pgm);
293     }
294 }
295
296 /* Destroys an INPUT PROGRAM source. */
297 static void
298 input_program_source_destroy (struct case_source *source)
299 {
300   struct input_program_pgm *inp = source->aux;
301
302   destroy_input_program (inp);
303 }
304
305 static const struct case_source_class input_program_source_class =
306   {
307     "INPUT PROGRAM",
308     NULL,
309     input_program_source_read,
310     input_program_source_destroy,
311   };
312 \f
313 int
314 cmd_end_case (void)
315 {
316   assert (in_input_program ());
317   if (token == '.')
318     return CMD_END_CASE;
319   return lex_end_of_command ();
320 }
321
322 /* Sends the current case as the source's output. */
323 int
324 end_case_trns_proc (void *inp_, struct ccase *c, int case_nr UNUSED)
325 {
326   struct input_program_pgm *inp = inp_;
327
328   if (!inp->write_case (inp->wc_data))
329     return TRNS_ERROR;
330
331   inp->case_nr++;
332   clear_case (inp, c);
333   return TRNS_CONTINUE;
334 }
335
336 /* REREAD transformation. */
337 struct reread_trns
338   {
339     struct dfm_reader *reader;  /* File to move file pointer back on. */
340     struct expression *column;  /* Column to reset file pointer to. */
341   };
342
343 /* Parses REREAD command. */
344 int
345 cmd_reread (void)
346 {
347   struct file_handle *fh;       /* File to be re-read. */
348   struct expression *e;         /* Expression for column to set. */
349   struct reread_trns *t;        /* Created transformation. */
350
351   fh = fh_get_default_handle ();
352   e = NULL;
353   while (token != '.')
354     {
355       if (lex_match_id ("COLUMN"))
356         {
357           lex_match ('=');
358           
359           if (e)
360             {
361               msg (SE, _("COLUMN subcommand multiply specified."));
362               expr_free (e);
363               return CMD_CASCADING_FAILURE;
364             }
365           
366           e = expr_parse (default_dict, EXPR_NUMBER);
367           if (!e)
368             return CMD_CASCADING_FAILURE;
369         }
370       else if (lex_match_id ("FILE"))
371         {
372           lex_match ('=');
373           fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
374           if (fh == NULL)
375             {
376               expr_free (e);
377               return CMD_CASCADING_FAILURE;
378             }
379           lex_get ();
380         }
381       else
382         {
383           lex_error (NULL);
384           expr_free (e);
385         }
386     }
387
388   t = xmalloc (sizeof *t);
389   t->reader = dfm_open_reader (fh);
390   t->column = e;
391   add_transformation (reread_trns_proc, reread_trns_free, t);
392
393   return CMD_SUCCESS;
394 }
395
396 /* Executes a REREAD transformation. */
397 static int
398 reread_trns_proc (void *t_, struct ccase *c, int case_num)
399 {
400   struct reread_trns *t = t_;
401
402   if (t->column == NULL)
403     dfm_reread_record (t->reader, 1);
404   else
405     {
406       double column = expr_evaluate_num (t->column, c, case_num);
407       if (!finite (column) || column < 1)
408         {
409           msg (SE, _("REREAD: Column numbers must be positive finite "
410                "numbers.  Column set to 1."));
411           dfm_reread_record (t->reader, 1);
412         }
413       else
414         dfm_reread_record (t->reader, column);
415     }
416   return TRNS_CONTINUE;
417 }
418
419 /* Frees a REREAD transformation.
420    Returns true if successful, false if an I/O error occurred. */
421 static bool
422 reread_trns_free (void *t_)
423 {
424   struct reread_trns *t = t_;
425   expr_free (t->column);
426   dfm_close_reader (t->reader);
427   return true;
428 }
429
430 /* Parses END FILE command. */
431 int
432 cmd_end_file (void)
433 {
434   assert (in_input_program ());
435
436   add_transformation (end_file_trns_proc, NULL, NULL);
437
438   return lex_end_of_command ();
439 }
440
441 /* Executes an END FILE transformation. */
442 static int
443 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
444                     int case_num UNUSED)
445 {
446   return TRNS_END_FILE;
447 }