Fixed bug reporting the significance of paired value t-test.
[pspp-builds.git] / src / language / data-io / inpt-pgm.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU 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, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <language/data-io/inpt-pgm.h>
20
21 #include <float.h>
22 #include <stdlib.h>
23
24 #include <data/case.h>
25 #include <data/caseinit.h>
26 #include <data/casereader-provider.h>
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <data/transformations.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/data-io/data-reader.h>
33 #include <language/data-io/file-handle.h>
34 #include <language/expressions/public.h>
35 #include <language/lexer/lexer.h>
36 #include <libpspp/assertion.h>
37 #include <libpspp/compiler.h>
38 #include <libpspp/message.h>
39 #include <libpspp/message.h>
40 #include <libpspp/misc.h>
41 #include <libpspp/str.h>
42
43 #include "xalloc.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     struct trns_chain *trns_chain;
68     enum trns_result restart;
69
70     casenumber case_nr;             /* Incremented by END CASE transformation. */
71
72     struct caseinit *init;
73     size_t value_cnt;
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 casereader_class input_program_casereader_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 dataset *ds, struct input_program_pgm *inp)
97 {
98   add_transformation (ds, end_case_trns_proc, NULL, inp);
99 }
100
101 int
102 cmd_input_program (struct lexer *lexer, struct dataset *ds)
103 {
104   struct input_program_pgm *inp;
105   bool saw_END_CASE = false;
106
107   proc_discard_active_file (ds);
108   if (lex_token (lexer) != '.')
109     return lex_end_of_command (lexer);
110
111   inp = xmalloc (sizeof *inp);
112   inp->trns_chain = NULL;
113   inp->init = NULL;
114
115   inside_input_program = true;
116   for (;;)
117     {
118       enum cmd_result result = cmd_parse_in_state (lexer, ds, CMD_STATE_INPUT_PROGRAM);
119       if (result == CMD_END_INPUT_PROGRAM)
120         break;
121       else if (result == CMD_END_CASE)
122         {
123           emit_END_CASE (ds, inp);
124           saw_END_CASE = true;
125         }
126       else if (cmd_result_is_failure (result) && result != CMD_FAILURE)
127         {
128           if (result == CMD_EOF)
129             msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
130           inside_input_program = false;
131           proc_discard_active_file (ds);
132           destroy_input_program (inp);
133           return result;
134         }
135     }
136   if (!saw_END_CASE)
137     emit_END_CASE (ds, inp);
138   inside_input_program = false;
139
140   if (dict_get_next_value_idx (dataset_dict (ds)) == 0)
141     {
142       msg (SE, _("Input program did not create any variables."));
143       proc_discard_active_file (ds);
144       destroy_input_program (inp);
145       return CMD_FAILURE;
146     }
147
148   inp->trns_chain = proc_capture_transformations (ds);
149   trns_chain_finalize (inp->trns_chain);
150
151   inp->restart = TRNS_CONTINUE;
152
153   /* Figure out how to initialize each input case. */
154   inp->init = caseinit_create ();
155   caseinit_mark_for_init (inp->init, dataset_dict (ds));
156   inp->value_cnt = dict_get_next_value_idx (dataset_dict (ds));
157
158   proc_set_active_file_data (
159     ds, casereader_create_sequential (NULL, inp->value_cnt, CASENUMBER_MAX,
160                                       &input_program_casereader_class, inp));
161
162   return CMD_SUCCESS;
163 }
164
165 int
166 cmd_end_input_program (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
167 {
168   assert (in_input_program ());
169   return CMD_END_INPUT_PROGRAM;
170 }
171
172 /* Returns true if STATE is valid given the transformations that
173    are allowed within INPUT PROGRAM. */
174 static bool
175 is_valid_state (enum trns_result state)
176 {
177   return (state == TRNS_CONTINUE
178           || state == TRNS_ERROR
179           || state == TRNS_END_FILE
180           || state >= 0);
181 }
182
183 /* Reads one case into C.
184    Returns true if successful, false at end of file or if an
185    I/O error occurred. */
186 static bool
187 input_program_casereader_read (struct casereader *reader UNUSED, void *inp_,
188                                struct ccase *c)
189 {
190   struct input_program_pgm *inp = inp_;
191
192   case_create (c, inp->value_cnt);
193
194   do
195     {
196       assert (is_valid_state (inp->restart));
197       if (inp->restart == TRNS_ERROR || inp->restart == TRNS_END_FILE)
198         {
199           case_destroy (c);
200           return false;
201         }
202
203       caseinit_init_vars (inp->init, c);
204       inp->restart = trns_chain_execute (inp->trns_chain, inp->restart,
205                                          c, inp->case_nr);
206       assert (is_valid_state (inp->restart));
207       caseinit_update_left_vars (inp->init, c);
208     }
209   while (inp->restart < 0);
210
211   return true;
212 }
213
214 static void
215 destroy_input_program (struct input_program_pgm *pgm)
216 {
217   if (pgm != NULL)
218     {
219       trns_chain_destroy (pgm->trns_chain);
220       caseinit_destroy (pgm->init);
221       free (pgm);
222     }
223 }
224
225 /* Destroys the casereader. */
226 static void
227 input_program_casereader_destroy (struct casereader *reader UNUSED, void *inp_)
228 {
229   struct input_program_pgm *inp = inp_;
230   if (inp->restart == TRNS_ERROR)
231     casereader_force_error (reader);
232   destroy_input_program (inp);
233 }
234
235 static const struct casereader_class input_program_casereader_class =
236   {
237     input_program_casereader_read,
238     input_program_casereader_destroy,
239     NULL,
240     NULL,
241   };
242 \f
243 int
244 cmd_end_case (struct lexer *lexer, struct dataset *ds UNUSED)
245 {
246   assert (in_input_program ());
247   if (lex_token (lexer) == '.')
248     return CMD_END_CASE;
249   return lex_end_of_command (lexer);
250 }
251
252 /* Outputs the current case */
253 int
254 end_case_trns_proc (void *inp_, struct ccase *c UNUSED,
255                     casenumber case_nr UNUSED)
256 {
257   struct input_program_pgm *inp = inp_;
258   inp->case_nr++;
259   return TRNS_END_CASE;
260 }
261
262 /* REREAD transformation. */
263 struct reread_trns
264   {
265     struct dfm_reader *reader;  /* File to move file pointer back on. */
266     struct expression *column;  /* Column to reset file pointer to. */
267   };
268
269 /* Parses REREAD command. */
270 int
271 cmd_reread (struct lexer *lexer, struct dataset *ds)
272 {
273   struct file_handle *fh;       /* File to be re-read. */
274   struct expression *e;         /* Expression for column to set. */
275   struct reread_trns *t;        /* Created transformation. */
276
277   fh = fh_get_default_handle ();
278   e = NULL;
279   while (lex_token (lexer) != '.')
280     {
281       if (lex_match_id (lexer, "COLUMN"))
282         {
283           lex_match (lexer, '=');
284
285           if (e)
286             {
287               msg (SE, _("COLUMN subcommand multiply specified."));
288               expr_free (e);
289               return CMD_CASCADING_FAILURE;
290             }
291
292           e = expr_parse (lexer, ds, EXPR_NUMBER);
293           if (!e)
294             return CMD_CASCADING_FAILURE;
295         }
296       else if (lex_match_id (lexer, "FILE"))
297         {
298           lex_match (lexer, '=');
299           fh_unref (fh);
300           fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
301           if (fh == NULL)
302             {
303               expr_free (e);
304               return CMD_CASCADING_FAILURE;
305             }
306         }
307       else
308         {
309           lex_error (lexer, NULL);
310           expr_free (e);
311           return CMD_CASCADING_FAILURE;
312         }
313     }
314
315   t = xmalloc (sizeof *t);
316   t->reader = dfm_open_reader (fh, lexer);
317   t->column = e;
318   add_transformation (ds, reread_trns_proc, reread_trns_free, t);
319
320   fh_unref (fh);
321   return CMD_SUCCESS;
322 }
323
324 /* Executes a REREAD transformation. */
325 static int
326 reread_trns_proc (void *t_, struct ccase *c, casenumber case_num)
327 {
328   struct reread_trns *t = t_;
329
330   if (t->column == NULL)
331     dfm_reread_record (t->reader, 1);
332   else
333     {
334       double column = expr_evaluate_num (t->column, c, case_num);
335       if (!isfinite (column) || column < 1)
336         {
337           msg (SE, _("REREAD: Column numbers must be positive finite "
338                "numbers.  Column set to 1."));
339           dfm_reread_record (t->reader, 1);
340         }
341       else
342         dfm_reread_record (t->reader, column);
343     }
344   return TRNS_CONTINUE;
345 }
346
347 /* Frees a REREAD transformation.
348    Returns true if successful, false if an I/O error occurred. */
349 static bool
350 reread_trns_free (void *t_)
351 {
352   struct reread_trns *t = t_;
353   expr_free (t->column);
354   dfm_close_reader (t->reader);
355   return true;
356 }
357
358 /* Parses END FILE command. */
359 int
360 cmd_end_file (struct lexer *lexer, struct dataset *ds)
361 {
362   assert (in_input_program ());
363
364   add_transformation (ds, end_file_trns_proc, NULL, NULL);
365
366   return lex_end_of_command (lexer);
367 }
368
369 /* Executes an END FILE transformation. */
370 static int
371 end_file_trns_proc (void *trns_ UNUSED, struct ccase *c UNUSED,
372                     casenumber case_num UNUSED)
373 {
374   return TRNS_END_FILE;
375 }