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