Merge two option structs
[pspp] / src / language / data-io / get-data.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 <string.h>
22
23 #include "data/dataset.h"
24 #include "data/dictionary.h"
25 #include "data/format.h"
26 #include "data/gnumeric-reader.h"
27 #include "data/ods-reader.h"
28 #include "data/spreadsheet-reader.h"
29 #include "data/psql-reader.h"
30 #include "data/settings.h"
31 #include "language/command.h"
32 #include "language/data-io/data-parser.h"
33 #include "language/data-io/data-reader.h"
34 #include "language/data-io/file-handle.h"
35 #include "language/data-io/placement-parser.h"
36 #include "language/lexer/format-parser.h"
37 #include "language/lexer/lexer.h"
38 #include "libpspp/cast.h"
39 #include "libpspp/i18n.h"
40 #include "libpspp/message.h"
41
42 #include "gl/xalloc.h"
43
44 #include "gettext.h"
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) (msgid)
47
48 static bool parse_spreadsheet (struct lexer *lexer, char **filename,
49                                struct spreadsheet_read_options *opts);
50
51 static void destroy_spreadsheet_read_info (struct spreadsheet_read_info *, struct spreadsheet_read_options *);
52
53 static int parse_get_txt (struct lexer *lexer, struct dataset *);
54 static int parse_get_psql (struct lexer *lexer, struct dataset *);
55
56 int
57 cmd_get_data (struct lexer *lexer, struct dataset *ds)
58 {
59   char *tok = NULL;
60   lex_force_match (lexer, T_SLASH);
61
62   if (!lex_force_match_id (lexer, "TYPE"))
63     return CMD_FAILURE;
64
65   lex_force_match (lexer, T_EQUALS);
66
67   tok = strdup (lex_tokcstr (lexer));
68   if (lex_match_id (lexer, "TXT"))
69     {
70       free (tok);
71       return parse_get_txt (lexer, ds);
72     }
73   else if (lex_match_id (lexer, "PSQL"))
74     {
75       free (tok);
76       return parse_get_psql (lexer, ds);
77     }
78   else if (lex_match_id (lexer, "GNM") || 
79       lex_match_id (lexer, "ODS"))
80     {
81       char *filename = NULL;
82       struct casereader *reader = NULL;
83       struct dictionary *dict = NULL;
84       struct spreadsheet_read_options opts;
85       if (!parse_spreadsheet (lexer, &filename, &opts))
86         goto error;
87
88       if ( 0 == strncasecmp (tok, "GNM", 3))
89         {
90           struct spreadsheet *spreadsheet = gnumeric_probe (filename, true);
91           reader = gnumeric_make_reader (spreadsheet, &opts);
92           dict = spreadsheet->dict;
93         }
94       else if (0 == strncasecmp (tok, "ODS", 3))
95         {
96           struct spreadsheet *spreadsheet = ods_probe (filename, true);
97           reader = ods_make_reader (spreadsheet, &opts);
98           dict = spreadsheet->dict;
99         }
100
101       if (reader)
102         {
103           dataset_set_dict (ds, dict);
104           dataset_set_source (ds, reader);
105           free (tok);
106           return CMD_SUCCESS;
107         }
108     }
109   else
110     msg (SE, _("Unsupported TYPE %s."), tok);
111
112  error:
113   free (tok);
114   return CMD_FAILURE;
115 }
116
117 static int
118 parse_get_psql (struct lexer *lexer, struct dataset *ds)
119 {
120   struct psql_read_info psql;
121   psql.allow_clear = false;
122   psql.conninfo = NULL;
123   psql.str_width = -1;
124   psql.bsize = -1;
125   ds_init_empty (&psql.sql);
126
127   lex_force_match (lexer, T_SLASH);
128
129   if (!lex_force_match_id (lexer, "CONNECT"))
130     goto error;
131
132   lex_force_match (lexer, T_EQUALS);
133
134   if (!lex_force_string (lexer))
135     goto error;
136
137   psql.conninfo = ss_xstrdup (lex_tokss (lexer));
138
139   lex_get (lexer);
140
141   while (lex_match (lexer, T_SLASH) )
142     {
143       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
144         {
145           lex_match (lexer, T_EQUALS);
146           psql.str_width = lex_integer (lexer);
147           lex_get (lexer);
148         }
149       else if ( lex_match_id (lexer, "BSIZE"))
150         {
151           lex_match (lexer, T_EQUALS);
152           psql.bsize = lex_integer (lexer);
153           lex_get (lexer);
154         }
155       else if ( lex_match_id (lexer, "UNENCRYPTED"))
156         {
157           psql.allow_clear = true;
158         }
159       else if (lex_match_id (lexer, "SQL"))
160         {
161           lex_match (lexer, T_EQUALS);
162           if ( ! lex_force_string (lexer) )
163             goto error;
164
165           ds_put_substring (&psql.sql, lex_tokss (lexer));
166           lex_get (lexer);
167         }
168      }
169   {
170     struct dictionary *dict = NULL;
171     struct casereader *reader = psql_open_reader (&psql, &dict);
172
173     if ( reader )
174       {
175         dataset_set_dict (ds, dict);
176         dataset_set_source (ds, reader);
177       }
178   }
179
180   ds_destroy (&psql.sql);
181   free (psql.conninfo);
182
183   return CMD_SUCCESS;
184
185  error:
186
187   ds_destroy (&psql.sql);
188   free (psql.conninfo);
189
190   return CMD_FAILURE;
191 }
192
193 static bool
194 parse_spreadsheet (struct lexer *lexer, char **filename, 
195                    struct spreadsheet_read_options *opts)
196 {
197   opts->sheet_index = 1;
198   opts->sheet_name = NULL;
199   opts->cell_range = NULL;
200   opts->read_names = true;
201   opts->asw = -1;
202
203   lex_force_match (lexer, T_SLASH);
204
205   if (!lex_force_match_id (lexer, "FILE"))
206     goto error;
207
208   lex_force_match (lexer, T_EQUALS);
209
210   if (!lex_force_string (lexer))
211     goto error;
212
213   *filename  = utf8_to_filename (lex_tokcstr (lexer));
214
215   lex_get (lexer);
216
217   while (lex_match (lexer, T_SLASH) )
218     {
219       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
220         {
221           lex_match (lexer, T_EQUALS);
222           opts->asw = lex_integer (lexer);
223           lex_get (lexer);
224         }
225       else if (lex_match_id (lexer, "SHEET"))
226         {
227           lex_match (lexer, T_EQUALS);
228           if (lex_match_id (lexer, "NAME"))
229             {
230               if ( ! lex_force_string (lexer) )
231                 goto error;
232
233               opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
234               opts->sheet_index = -1;
235
236               lex_get (lexer);
237             }
238           else if (lex_match_id (lexer, "INDEX"))
239             {
240               opts->sheet_index = lex_integer (lexer);
241               if (opts->sheet_index <= 0)
242                 {
243                   msg (SE, _("The sheet index must be greater than or equal to 1"));
244                   goto error;
245                 }
246               lex_get (lexer);
247             }
248           else
249             {
250               msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
251                    "/SHEET", "NAME", "INDEX");
252               goto error;
253             }
254         }
255       else if (lex_match_id (lexer, "CELLRANGE"))
256         {
257           lex_match (lexer, T_EQUALS);
258
259           if (lex_match_id (lexer, "FULL"))
260             {
261               opts->cell_range = NULL;
262             }
263           else if (lex_match_id (lexer, "RANGE"))
264             {
265               if ( ! lex_force_string (lexer) )
266                 goto error;
267
268               opts->cell_range = ss_xstrdup (lex_tokss (lexer));
269               lex_get (lexer);
270             }
271           else
272             {
273               msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
274                    "/CELLRANGE", "FULL", "RANGE");
275               goto error;
276             }
277         }
278       else if (lex_match_id (lexer, "READNAMES"))
279         {
280           lex_match (lexer, T_EQUALS);
281
282           if ( lex_match_id (lexer, "ON"))
283             {
284               opts->read_names = true;
285             }
286           else if (lex_match_id (lexer, "OFF"))
287             {
288               opts->read_names = false;
289             }
290           else
291             {
292               msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
293                    "/READNAMES", "ON", "OFF");
294               goto error;
295             }
296         }
297       else
298         {
299           lex_error (lexer, NULL);
300           goto error;
301         }
302     }
303
304   return true;
305
306  error:
307   return false;
308 }
309
310
311 static bool
312 set_type (struct data_parser *parser, const char *subcommand,
313           enum data_parser_type type, bool *has_type)
314 {
315   if (!*has_type)
316     {
317       data_parser_set_type (parser, type);
318       *has_type = true;
319     }
320   else if (type != data_parser_get_type (parser))
321     {
322       msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
323                  "was stated or implied earlier in this command."),
324            subcommand,
325            type == DP_FIXED ? "FIXED" : "DELIMITED",
326            type == DP_FIXED ? "DELIMITED" : "FIXED");
327       return false;
328     }
329   return true;
330 }
331
332 static int
333 parse_get_txt (struct lexer *lexer, struct dataset *ds)
334 {
335   struct data_parser *parser = NULL;
336   struct dictionary *dict = dict_create (get_default_encoding ());
337   struct file_handle *fh = NULL;
338   struct dfm_reader *reader = NULL;
339   char *encoding = NULL;
340   char *name = NULL;
341
342   int record;
343   enum data_parser_type type;
344   bool has_type;
345
346   lex_force_match (lexer, T_SLASH);
347
348   if (!lex_force_match_id (lexer, "FILE"))
349     goto error;
350   lex_force_match (lexer, T_EQUALS);
351   fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
352   if (fh == NULL)
353     goto error;
354
355   parser = data_parser_create (dict);
356   has_type = false;
357   data_parser_set_type (parser, DP_DELIMITED);
358   data_parser_set_span (parser, false);
359   data_parser_set_quotes (parser, ss_empty ());
360   data_parser_set_empty_line_has_field (parser, true);
361
362   for (;;)
363     {
364       if (!lex_force_match (lexer, T_SLASH))
365         goto error;
366
367       if (lex_match_id (lexer, "ENCODING"))
368         {
369           lex_match (lexer, T_EQUALS);
370           if (!lex_force_string (lexer))
371             goto error;
372
373           free (encoding);
374           encoding = ss_xstrdup (lex_tokss (lexer));
375
376           lex_get (lexer);
377         }
378       else if (lex_match_id (lexer, "ARRANGEMENT"))
379         {
380           bool ok;
381
382           lex_match (lexer, T_EQUALS);
383           if (lex_match_id (lexer, "FIXED"))
384             ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
385           else if (lex_match_id (lexer, "DELIMITED"))
386             ok = set_type (parser, "ARRANGEMENT=DELIMITED",
387                            DP_DELIMITED, &has_type);
388           else
389             {
390               lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
391               goto error;
392             }
393           if (!ok)
394             goto error;
395         }
396       else if (lex_match_id (lexer, "FIRSTCASE"))
397         {
398           lex_match (lexer, T_EQUALS);
399           if (!lex_force_int (lexer))
400             goto error;
401           if (lex_integer (lexer) < 1)
402             {
403               msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
404               goto error;
405             }
406           data_parser_set_skip (parser, lex_integer (lexer) - 1);
407           lex_get (lexer);
408         }
409       else if (lex_match_id_n (lexer, "DELCASE", 4))
410         {
411           if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
412             goto error;
413           lex_match (lexer, T_EQUALS);
414           if (lex_match_id (lexer, "LINE"))
415             data_parser_set_span (parser, false);
416           else if (lex_match_id (lexer, "VARIABLES"))
417             {
418               data_parser_set_span (parser, true);
419
420               /* VARIABLES takes an integer argument, but for no
421                  good reason.  We just ignore it. */
422               if (!lex_force_int (lexer))
423                 goto error;
424               lex_get (lexer);
425             }
426           else
427             {
428               lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
429               goto error;
430             }
431         }
432       else if (lex_match_id (lexer, "FIXCASE"))
433         {
434           if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
435             goto error;
436           lex_match (lexer, T_EQUALS);
437           if (!lex_force_int (lexer))
438             goto error;
439           if (lex_integer (lexer) < 1)
440             {
441               msg (SE, _("Value of FIXCASE must be at least 1."));
442               goto error;
443             }
444           data_parser_set_records (parser, lex_integer (lexer));
445           lex_get (lexer);
446         }
447       else if (lex_match_id (lexer, "IMPORTCASES"))
448         {
449           lex_match (lexer, T_EQUALS);
450           if (lex_match (lexer, T_ALL))
451             {
452               data_parser_set_case_limit (parser, -1);
453               data_parser_set_case_percent (parser, 100);
454             }
455           else if (lex_match_id (lexer, "FIRST"))
456             {
457               if (!lex_force_int (lexer))
458                 goto error;
459               if (lex_integer (lexer) < 1)
460                 {
461                   msg (SE, _("Value of FIRST must be at least 1."));
462                   goto error;
463                 }
464               data_parser_set_case_limit (parser, lex_integer (lexer));
465               lex_get (lexer);
466             }
467           else if (lex_match_id (lexer, "PERCENT"))
468             {
469               if (!lex_force_int (lexer))
470                 goto error;
471               if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
472                 {
473                   msg (SE, _("Value of PERCENT must be between 1 and 100."));
474                   goto error;
475                 }
476               data_parser_set_case_percent (parser, lex_integer (lexer));
477               lex_get (lexer);
478             }
479         }
480       else if (lex_match_id_n (lexer, "DELIMITERS", 4))
481         {
482           struct string hard_seps = DS_EMPTY_INITIALIZER;
483           const char *soft_seps = "";
484           struct substring s;
485           int c;
486
487           if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
488             goto error;
489           lex_match (lexer, T_EQUALS);
490
491           if (!lex_force_string (lexer))
492             goto error;
493
494           /* XXX should support multibyte UTF-8 characters */
495           s = lex_tokss (lexer);
496           if (ss_match_string (&s, ss_cstr ("\\t")))
497             ds_put_cstr (&hard_seps, "\t");
498           if (ss_match_string (&s, ss_cstr ("\\\\")))
499             ds_put_cstr (&hard_seps, "\\");
500           while ((c = ss_get_byte (&s)) != EOF)
501             if (c == ' ')
502               soft_seps = " ";
503             else
504               ds_put_byte (&hard_seps, c);
505           data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
506           data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
507           ds_destroy (&hard_seps);
508
509           lex_get (lexer);
510         }
511       else if (lex_match_id (lexer, "QUALIFIERS"))
512         {
513           if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
514             goto error;
515           lex_match (lexer, T_EQUALS);
516
517           if (!lex_force_string (lexer))
518             goto error;
519
520           /* XXX should support multibyte UTF-8 characters */
521           if (settings_get_syntax () == COMPATIBLE
522               && ss_length (lex_tokss (lexer)) != 1)
523             {
524               msg (SE, _("In compatible syntax mode, the QUALIFIER string "
525                          "must contain exactly one character."));
526               goto error;
527             }
528
529           data_parser_set_quotes (parser, lex_tokss (lexer));
530           lex_get (lexer);
531         }
532       else if (settings_get_syntax () == ENHANCED
533                && lex_match_id (lexer, "ESCAPE"))
534         data_parser_set_quote_escape (parser, true);
535       else if (lex_match_id (lexer, "VARIABLES"))
536         break;
537       else
538         {
539           lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
540           goto error;
541         }
542     }
543   lex_match (lexer, T_EQUALS);
544
545   record = 1;
546   type = data_parser_get_type (parser);
547   do
548     {
549       struct fmt_spec input, output;
550       struct variable *v;
551       int fc, lc;
552
553       while (type == DP_FIXED && lex_match (lexer, T_SLASH))
554         {
555           if (!lex_force_int (lexer))
556             goto error;
557           if (lex_integer (lexer) < record)
558             {
559               msg (SE, _("The record number specified, %ld, is at or "
560                          "before the previous record, %d.  Data "
561                          "fields must be listed in order of "
562                          "increasing record number."),
563                    lex_integer (lexer), record);
564               goto error;
565             }
566           if (lex_integer (lexer) > data_parser_get_records (parser))
567             {
568               msg (SE, _("The record number specified, %ld, exceeds "
569                          "the number of records per case specified "
570                          "on FIXCASE, %d."),
571                    lex_integer (lexer), data_parser_get_records (parser));
572               goto error;
573             }
574           record = lex_integer (lexer);
575           lex_get (lexer);
576         }
577
578       if (!lex_force_id (lexer)
579           || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
580         goto error;
581       name = xstrdup (lex_tokcstr (lexer));
582       lex_get (lexer);
583
584       if (type == DP_DELIMITED)
585         {
586           if (!parse_format_specifier (lexer, &input)
587               || !fmt_check_input (&input))
588             goto error;
589
590           output = fmt_for_output_from_input (&input);
591         }
592       else
593         {
594           char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
595           enum fmt_type fmt_type;
596           int w, d;
597
598           if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
599             goto error;
600
601           /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR).  */
602           if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
603             goto error;
604           if (!fmt_from_name (fmt_type_name, &fmt_type))
605             {
606               msg (SE, _("Unknown format type `%s'."), fmt_type_name);
607               goto error;
608             }
609
610           /* Compose input format. */
611           input.type = fmt_type;
612           input.w = lc - fc + 1;
613           input.d = 0;
614           if (!fmt_check_input (&input))
615             goto error;
616
617           /* Compose output format. */
618           if (w != 0)
619             {
620               output.type = fmt_type;
621               output.w = w;
622               output.d = d;
623               if (!fmt_check_output (&output))
624                 goto error;
625             }
626           else
627             output = fmt_for_output_from_input (&input);
628         }
629
630       v = dict_create_var (dict, name, fmt_var_width (&input));
631       if (v == NULL)
632         {
633           msg (SE, _("%s is a duplicate variable name."), name);
634           goto error;
635         }
636       var_set_both_formats (v, &output);
637
638       if (type == DP_DELIMITED)
639         data_parser_add_delimited_field (parser, &input,
640                                          var_get_case_index (v),
641                                          name);
642       else
643         data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
644                                      name, record, fc);
645       free (name);
646       name = NULL;
647     }
648   while (lex_token (lexer) != T_ENDCMD);
649
650   reader = dfm_open_reader (fh, lexer, encoding);
651   if (reader == NULL)
652     goto error;
653
654   data_parser_make_active_file (parser, ds, reader, dict);
655   fh_unref (fh);
656   free (encoding);
657   return CMD_SUCCESS;
658
659  error:
660   data_parser_destroy (parser);
661   dict_destroy (dict);
662   fh_unref (fh);
663   free (name);
664   free (encoding);
665   return CMD_CASCADING_FAILURE;
666 }
667
668
669 static void 
670 destroy_spreadsheet_read_info (UNUSED struct spreadsheet_read_info *sri,
671                                UNUSED struct spreadsheet_read_options *opts)
672 {
673 }