Changed include paths to be explicitly specified in the #include directive.
[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 #include <libpspp/message.h>
22 #include <float.h>
23 #include <stdlib.h>
24 #include <libpspp/alloc.h>
25 #include <data/case.h>
26 #include <language/command.h>
27 #include <libpspp/compiler.h>
28 #include <language/data-io/data-list.h>
29 #include <language/data-io/data-reader.h>
30 #include <data/dictionary.h>
31 #include <libpspp/message.h>
32 #include <language/expressions/public.h>
33 #include <language/data-io/file-handle.h>
34 #include <language/lexer/lexer.h>
35 #include <libpspp/misc.h>
36 #include <libpspp/str.h>
37 #include <data/variable.h>
38 #include <procedure.h>
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43 #include <libpspp/debug-print.h>
44
45 /* Indicates how a `union value' should be initialized. */
46 enum value_init_type
47   {
48     INP_NUMERIC = 01,           /* Numeric. */
49     INP_STRING = 0,             /* String. */
50     
51     INP_INIT_ONCE = 02,         /* Initialize only once. */
52     INP_REINIT = 0,             /* Reinitialize for each iteration. */
53   };
54
55 struct input_program_pgm 
56   {
57     enum value_init_type *init; /* How to initialize each `union value'. */
58     size_t init_cnt;            /* Number of elements in inp_init. */
59     size_t case_size;           /* Size of case in bytes. */
60   };
61
62 static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
63 static trns_free_func reread_trns_free;
64
65 int
66 cmd_input_program (void)
67 {
68   discard_variables ();
69
70   /* FIXME: we shouldn't do this here, but I'm afraid that other
71      code will check the class of vfm_source. */
72   vfm_source = create_case_source (&input_program_source_class, NULL);
73
74   return lex_end_of_command ();
75 }
76
77 int
78 cmd_end_input_program (void)
79 {
80   struct input_program_pgm *inp;
81   size_t i;
82
83   if (!case_source_is_class (vfm_source, &input_program_source_class))
84     {
85       msg (SE, _("No matching INPUT PROGRAM command."));
86       return CMD_CASCADING_FAILURE;
87     }
88   
89   if (dict_get_next_value_idx (default_dict) == 0)
90     msg (SW, _("No data-input or transformation commands specified "
91          "between INPUT PROGRAM and END INPUT PROGRAM."));
92
93   /* Mark the boundary between INPUT PROGRAM transformations and
94      ordinary transformations. */
95   f_trns = n_trns;
96
97   /* Figure out how to initialize each input case. */
98   inp = xmalloc (sizeof *inp);
99   inp->init_cnt = dict_get_next_value_idx (default_dict);
100   inp->init = xnmalloc (inp->init_cnt, sizeof *inp->init);
101   for (i = 0; i < inp->init_cnt; i++)
102     inp->init[i] = -1;
103   for (i = 0; i < dict_get_var_cnt (default_dict); i++)
104     {
105       struct variable *var = dict_get_var (default_dict, i);
106       enum value_init_type value_init;
107       size_t j;
108       
109       value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
110       value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE;
111
112       for (j = 0; j < var->nv; j++)
113         inp->init[j + var->fv] = value_init;
114     }
115   for (i = 0; i < inp->init_cnt; i++)
116     assert (inp->init[i] != -1);
117   inp->case_size = dict_get_case_size (default_dict);
118
119   /* Put inp into vfm_source for later use. */
120   vfm_source->aux = inp;
121
122   return lex_end_of_command ();
123 }
124
125 /* Initializes case C.  Called before the first case is read. */
126 static void
127 init_case (const struct input_program_pgm *inp, struct ccase *c)
128 {
129   size_t i;
130
131   for (i = 0; i < inp->init_cnt; i++)
132     switch (inp->init[i]) 
133       {
134       case INP_NUMERIC | INP_INIT_ONCE:
135         case_data_rw (c, i)->f = 0.0;
136         break;
137       case INP_NUMERIC | INP_REINIT:
138         case_data_rw (c, i)->f = SYSMIS;
139         break;
140       case INP_STRING | INP_INIT_ONCE:
141       case INP_STRING | INP_REINIT:
142         memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
143         break;
144       default:
145         assert (0);
146       }
147 }
148
149 /* Clears case C.  Called between reading successive records. */
150 static void
151 clear_case (const struct input_program_pgm *inp, struct ccase *c)
152 {
153   size_t i;
154
155   for (i = 0; i < inp->init_cnt; i++)
156     switch (inp->init[i]) 
157       {
158       case INP_NUMERIC | INP_INIT_ONCE:
159         break;
160       case INP_NUMERIC | INP_REINIT:
161         case_data_rw (c, i)->f = SYSMIS;
162         break;
163       case INP_STRING | INP_INIT_ONCE:
164         break;
165       case INP_STRING | INP_REINIT:
166         memset (case_data_rw (c, i)->s, ' ', sizeof case_data_rw (c, i)->s);
167         break;
168       default:
169         assert (0);
170       }
171 }
172
173 /* Executes each transformation in turn on a `blank' case.
174    Returns true if successful, false if an I/O error occurred. */
175 static bool
176 input_program_source_read (struct case_source *source,
177                            struct ccase *c,
178                            write_case_func *write_case,
179                            write_case_data wc_data)
180 {
181   struct input_program_pgm *inp = source->aux;
182   size_t i;
183
184   /* Nonzero if there were any END CASE commands in the set of
185      transformations.  If so, we don't automatically write out
186      cases. */
187   int end_case = 0;
188
189   /* FIXME?  This is the number of cases sent out of the input
190      program, not the number of cases written to the procedure.
191      The difference should only show up in $CASENUM in COMPUTE.
192      We should check behavior against SPSS. */
193   int cases_written = 0;
194
195   assert (inp != NULL);
196
197   /* Figure end_case. */
198   for (i = 0; i < f_trns; i++)
199     if (t_trns[i].proc == end_case_trns_proc)
200       end_case = 1;
201
202   /* FIXME: This is an ugly kluge. */
203   for (i = 0; i < f_trns; i++)
204     if (t_trns[i].proc == repeating_data_trns_proc)
205       repeating_data_set_write_case (t_trns[i].private, write_case, wc_data);
206
207   init_case (inp, c);
208   for (;;)
209     {
210       /* Perform transformations on `blank' case. */
211       for (i = 0; i < f_trns; )
212         {
213           int code;
214
215           if (t_trns[i].proc == end_case_trns_proc) 
216             {
217               cases_written++;
218               if (!write_case (wc_data))
219                 return false;
220               clear_case (inp, c);
221               i++;
222               continue;
223             }
224
225           code = t_trns[i].proc (t_trns[i].private, c, cases_written + 1);
226           switch (code)
227             {
228             case TRNS_CONTINUE:
229               i++;
230               break;
231
232             case TRNS_DROP_CASE:
233               abort ();
234
235             case TRNS_ERROR:
236               return false;
237
238             case TRNS_NEXT_CASE:
239               goto next_case;
240
241             case TRNS_END_FILE:
242               return true;
243
244             default:
245               i = code;
246               break;
247             }
248         }
249
250       /* Write the case if appropriate. */
251       if (!end_case) 
252         {
253           cases_written++;
254           if (!write_case (wc_data))
255             return false;
256         }
257
258       /* Blank out the case for the next iteration. */
259     next_case:
260       clear_case (inp, c);
261     }
262 }
263
264 /* Destroys an INPUT PROGRAM source. */
265 static void
266 input_program_source_destroy (struct case_source *source)
267 {
268   struct input_program_pgm *inp = source->aux;
269
270   cancel_transformations ();
271
272   if (inp != NULL) 
273     {
274       free (inp->init);
275       free (inp);
276     }
277 }
278
279 const struct case_source_class input_program_source_class =
280   {
281     "INPUT PROGRAM",
282     NULL,
283     input_program_source_read,
284     input_program_source_destroy,
285   };
286 \f
287 int
288 cmd_end_case (void)
289 {
290   if (!case_source_is_class (vfm_source, &input_program_source_class))
291     {
292       msg (SE, _("This command may only be executed between INPUT PROGRAM "
293                  "and END INPUT PROGRAM."));
294       return CMD_CASCADING_FAILURE;
295     }
296
297   add_transformation (end_case_trns_proc, NULL, NULL);
298
299   return lex_end_of_command ();
300 }
301
302 /* Should never be called, because this is handled in
303    input_program_source_read(). */
304 int
305 end_case_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
306                     int case_num UNUSED)
307 {
308   abort ();
309 }
310
311 /* REREAD transformation. */
312 struct reread_trns
313   {
314     struct dfm_reader *reader;  /* File to move file pointer back on. */
315     struct expression *column;  /* Column to reset file pointer to. */
316   };
317
318 /* Parses REREAD command. */
319 int
320 cmd_reread (void)
321 {
322   struct file_handle *fh;       /* File to be re-read. */
323   struct expression *e;         /* Expression for column to set. */
324   struct reread_trns *t;        /* Created transformation. */
325
326   fh = fh_get_default_handle ();
327   e = NULL;
328   while (token != '.')
329     {
330       if (lex_match_id ("COLUMN"))
331         {
332           lex_match ('=');
333           
334           if (e)
335             {
336               msg (SE, _("COLUMN subcommand multiply specified."));
337               expr_free (e);
338               return CMD_CASCADING_FAILURE;
339             }
340           
341           e = expr_parse (default_dict, EXPR_NUMBER);
342           if (!e)
343             return CMD_CASCADING_FAILURE;
344         }
345       else if (lex_match_id ("FILE"))
346         {
347           lex_match ('=');
348           fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
349           if (fh == NULL)
350             {
351               expr_free (e);
352               return CMD_CASCADING_FAILURE;
353             }
354           lex_get ();
355         }
356       else
357         {
358           lex_error (NULL);
359           expr_free (e);
360         }
361     }
362
363   t = xmalloc (sizeof *t);
364   t->reader = dfm_open_reader (fh);
365   t->column = e;
366   add_transformation (reread_trns_proc, reread_trns_free, t);
367
368   return CMD_SUCCESS;
369 }
370
371 /* Executes a REREAD transformation. */
372 static int
373 reread_trns_proc (void *t_, struct ccase *c, int case_num)
374 {
375   struct reread_trns *t = t_;
376
377   if (t->column == NULL)
378     dfm_reread_record (t->reader, 1);
379   else
380     {
381       double column = expr_evaluate_num (t->column, c, case_num);
382       if (!finite (column) || column < 1)
383         {
384           msg (SE, _("REREAD: Column numbers must be positive finite "
385                "numbers.  Column set to 1."));
386           dfm_reread_record (t->reader, 1);
387         }
388       else
389         dfm_reread_record (t->reader, column);
390     }
391   return TRNS_CONTINUE;
392 }
393
394 /* Frees a REREAD transformation.
395    Returns true if successful, false if an I/O error occurred. */
396 static bool
397 reread_trns_free (void *t_)
398 {
399   struct reread_trns *t = t_;
400   expr_free (t->column);
401   dfm_close_reader (t->reader);
402   return true;
403 }
404
405 /* Parses END FILE command. */
406 int
407 cmd_end_file (void)
408 {
409   if (!case_source_is_class (vfm_source, &input_program_source_class))
410     {
411       msg (SE, _("This command may only be executed between INPUT PROGRAM "
412                  "and END INPUT PROGRAM."));
413       return CMD_CASCADING_FAILURE;
414     }
415
416   add_transformation (end_file_trns_proc, NULL, NULL);
417
418   return lex_end_of_command ();
419 }
420
421 /* Executes an END FILE transformation. */
422 static int
423 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
424                     int case_num UNUSED)
425 {
426   return TRNS_END_FILE;
427 }