Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[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
70     size_t case_nr;             /* Incremented by END CASE transformation. */
71     write_case_func *write_case;/* Called by END CASE. */
72     write_case_data wc_data;    /* Aux data used by END CASE. */
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 (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   /* Figure out how to initialize each input case. */
156   inp->init_cnt = dict_get_next_value_idx (dataset_dict (ds));
157   inp->init = xnmalloc (inp->init_cnt, sizeof *inp->init);
158   for (i = 0; i < inp->init_cnt; i++)
159     inp->init[i] = -1;
160   for (i = 0; i < dict_get_var_cnt (dataset_dict (ds)); i++)
161     {
162       struct variable *var = dict_get_var (dataset_dict (ds), i);
163       size_t value_cnt = var_get_value_cnt (var);
164       enum value_init_type value_init;
165       size_t j;
166       
167       value_init = var_is_numeric (var) ? INP_NUMERIC : INP_STRING;
168       value_init |= var_get_leave (var) ? INP_INIT_ONCE : INP_REINIT;
169
170       for (j = 0; j < value_cnt; j++)
171         inp->init[j + var_get_case_index (var)] = value_init;
172     }
173   for (i = 0; i < inp->init_cnt; i++)
174     assert (inp->init[i] != -1);
175   inp->case_size = dict_get_case_size (dataset_dict (ds));
176
177   proc_set_source (ds, 
178                   create_case_source (&input_program_source_class, inp));
179
180   return CMD_SUCCESS;
181 }
182
183 int
184 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
185 {
186   assert (in_input_program ());
187   return CMD_END_INPUT_PROGRAM; 
188 }
189
190 /* Initializes case C.  Called before the first case is read. */
191 static void
192 init_case (const struct input_program_pgm *inp, struct ccase *c)
193 {
194   size_t i;
195
196   for (i = 0; i < inp->init_cnt; i++)
197     switch (inp->init[i]) 
198       {
199       case INP_NUMERIC | INP_INIT_ONCE:
200         case_data_rw_idx (c, i)->f = 0.0;
201         break;
202       case INP_NUMERIC | INP_REINIT:
203         case_data_rw_idx (c, i)->f = SYSMIS;
204         break;
205       case INP_STRING | INP_INIT_ONCE:
206       case INP_STRING | INP_REINIT:
207         memset (case_data_rw_idx (c, i)->s, ' ',
208                 sizeof case_data_rw_idx (c, i)->s);
209         break;
210       default:
211         NOT_REACHED ();
212       }
213 }
214
215 /* Clears case C.  Called between reading successive records. */
216 static void
217 clear_case (const struct input_program_pgm *inp, struct ccase *c)
218 {
219   size_t i;
220
221   for (i = 0; i < inp->init_cnt; i++)
222     switch (inp->init[i]) 
223       {
224       case INP_NUMERIC | INP_INIT_ONCE:
225         break;
226       case INP_NUMERIC | INP_REINIT:
227         case_data_rw_idx (c, i)->f = SYSMIS;
228         break;
229       case INP_STRING | INP_INIT_ONCE:
230         break;
231       case INP_STRING | INP_REINIT:
232         memset (case_data_rw_idx (c, i)->s, ' ',
233                 sizeof case_data_rw_idx (c, i)->s);
234         break;
235       default:
236         NOT_REACHED ();
237       }
238 }
239
240 /* Executes each transformation in turn on a `blank' case.
241    Returns true if successful, false if an I/O error occurred. */
242 static bool
243 input_program_source_read (struct case_source *source,
244                            struct ccase *c,
245                            write_case_func *write_case,
246                            write_case_data wc_data)
247 {
248   struct input_program_pgm *inp = source->aux;
249
250   inp->case_nr = 1;
251   inp->write_case = write_case;
252   inp->wc_data = wc_data;
253   for (init_case (inp, c); ; clear_case (inp, c))
254     {
255       enum trns_result result = trns_chain_execute (inp->trns_chain, c,
256                                                     &inp->case_nr);
257       if (result == TRNS_ERROR)
258         return false;
259       else if (result == TRNS_END_FILE)
260         return true;
261     }
262 }
263
264 static void
265 destroy_input_program (struct input_program_pgm *pgm) 
266 {
267   if (pgm != NULL) 
268     {
269       trns_chain_destroy (pgm->trns_chain);
270       free (pgm->init);
271       free (pgm);
272     }
273 }
274
275 /* Destroys an INPUT PROGRAM source. */
276 static void
277 input_program_source_destroy (struct case_source *source)
278 {
279   struct input_program_pgm *inp = source->aux;
280
281   destroy_input_program (inp);
282 }
283
284 static const struct case_source_class input_program_source_class =
285   {
286     "INPUT PROGRAM",
287     NULL,
288     input_program_source_read,
289     input_program_source_destroy,
290   };
291 \f
292 int
293 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
294 {
295   assert (in_input_program ());
296   if (lex_token (lexer) == '.')
297     return CMD_END_CASE;
298   return lex_end_of_command (lexer);
299 }
300
301 /* Sends the current case as the source's output. */
302 int
303 end_case_trns_proc (void *inp_, struct ccase *c, casenumber case_nr UNUSED)
304 {
305   struct input_program_pgm *inp = inp_;
306
307   if (!inp->write_case (inp->wc_data))
308     return TRNS_ERROR;
309
310   inp->case_nr++;
311   clear_case (inp, c);
312   return TRNS_CONTINUE;
313 }
314
315 /* REREAD transformation. */
316 struct reread_trns
317   {
318     struct dfm_reader *reader;  /* File to move file pointer back on. */
319     struct expression *column;  /* Column to reset file pointer to. */
320   };
321
322 /* Parses REREAD command. */
323 int
324 cmd_reread (struct lexer *lexer, struct dataset *ds)
325 {
326   struct file_handle *fh;       /* File to be re-read. */
327   struct expression *e;         /* Expression for column to set. */
328   struct reread_trns *t;        /* Created transformation. */
329
330   fh = fh_get_default_handle ();
331   e = NULL;
332   while (lex_token (lexer) != '.')
333     {
334       if (lex_match_id (lexer, "COLUMN"))
335         {
336           lex_match (lexer, '=');
337           
338           if (e)
339             {
340               msg (SE, _("COLUMN subcommand multiply specified."));
341               expr_free (e);
342               return CMD_CASCADING_FAILURE;
343             }
344           
345           e = expr_parse (lexer, ds, EXPR_NUMBER);
346           if (!e)
347             return CMD_CASCADING_FAILURE;
348         }
349       else if (lex_match_id (lexer, "FILE"))
350         {
351           lex_match (lexer, '=');
352           fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
353           if (fh == NULL)
354             {
355               expr_free (e);
356               return CMD_CASCADING_FAILURE;
357             }
358         }
359       else
360         {
361           lex_error (lexer, NULL);
362           expr_free (e);
363           return CMD_CASCADING_FAILURE;
364         }
365     }
366
367   t = xmalloc (sizeof *t);
368   t->reader = dfm_open_reader (fh, lexer);
369   t->column = e;
370   add_transformation (ds, reread_trns_proc, reread_trns_free, t);
371
372   return CMD_SUCCESS;
373 }
374
375 /* Executes a REREAD transformation. */
376 static int
377 reread_trns_proc (void *t_, struct ccase *c, casenumber case_num)
378 {
379   struct reread_trns *t = t_;
380
381   if (t->column == NULL)
382     dfm_reread_record (t->reader, 1);
383   else
384     {
385       double column = expr_evaluate_num (t->column, c, case_num);
386       if (!finite (column) || column < 1)
387         {
388           msg (SE, _("REREAD: Column numbers must be positive finite "
389                "numbers.  Column set to 1."));
390           dfm_reread_record (t->reader, 1);
391         }
392       else
393         dfm_reread_record (t->reader, column);
394     }
395   return TRNS_CONTINUE;
396 }
397
398 /* Frees a REREAD transformation.
399    Returns true if successful, false if an I/O error occurred. */
400 static bool
401 reread_trns_free (void *t_)
402 {
403   struct reread_trns *t = t_;
404   expr_free (t->column);
405   dfm_close_reader (t->reader);
406   return true;
407 }
408
409 /* Parses END FILE command. */
410 int
411 cmd_end_file (struct lexer *lexer, struct dataset *ds)
412 {
413   assert (in_input_program ());
414
415   add_transformation (ds, end_file_trns_proc, NULL, NULL);
416
417   return lex_end_of_command (lexer);
418 }
419
420 /* Executes an END FILE transformation. */
421 static int
422 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
423                     casenumber case_num UNUSED)
424 {
425   return TRNS_END_FILE;
426 }