Fixed bug reporting the significance of paired value t-test.
[pspp-builds.git] / src / language / data-io / get-data.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2008 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 <stdlib.h>
20
21 #include <data/gnumeric-reader.h>
22 #include <data/psql-reader.h>
23
24 #include <data/dictionary.h>
25 #include <data/format.h>
26 #include <data/procedure.h>
27 #include <data/settings.h>
28 #include <language/command.h>
29 #include <language/data-io/data-parser.h>
30 #include <language/data-io/data-reader.h>
31 #include <language/data-io/file-handle.h>
32 #include <language/data-io/placement-parser.h>
33 #include <language/lexer/format-parser.h>
34 #include <language/lexer/lexer.h>
35 #include <libpspp/message.h>
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) (msgid)
40
41 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
42 static int parse_get_txt (struct lexer *lexer, struct dataset *);
43 static int parse_get_psql (struct lexer *lexer, struct dataset *);
44
45 int
46 cmd_get_data (struct lexer *lexer, struct dataset *ds)
47 {
48   lex_force_match (lexer, '/');
49
50   if (!lex_force_match_id (lexer, "TYPE"))
51     return CMD_FAILURE;
52
53   lex_force_match (lexer, '=');
54
55   if (lex_match_id (lexer, "GNM"))
56     return parse_get_gnm (lexer, ds);
57   else if (lex_match_id (lexer, "TXT"))
58     return parse_get_txt (lexer, ds);
59   else if (lex_match_id (lexer, "PSQL"))
60     return parse_get_psql (lexer, ds);
61
62   msg (SE, _("Unsupported TYPE %s"), lex_tokid (lexer));
63   return CMD_FAILURE;
64 }
65
66 static int
67 parse_get_psql (struct lexer *lexer, struct dataset *ds)
68 {
69   struct psql_read_info psql;
70   psql.allow_clear = false;
71   psql.conninfo = NULL;
72   psql.str_width = -1;
73   psql.bsize = -1;
74   ds_init_empty (&psql.sql);
75
76   lex_force_match (lexer, '/');
77
78   if (!lex_force_match_id (lexer, "CONNECT"))
79     goto error;
80
81   lex_force_match (lexer, '=');
82
83   if (!lex_force_string (lexer))
84     goto error;
85
86   psql.conninfo = strdup (ds_cstr (lex_tokstr (lexer)));
87
88   lex_get (lexer);
89
90   while (lex_match (lexer, '/') )
91     {
92       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
93         {
94           lex_match (lexer, '=');
95           psql.str_width = lex_integer (lexer);
96           lex_get (lexer);
97         }
98       else if ( lex_match_id (lexer, "BSIZE"))
99         {
100           lex_match (lexer, '=');
101           psql.bsize = lex_integer (lexer);
102           lex_get (lexer);
103         }
104       else if ( lex_match_id (lexer, "UNENCRYPTED"))
105         {
106           psql.allow_clear = true;
107         }
108       else if (lex_match_id (lexer, "SQL"))
109         {
110           lex_match (lexer, '=');
111           if ( ! lex_force_string (lexer) )
112             goto error;
113
114           ds_put_substring (&psql.sql,  lex_tokstr (lexer)->ss);
115           lex_get (lexer);
116         }
117      }
118   {
119     struct dictionary *dict = NULL;
120     struct casereader *reader = psql_open_reader (&psql, &dict);
121
122     if ( reader )
123       proc_set_active_file (ds, reader, dict);
124   }
125
126   ds_destroy (&psql.sql);
127   free (psql.conninfo);
128
129   return CMD_SUCCESS;
130
131  error:
132
133   ds_destroy (&psql.sql);
134   free (psql.conninfo);
135
136   return CMD_FAILURE;
137 }
138
139 static int
140 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
141 {
142   struct gnumeric_read_info gri  = {NULL, NULL, NULL, 1, true, -1};
143
144   lex_force_match (lexer, '/');
145
146   if (!lex_force_match_id (lexer, "FILE"))
147     goto error;
148
149   lex_force_match (lexer, '=');
150
151   if (!lex_force_string (lexer))
152     goto error;
153
154   gri.file_name = strdup (ds_cstr (lex_tokstr (lexer)));
155
156   lex_get (lexer);
157
158   while (lex_match (lexer, '/') )
159     {
160       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
161         {
162           lex_match (lexer, '=');
163           gri.asw = lex_integer (lexer);
164         }
165       else if (lex_match_id (lexer, "SHEET"))
166         {
167           lex_match (lexer, '=');
168           if (lex_match_id (lexer, "NAME"))
169             {
170               if ( ! lex_force_string (lexer) )
171                 goto error;
172
173               gri.sheet_name = strdup (ds_cstr (lex_tokstr (lexer)));
174               gri.sheet_index = -1;
175             }
176           else if (lex_match_id (lexer, "INDEX"))
177             {
178               gri.sheet_index = lex_integer (lexer);
179             }
180           else
181             goto error;
182         }
183       else if (lex_match_id (lexer, "CELLRANGE"))
184         {
185           lex_match (lexer, '=');
186
187           if (lex_match_id (lexer, "FULL"))
188             {
189               gri.cell_range = NULL;
190               lex_put_back (lexer, T_ID);
191             }
192           else if (lex_match_id (lexer, "RANGE"))
193             {
194               if ( ! lex_force_string (lexer) )
195                 goto error;
196
197               gri.cell_range = strdup (ds_cstr (lex_tokstr (lexer)));
198             }
199           else
200             goto error;
201         }
202       else if (lex_match_id (lexer, "READNAMES"))
203         {
204           lex_match (lexer, '=');
205
206           if ( lex_match_id (lexer, "ON"))
207             {
208               gri.read_names = true;
209             }
210           else if (lex_match_id (lexer, "OFF"))
211             {
212               gri.read_names = false;
213             }
214           else
215             goto error;
216           lex_put_back (lexer, T_ID);
217         }
218       else
219         {
220           printf ("Unknown data file type \"\%s\"\n", lex_tokid (lexer));
221           goto error;
222         }
223       lex_get (lexer);
224     }
225
226   {
227     struct dictionary *dict = NULL;
228     struct casereader *reader = gnumeric_open_reader (&gri, &dict);
229
230     if ( reader )
231       proc_set_active_file (ds, reader, dict);
232   }
233
234   free (gri.file_name);
235   free (gri.sheet_name);
236   free (gri.cell_range);
237   return CMD_SUCCESS;
238
239  error:
240
241   free (gri.file_name);
242   free (gri.sheet_name);
243   free (gri.cell_range);
244   return CMD_FAILURE;
245 }
246
247 static bool
248 set_type (struct data_parser *parser, const char *subcommand,
249           enum data_parser_type type, bool *has_type)
250 {
251   if (!*has_type)
252     {
253       data_parser_set_type (parser, type);
254       *has_type = true;
255     }
256   else if (type != data_parser_get_type (parser))
257     {
258       msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
259                  "was stated or implied earlier in this command."),
260            subcommand,
261            type == DP_FIXED ? "FIXED" : "DELIMITED",
262            type == DP_FIXED ? "DELIMITED" : "FIXED");
263       return false;
264     }
265   return true;
266 }
267
268 static int
269 parse_get_txt (struct lexer *lexer, struct dataset *ds)
270 {
271   struct data_parser *parser = NULL;
272   struct dictionary *dict = NULL;
273   struct file_handle *fh = NULL;
274   struct dfm_reader *reader = NULL;
275
276   int record;
277   enum data_parser_type type;
278   bool has_type;
279
280   lex_force_match (lexer, '/');
281
282   if (!lex_force_match_id (lexer, "FILE"))
283     goto error;
284   lex_force_match (lexer, '=');
285   fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
286   if (fh == NULL)
287     goto error;
288
289   parser = data_parser_create ();
290   has_type = false;
291   data_parser_set_type (parser, DP_DELIMITED);
292   data_parser_set_span (parser, false);
293   data_parser_set_quotes (parser, ss_empty ());
294   data_parser_set_empty_line_has_field (parser, true);
295
296   for (;;)
297     {
298       if (!lex_force_match (lexer, '/'))
299         goto error;
300
301       if (lex_match_id (lexer, "ARRANGEMENT"))
302         {
303           bool ok;
304
305           lex_match (lexer, '=');
306           if (lex_match_id (lexer, "FIXED"))
307             ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
308           else if (lex_match_id (lexer, "DELIMITED"))
309             ok = set_type (parser, "ARRANGEMENT=DELIMITED",
310                            DP_DELIMITED, &has_type);
311           else
312             {
313               lex_error (lexer, _("expecting FIXED or DELIMITED"));
314               goto error;
315             }
316           if (!ok)
317             goto error;
318         }
319       else if (lex_match_id (lexer, "FIRSTCASE"))
320         {
321           lex_match (lexer, '=');
322           if (!lex_force_int (lexer))
323             goto error;
324           if (lex_integer (lexer) < 1)
325             {
326               msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
327               goto error;
328             }
329           data_parser_set_skip (parser, lex_integer (lexer) - 1);
330           lex_get (lexer);
331         }
332       else if (lex_match_id_n (lexer, "DELCASE", 4))
333         {
334           if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
335             goto error;
336           lex_match (lexer, '=');
337           if (lex_match_id (lexer, "LINE"))
338             data_parser_set_span (parser, false);
339           else if (lex_match_id (lexer, "VARIABLES"))
340             {
341               data_parser_set_span (parser, true);
342
343               /* VARIABLES takes an integer argument, but for no
344                  good reason.  We just ignore it. */
345               if (!lex_force_int (lexer))
346                 goto error;
347               lex_get (lexer);
348             }
349           else
350             {
351               lex_error (lexer, _("expecting LINE or VARIABLES"));
352               goto error;
353             }
354         }
355       else if (lex_match_id (lexer, "FIXCASE"))
356         {
357           if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
358             goto error;
359           lex_match (lexer, '=');
360           if (!lex_force_int (lexer))
361             goto error;
362           if (lex_integer (lexer) < 1)
363             {
364               msg (SE, _("Value of FIXCASE must be at least 1."));
365               goto error;
366             }
367           data_parser_set_records (parser, lex_integer (lexer));
368           lex_get (lexer);
369         }
370       else if (lex_match_id (lexer, "IMPORTCASES"))
371         {
372           lex_match (lexer, '=');
373           if (lex_match (lexer, T_ALL))
374             {
375               data_parser_set_case_limit (parser, -1);
376               data_parser_set_case_percent (parser, 100);
377             }
378           else if (lex_match_id (lexer, "FIRST"))
379             {
380               if (!lex_force_int (lexer))
381                 goto error;
382               if (lex_integer (lexer) < 1)
383                 {
384                   msg (SE, _("Value of FIRST must be at least 1."));
385                   goto error;
386                 }
387               data_parser_set_case_limit (parser, lex_integer (lexer));
388               lex_get (lexer);
389             }
390           else if (lex_match_id (lexer, "PERCENT"))
391             {
392               if (!lex_force_int (lexer))
393                 goto error;
394               if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
395                 {
396                   msg (SE, _("Value of PERCENT must be between 1 and 100."));
397                   goto error;
398                 }
399               data_parser_set_case_percent (parser, lex_integer (lexer));
400               lex_get (lexer);
401             }
402         }
403       else if (lex_match_id_n (lexer, "DELIMITERS", 4))
404         {
405           struct string hard_seps = DS_EMPTY_INITIALIZER;
406           const char *soft_seps = "";
407           struct substring s;
408           int c;
409
410           if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
411             goto error;
412           lex_match (lexer, '=');
413
414           if (!lex_force_string (lexer))
415             goto error;
416
417           s = ds_ss (lex_tokstr (lexer));
418           if (ss_match_string (&s, ss_cstr ("\\t")))
419             ds_put_cstr (&hard_seps, "\t");
420           if (ss_match_string (&s, ss_cstr ("\\\\")))
421             ds_put_cstr (&hard_seps, "\\");
422           while ((c = ss_get_char (&s)) != EOF)
423             if (c == ' ')
424               soft_seps = " ";
425             else
426               ds_put_char (&hard_seps, c);
427           data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
428           data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
429           ds_destroy (&hard_seps);
430
431           lex_get (lexer);
432         }
433       else if (lex_match_id (lexer, "QUALIFIERS"))
434         {
435           if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
436             goto error;
437           lex_match (lexer, '=');
438
439           if (!lex_force_string (lexer))
440             goto error;
441
442           if (settings_get_syntax () == COMPATIBLE
443               && ds_length (lex_tokstr (lexer)) != 1)
444             {
445               msg (SE, _("In compatible syntax mode, the QUALIFIER string "
446                          "must contain exactly one character."));
447               goto error;
448             }
449
450           data_parser_set_quotes (parser, ds_ss (lex_tokstr (lexer)));
451           lex_get (lexer);
452         }
453       else if (settings_get_syntax () == ENHANCED
454                && lex_match_id (lexer, "ESCAPE"))
455         data_parser_set_quote_escape (parser, true);
456       else if (lex_match_id (lexer, "VARIABLES"))
457         break;
458       else
459         {
460           lex_error (lexer, _("expecting VARIABLES"));
461           goto error;
462         }
463     }
464   lex_match (lexer, '=');
465
466   dict = dict_create ();
467   record = 1;
468   type = data_parser_get_type (parser);
469   do
470     {
471       char name[VAR_NAME_LEN + 1];
472       struct fmt_spec input, output;
473       int fc, lc;
474       struct variable *v;
475
476       while (type == DP_FIXED && lex_match (lexer, '/'))
477         {
478           if (!lex_force_int (lexer))
479             goto error;
480           if (lex_integer (lexer) < record)
481             {
482               msg (SE, _("The record number specified, %ld, is at or "
483                          "before the previous record, %d.  Data "
484                          "fields must be listed in order of "
485                          "increasing record number."),
486                    lex_integer (lexer), record);
487               goto error;
488             }
489           if (lex_integer (lexer) > data_parser_get_records (parser))
490             {
491               msg (SE, _("The record number specified, %ld, exceeds "
492                          "the number of records per case specified "
493                          "on FIXCASE, %d."),
494                    lex_integer (lexer), data_parser_get_records (parser));
495               goto error;
496             }
497           record = lex_integer (lexer);
498           lex_get (lexer);
499         }
500
501       if (!lex_force_id (lexer))
502         goto error;
503       strcpy (name, lex_tokid (lexer));
504       lex_get (lexer);
505
506       if (type == DP_DELIMITED)
507         {
508           if (!parse_format_specifier (lexer, &input)
509               || !fmt_check_input (&input))
510             goto error;
511         }
512       else
513         {
514           if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
515             goto error;
516           if (!parse_format_specifier_name (lexer, &input.type))
517             goto error;
518           input.w = lc - fc + 1;
519           input.d = 0;
520           if (!fmt_check_input (&input))
521             goto error;
522         }
523       output = fmt_for_output_from_input (&input);
524
525       v = dict_create_var (dict, name, fmt_var_width (&input));
526       if (v == NULL)
527         {
528           msg (SE, _("%s is a duplicate variable name."), name);
529           goto error;
530         }
531       var_set_both_formats (v, &output);
532
533       if (type == DP_DELIMITED)
534         data_parser_add_delimited_field (parser, &input,
535                                          var_get_case_index (v),
536                                          name);
537       else
538         data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
539                                      name, record, fc);
540     }
541   while (lex_token (lexer) != '.');
542
543   reader = dfm_open_reader (fh, lexer);
544   if (reader == NULL)
545     goto error;
546
547   data_parser_make_active_file (parser, ds, reader, dict);
548   fh_unref (fh);
549   return CMD_SUCCESS;
550
551  error:
552   data_parser_destroy (parser);
553   dict_destroy (dict);
554   fh_unref (fh);
555   return CMD_CASCADING_FAILURE;
556 }