work on data list
[pspp] / src / language / data-io / get-data.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 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 *encoding = NULL;
311   char *name = NULL;
312
313   int record;
314   enum data_parser_type type;
315   bool has_type;
316
317   lex_force_match (lexer, T_SLASH);
318
319   if (!lex_force_match_id (lexer, "FILE"))
320     goto error;
321   lex_force_match (lexer, T_EQUALS);
322   fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
323   if (fh == NULL)
324     goto error;
325
326   parser = data_parser_create (dict);
327   has_type = false;
328   data_parser_set_type (parser, DP_DELIMITED);
329   data_parser_set_span (parser, false);
330   data_parser_set_quotes (parser, ss_empty ());
331   data_parser_set_empty_line_has_field (parser, true);
332
333   for (;;)
334     {
335       if (!lex_force_match (lexer, T_SLASH))
336         goto error;
337
338       if (lex_match_id (lexer, "ENCODING"))
339         {
340           lex_match (lexer, T_EQUALS);
341           if (!lex_force_string (lexer))
342             goto error;
343
344           free (encoding);
345           encoding = ss_xstrdup (lex_tokss (lexer));
346
347           lex_get (lexer);
348         }
349       else if (lex_match_id (lexer, "ARRANGEMENT"))
350         {
351           bool ok;
352
353           lex_match (lexer, T_EQUALS);
354           if (lex_match_id (lexer, "FIXED"))
355             ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
356           else if (lex_match_id (lexer, "DELIMITED"))
357             ok = set_type (parser, "ARRANGEMENT=DELIMITED",
358                            DP_DELIMITED, &has_type);
359           else
360             {
361               lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
362               goto error;
363             }
364           if (!ok)
365             goto error;
366         }
367       else if (lex_match_id (lexer, "FIRSTCASE"))
368         {
369           lex_match (lexer, T_EQUALS);
370           if (!lex_force_int (lexer))
371             goto error;
372           if (lex_integer (lexer) < 1)
373             {
374               msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
375               goto error;
376             }
377           data_parser_set_skip (parser, lex_integer (lexer) - 1);
378           lex_get (lexer);
379         }
380       else if (lex_match_id_n (lexer, "DELCASE", 4))
381         {
382           if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
383             goto error;
384           lex_match (lexer, T_EQUALS);
385           if (lex_match_id (lexer, "LINE"))
386             data_parser_set_span (parser, false);
387           else if (lex_match_id (lexer, "VARIABLES"))
388             {
389               data_parser_set_span (parser, true);
390
391               /* VARIABLES takes an integer argument, but for no
392                  good reason.  We just ignore it. */
393               if (!lex_force_int (lexer))
394                 goto error;
395               lex_get (lexer);
396             }
397           else
398             {
399               lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
400               goto error;
401             }
402         }
403       else if (lex_match_id (lexer, "FIXCASE"))
404         {
405           if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
406             goto error;
407           lex_match (lexer, T_EQUALS);
408           if (!lex_force_int (lexer))
409             goto error;
410           if (lex_integer (lexer) < 1)
411             {
412               msg (SE, _("Value of FIXCASE must be at least 1."));
413               goto error;
414             }
415           data_parser_set_records (parser, lex_integer (lexer));
416           lex_get (lexer);
417         }
418       else if (lex_match_id (lexer, "IMPORTCASES"))
419         {
420           lex_match (lexer, T_EQUALS);
421           if (lex_match (lexer, T_ALL))
422             {
423               data_parser_set_case_limit (parser, -1);
424               data_parser_set_case_percent (parser, 100);
425             }
426           else if (lex_match_id (lexer, "FIRST"))
427             {
428               if (!lex_force_int (lexer))
429                 goto error;
430               if (lex_integer (lexer) < 1)
431                 {
432                   msg (SE, _("Value of FIRST must be at least 1."));
433                   goto error;
434                 }
435               data_parser_set_case_limit (parser, lex_integer (lexer));
436               lex_get (lexer);
437             }
438           else if (lex_match_id (lexer, "PERCENT"))
439             {
440               if (!lex_force_int (lexer))
441                 goto error;
442               if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
443                 {
444                   msg (SE, _("Value of PERCENT must be between 1 and 100."));
445                   goto error;
446                 }
447               data_parser_set_case_percent (parser, lex_integer (lexer));
448               lex_get (lexer);
449             }
450         }
451       else if (lex_match_id_n (lexer, "DELIMITERS", 4))
452         {
453           struct string hard_seps = DS_EMPTY_INITIALIZER;
454           const char *soft_seps = "";
455           struct substring s;
456           int c;
457
458           if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
459             goto error;
460           lex_match (lexer, T_EQUALS);
461
462           if (!lex_force_string (lexer))
463             goto error;
464
465           /* XXX should support multibyte UTF-8 characters */
466           s = lex_tokss (lexer);
467           if (ss_match_string (&s, ss_cstr ("\\t")))
468             ds_put_cstr (&hard_seps, "\t");
469           if (ss_match_string (&s, ss_cstr ("\\\\")))
470             ds_put_cstr (&hard_seps, "\\");
471           while ((c = ss_get_byte (&s)) != EOF)
472             if (c == ' ')
473               soft_seps = " ";
474             else
475               ds_put_byte (&hard_seps, c);
476           data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
477           data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
478           ds_destroy (&hard_seps);
479
480           lex_get (lexer);
481         }
482       else if (lex_match_id (lexer, "QUALIFIERS"))
483         {
484           if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
485             goto error;
486           lex_match (lexer, T_EQUALS);
487
488           if (!lex_force_string (lexer))
489             goto error;
490
491           /* XXX should support multibyte UTF-8 characters */
492           if (settings_get_syntax () == COMPATIBLE
493               && ss_length (lex_tokss (lexer)) != 1)
494             {
495               msg (SE, _("In compatible syntax mode, the QUALIFIER string "
496                          "must contain exactly one character."));
497               goto error;
498             }
499
500           data_parser_set_quotes (parser, lex_tokss (lexer));
501           lex_get (lexer);
502         }
503       else if (settings_get_syntax () == ENHANCED
504                && lex_match_id (lexer, "ESCAPE"))
505         data_parser_set_quote_escape (parser, true);
506       else if (lex_match_id (lexer, "VARIABLES"))
507         break;
508       else
509         {
510           lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
511           goto error;
512         }
513     }
514   lex_match (lexer, T_EQUALS);
515
516   record = 1;
517   type = data_parser_get_type (parser);
518   do
519     {
520       struct fmt_spec input, output;
521       struct variable *v;
522       int fc, lc;
523
524       while (type == DP_FIXED && lex_match (lexer, T_SLASH))
525         {
526           if (!lex_force_int (lexer))
527             goto error;
528           if (lex_integer (lexer) < record)
529             {
530               msg (SE, _("The record number specified, %ld, is at or "
531                          "before the previous record, %d.  Data "
532                          "fields must be listed in order of "
533                          "increasing record number."),
534                    lex_integer (lexer), record);
535               goto error;
536             }
537           if (lex_integer (lexer) > data_parser_get_records (parser))
538             {
539               msg (SE, _("The record number specified, %ld, exceeds "
540                          "the number of records per case specified "
541                          "on FIXCASE, %d."),
542                    lex_integer (lexer), data_parser_get_records (parser));
543               goto error;
544             }
545           record = lex_integer (lexer);
546           lex_get (lexer);
547         }
548
549       if (!lex_force_id (lexer)
550           || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
551         goto error;
552       name = xstrdup (lex_tokcstr (lexer));
553       lex_get (lexer);
554
555       if (type == DP_DELIMITED)
556         {
557           if (!parse_format_specifier (lexer, &input)
558               || !fmt_check_input (&input))
559             goto error;
560
561           output = fmt_for_output_from_input (&input);
562         }
563       else
564         {
565           char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
566           enum fmt_type fmt_type;
567           int w, d;
568
569           if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
570             goto error;
571
572           /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR).  */
573           if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
574             goto error;
575           if (!fmt_from_name (fmt_type_name, &fmt_type))
576             {
577               msg (SE, _("Unknown format type `%s'."), fmt_type_name);
578               goto error;
579             }
580
581           /* Compose input format. */
582           input.type = fmt_type;
583           input.w = lc - fc + 1;
584           input.d = 0;
585           if (!fmt_check_input (&input))
586             goto error;
587
588           /* Compose output format. */
589           if (w != 0)
590             {
591               output.type = fmt_type;
592               output.w = w;
593               output.d = d;
594               if (!fmt_check_output (&output))
595                 goto error;
596             }
597           else
598             output = fmt_for_output_from_input (&input);
599         }
600
601       v = dict_create_var (dict, name, fmt_var_width (&input));
602       if (v == NULL)
603         {
604           msg (SE, _("%s is a duplicate variable name."), name);
605           goto error;
606         }
607       var_set_both_formats (v, &output);
608
609       if (type == DP_DELIMITED)
610         data_parser_add_delimited_field (parser, &input,
611                                          var_get_case_index (v),
612                                          name);
613       else
614         data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
615                                      name, record, fc);
616       free (name);
617       name = NULL;
618     }
619   while (lex_token (lexer) != T_ENDCMD);
620
621   reader = dfm_open_reader (fh, lexer, encoding);
622   if (reader == NULL)
623     goto error;
624
625   data_parser_make_active_file (parser, ds, reader, dict);
626   fh_unref (fh);
627   free (encoding);
628   return CMD_SUCCESS;
629
630  error:
631   data_parser_destroy (parser);
632   dict_destroy (dict);
633   fh_unref (fh);
634   free (name);
635   free (encoding);
636   return CMD_CASCADING_FAILURE;
637 }
638
639
640 static void 
641 destroy_spreadsheet_read_info (struct spreadsheet_read_info *sri)
642 {
643   if ( NULL == sri)
644     return;
645
646   free (sri->sheet_name);
647   free (sri->cell_range);
648   free (sri->file_name);
649   free (sri);
650 }