1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
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.
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.
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/>. */
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"
42 #include "gl/xalloc.h"
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) (msgid)
48 static bool parse_spreadsheet (struct lexer *lexer, char **filename,
49 struct spreadsheet_read_options *opts);
51 static void destroy_spreadsheet_read_info (struct spreadsheet_read_info *, struct spreadsheet_read_options *);
53 static int parse_get_txt (struct lexer *lexer, struct dataset *);
54 static int parse_get_psql (struct lexer *lexer, struct dataset *);
57 cmd_get_data (struct lexer *lexer, struct dataset *ds)
60 lex_force_match (lexer, T_SLASH);
62 if (!lex_force_match_id (lexer, "TYPE"))
65 lex_force_match (lexer, T_EQUALS);
67 tok = strdup (lex_tokcstr (lexer));
68 if (lex_match_id (lexer, "TXT"))
71 return parse_get_txt (lexer, ds);
73 else if (lex_match_id (lexer, "PSQL"))
76 return parse_get_psql (lexer, ds);
78 else if (lex_match_id (lexer, "GNM") ||
79 lex_match_id (lexer, "ODS"))
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))
88 if ( 0 == strncasecmp (tok, "GNM", 3))
90 struct spreadsheet *spreadsheet = gnumeric_probe (filename, true);
91 reader = gnumeric_make_reader (spreadsheet, &opts);
92 dict = spreadsheet->dict;
94 else if (0 == strncasecmp (tok, "ODS", 3))
96 struct spreadsheet *spreadsheet = ods_probe (filename, true);
97 reader = ods_make_reader (spreadsheet, &opts);
98 dict = spreadsheet->dict;
103 dataset_set_dict (ds, dict);
104 dataset_set_source (ds, reader);
110 msg (SE, _("Unsupported TYPE %s."), tok);
118 parse_get_psql (struct lexer *lexer, struct dataset *ds)
120 struct psql_read_info psql;
121 psql.allow_clear = false;
122 psql.conninfo = NULL;
125 ds_init_empty (&psql.sql);
127 lex_force_match (lexer, T_SLASH);
129 if (!lex_force_match_id (lexer, "CONNECT"))
132 lex_force_match (lexer, T_EQUALS);
134 if (!lex_force_string (lexer))
137 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
141 while (lex_match (lexer, T_SLASH) )
143 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
145 lex_match (lexer, T_EQUALS);
146 psql.str_width = lex_integer (lexer);
149 else if ( lex_match_id (lexer, "BSIZE"))
151 lex_match (lexer, T_EQUALS);
152 psql.bsize = lex_integer (lexer);
155 else if ( lex_match_id (lexer, "UNENCRYPTED"))
157 psql.allow_clear = true;
159 else if (lex_match_id (lexer, "SQL"))
161 lex_match (lexer, T_EQUALS);
162 if ( ! lex_force_string (lexer) )
165 ds_put_substring (&psql.sql, lex_tokss (lexer));
170 struct dictionary *dict = NULL;
171 struct casereader *reader = psql_open_reader (&psql, &dict);
175 dataset_set_dict (ds, dict);
176 dataset_set_source (ds, reader);
180 ds_destroy (&psql.sql);
181 free (psql.conninfo);
187 ds_destroy (&psql.sql);
188 free (psql.conninfo);
194 parse_spreadsheet (struct lexer *lexer, char **filename,
195 struct spreadsheet_read_options *opts)
197 opts->sheet_index = 1;
198 opts->sheet_name = NULL;
199 opts->cell_range = NULL;
200 opts->read_names = true;
203 lex_force_match (lexer, T_SLASH);
205 if (!lex_force_match_id (lexer, "FILE"))
208 lex_force_match (lexer, T_EQUALS);
210 if (!lex_force_string (lexer))
213 *filename = utf8_to_filename (lex_tokcstr (lexer));
217 while (lex_match (lexer, T_SLASH) )
219 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
221 lex_match (lexer, T_EQUALS);
222 opts->asw = lex_integer (lexer);
225 else if (lex_match_id (lexer, "SHEET"))
227 lex_match (lexer, T_EQUALS);
228 if (lex_match_id (lexer, "NAME"))
230 if ( ! lex_force_string (lexer) )
233 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
234 opts->sheet_index = -1;
238 else if (lex_match_id (lexer, "INDEX"))
240 opts->sheet_index = lex_integer (lexer);
241 if (opts->sheet_index <= 0)
243 msg (SE, _("The sheet index must be greater than or equal to 1"));
250 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
251 "/SHEET", "NAME", "INDEX");
255 else if (lex_match_id (lexer, "CELLRANGE"))
257 lex_match (lexer, T_EQUALS);
259 if (lex_match_id (lexer, "FULL"))
261 opts->cell_range = NULL;
263 else if (lex_match_id (lexer, "RANGE"))
265 if ( ! lex_force_string (lexer) )
268 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
273 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
274 "/CELLRANGE", "FULL", "RANGE");
278 else if (lex_match_id (lexer, "READNAMES"))
280 lex_match (lexer, T_EQUALS);
282 if ( lex_match_id (lexer, "ON"))
284 opts->read_names = true;
286 else if (lex_match_id (lexer, "OFF"))
288 opts->read_names = false;
292 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
293 "/READNAMES", "ON", "OFF");
299 lex_error (lexer, NULL);
312 set_type (struct data_parser *parser, const char *subcommand,
313 enum data_parser_type type, bool *has_type)
317 data_parser_set_type (parser, type);
320 else if (type != data_parser_get_type (parser))
322 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
323 "was stated or implied earlier in this command."),
325 type == DP_FIXED ? "FIXED" : "DELIMITED",
326 type == DP_FIXED ? "DELIMITED" : "FIXED");
333 parse_get_txt (struct lexer *lexer, struct dataset *ds)
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;
343 enum data_parser_type type;
346 lex_force_match (lexer, T_SLASH);
348 if (!lex_force_match_id (lexer, "FILE"))
350 lex_force_match (lexer, T_EQUALS);
351 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
355 parser = data_parser_create (dict);
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);
364 if (!lex_force_match (lexer, T_SLASH))
367 if (lex_match_id (lexer, "ENCODING"))
369 lex_match (lexer, T_EQUALS);
370 if (!lex_force_string (lexer))
374 encoding = ss_xstrdup (lex_tokss (lexer));
378 else if (lex_match_id (lexer, "ARRANGEMENT"))
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);
390 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
396 else if (lex_match_id (lexer, "FIRSTCASE"))
398 lex_match (lexer, T_EQUALS);
399 if (!lex_force_int (lexer))
401 if (lex_integer (lexer) < 1)
403 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
406 data_parser_set_skip (parser, lex_integer (lexer) - 1);
409 else if (lex_match_id_n (lexer, "DELCASE", 4))
411 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
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"))
418 data_parser_set_span (parser, true);
420 /* VARIABLES takes an integer argument, but for no
421 good reason. We just ignore it. */
422 if (!lex_force_int (lexer))
428 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
432 else if (lex_match_id (lexer, "FIXCASE"))
434 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
436 lex_match (lexer, T_EQUALS);
437 if (!lex_force_int (lexer))
439 if (lex_integer (lexer) < 1)
441 msg (SE, _("Value of FIXCASE must be at least 1."));
444 data_parser_set_records (parser, lex_integer (lexer));
447 else if (lex_match_id (lexer, "IMPORTCASES"))
449 lex_match (lexer, T_EQUALS);
450 if (lex_match (lexer, T_ALL))
452 data_parser_set_case_limit (parser, -1);
453 data_parser_set_case_percent (parser, 100);
455 else if (lex_match_id (lexer, "FIRST"))
457 if (!lex_force_int (lexer))
459 if (lex_integer (lexer) < 1)
461 msg (SE, _("Value of FIRST must be at least 1."));
464 data_parser_set_case_limit (parser, lex_integer (lexer));
467 else if (lex_match_id (lexer, "PERCENT"))
469 if (!lex_force_int (lexer))
471 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
473 msg (SE, _("Value of PERCENT must be between 1 and 100."));
476 data_parser_set_case_percent (parser, lex_integer (lexer));
480 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
482 struct string hard_seps = DS_EMPTY_INITIALIZER;
483 const char *soft_seps = "";
487 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
489 lex_match (lexer, T_EQUALS);
491 if (!lex_force_string (lexer))
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)
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);
511 else if (lex_match_id (lexer, "QUALIFIERS"))
513 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
515 lex_match (lexer, T_EQUALS);
517 if (!lex_force_string (lexer))
520 /* XXX should support multibyte UTF-8 characters */
521 if (settings_get_syntax () == COMPATIBLE
522 && ss_length (lex_tokss (lexer)) != 1)
524 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
525 "must contain exactly one character."));
529 data_parser_set_quotes (parser, lex_tokss (lexer));
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"))
539 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
543 lex_match (lexer, T_EQUALS);
546 type = data_parser_get_type (parser);
549 struct fmt_spec input, output;
553 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
555 if (!lex_force_int (lexer))
557 if (lex_integer (lexer) < record)
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);
566 if (lex_integer (lexer) > data_parser_get_records (parser))
568 msg (SE, _("The record number specified, %ld, exceeds "
569 "the number of records per case specified "
571 lex_integer (lexer), data_parser_get_records (parser));
574 record = lex_integer (lexer);
578 if (!lex_force_id (lexer)
579 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
581 name = xstrdup (lex_tokcstr (lexer));
584 if (type == DP_DELIMITED)
586 if (!parse_format_specifier (lexer, &input)
587 || !fmt_check_input (&input))
590 output = fmt_for_output_from_input (&input);
594 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
595 enum fmt_type fmt_type;
598 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
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))
604 if (!fmt_from_name (fmt_type_name, &fmt_type))
606 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
610 /* Compose input format. */
611 input.type = fmt_type;
612 input.w = lc - fc + 1;
614 if (!fmt_check_input (&input))
617 /* Compose output format. */
620 output.type = fmt_type;
623 if (!fmt_check_output (&output))
627 output = fmt_for_output_from_input (&input);
630 v = dict_create_var (dict, name, fmt_var_width (&input));
633 msg (SE, _("%s is a duplicate variable name."), name);
636 var_set_both_formats (v, &output);
638 if (type == DP_DELIMITED)
639 data_parser_add_delimited_field (parser, &input,
640 var_get_case_index (v),
643 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
648 while (lex_token (lexer) != T_ENDCMD);
650 reader = dfm_open_reader (fh, lexer, encoding);
654 data_parser_make_active_file (parser, ds, reader, dict);
660 data_parser_destroy (parser);
665 return CMD_CASCADING_FAILURE;
670 destroy_spreadsheet_read_info (UNUSED struct spreadsheet_read_info *sri,
671 UNUSED struct spreadsheet_read_options *opts)