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