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