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