1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012,
3 2013, 2015, 2016 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "data/dataset.h"
25 #include "data/dictionary.h"
26 #include "data/format.h"
27 #include "data/gnumeric-reader.h"
28 #include "data/ods-reader.h"
29 #include "data/spreadsheet-reader.h"
30 #include "data/psql-reader.h"
31 #include "data/settings.h"
32 #include "language/command.h"
33 #include "language/data-io/data-parser.h"
34 #include "language/data-io/data-reader.h"
35 #include "language/data-io/file-handle.h"
36 #include "language/data-io/placement-parser.h"
37 #include "language/lexer/format-parser.h"
38 #include "language/lexer/lexer.h"
39 #include "libpspp/cast.h"
40 #include "libpspp/i18n.h"
41 #include "libpspp/message.h"
43 #include "gl/xalloc.h"
46 #define _(msgid) gettext (msgid)
47 #define N_(msgid) (msgid)
49 static bool parse_spreadsheet (struct lexer *lexer, char **filename,
50 struct spreadsheet_read_options *opts);
52 static void destroy_spreadsheet_read_info (struct spreadsheet_read_options *);
54 static int parse_get_txt (struct lexer *lexer, struct dataset *);
55 static int parse_get_psql (struct lexer *lexer, struct dataset *);
58 cmd_get_data (struct lexer *lexer, struct dataset *ds)
61 struct spreadsheet_read_options opts;
63 opts.sheet_name = NULL;
64 opts.sheet_index = -1;
65 opts.cell_range = NULL;
66 opts.read_names = false;
69 if (! lex_force_match (lexer, T_SLASH))
72 if (!lex_force_match_id (lexer, "TYPE"))
75 if (!lex_force_match (lexer, T_EQUALS))
78 const char *s = lex_tokcstr (lexer);
83 if (lex_match_id (lexer, "TXT"))
86 return parse_get_txt (lexer, ds);
88 else if (lex_match_id (lexer, "PSQL"))
91 return parse_get_psql (lexer, ds);
93 else if (lex_match_id (lexer, "GNM") ||
94 lex_match_id (lexer, "ODS"))
96 char *filename = NULL;
97 if (!parse_spreadsheet (lexer, &filename, &opts))
100 struct spreadsheet *spreadsheet = NULL;
101 if (0 == strncasecmp (tok, "GNM", 3))
102 spreadsheet = gnumeric_probe (filename, true);
103 else if (0 == strncasecmp (tok, "ODS", 3))
104 spreadsheet = ods_probe (filename, true);
106 if (spreadsheet == NULL)
108 msg (SE, _("error reading file `%s'"), filename);
114 struct casereader *reader = spreadsheet_make_reader (spreadsheet, &opts);
117 dataset_set_dict (ds, dict_clone (spreadsheet->dict));
118 dataset_set_source (ds, reader);
120 destroy_spreadsheet_read_info (&opts);
121 spreadsheet_unref (spreadsheet);
124 spreadsheet_unref (spreadsheet);
127 msg (SE, _("Unsupported TYPE %s."), tok);
131 destroy_spreadsheet_read_info (&opts);
137 parse_get_psql (struct lexer *lexer, struct dataset *ds)
139 struct psql_read_info psql;
140 psql.allow_clear = false;
141 psql.conninfo = NULL;
144 ds_init_empty (&psql.sql);
146 if (! lex_force_match (lexer, T_SLASH))
149 if (!lex_force_match_id (lexer, "CONNECT"))
152 if (! lex_force_match (lexer, T_EQUALS))
155 if (!lex_force_string (lexer))
158 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
162 while (lex_match (lexer, T_SLASH))
164 if (lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
166 lex_match (lexer, T_EQUALS);
167 if (lex_force_int_range (lexer, "ASSUMEDSTRWIDTH", 1, 32767))
169 psql.str_width = lex_integer (lexer);
173 else if (lex_match_id (lexer, "BSIZE"))
175 lex_match (lexer, T_EQUALS);
176 if (lex_force_int_range (lexer, "BSIZE", 1, INT_MAX))
178 psql.bsize = lex_integer (lexer);
182 else if (lex_match_id (lexer, "UNENCRYPTED"))
184 psql.allow_clear = true;
186 else if (lex_match_id (lexer, "SQL"))
188 lex_match (lexer, T_EQUALS);
189 if (! lex_force_string (lexer))
192 ds_put_substring (&psql.sql, lex_tokss (lexer));
197 struct dictionary *dict = NULL;
198 struct casereader *reader = psql_open_reader (&psql, &dict);
202 dataset_set_dict (ds, dict);
203 dataset_set_source (ds, reader);
207 ds_destroy (&psql.sql);
208 free (psql.conninfo);
214 ds_destroy (&psql.sql);
215 free (psql.conninfo);
221 parse_spreadsheet (struct lexer *lexer, char **filename,
222 struct spreadsheet_read_options *opts)
224 opts->sheet_index = 1;
225 opts->sheet_name = NULL;
226 opts->cell_range = NULL;
227 opts->read_names = true;
230 if (! lex_force_match (lexer, T_SLASH))
233 if (!lex_force_match_id (lexer, "FILE"))
236 if (! lex_force_match (lexer, T_EQUALS))
239 if (!lex_force_string (lexer))
242 *filename = utf8_to_filename (lex_tokcstr (lexer));
246 while (lex_match (lexer, T_SLASH))
248 if (lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
250 lex_match (lexer, T_EQUALS);
251 if (lex_force_int_range (lexer, "ASSUMEDSTRWIDTH", 1, 32767))
253 opts->asw = lex_integer (lexer);
257 else if (lex_match_id (lexer, "SHEET"))
259 lex_match (lexer, T_EQUALS);
260 if (lex_match_id (lexer, "NAME"))
262 if (! lex_force_string (lexer))
265 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
266 opts->sheet_index = -1;
270 else if (lex_match_id (lexer, "INDEX"))
272 if (!lex_force_int_range (lexer, "INDEX", 1, INT_MAX))
274 opts->sheet_index = lex_integer (lexer);
279 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
280 "/SHEET", "NAME", "INDEX");
284 else if (lex_match_id (lexer, "CELLRANGE"))
286 lex_match (lexer, T_EQUALS);
288 if (lex_match_id (lexer, "FULL"))
290 opts->cell_range = NULL;
292 else if (lex_match_id (lexer, "RANGE"))
294 if (! lex_force_string (lexer))
297 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
302 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
303 "/CELLRANGE", "FULL", "RANGE");
307 else if (lex_match_id (lexer, "READNAMES"))
309 lex_match (lexer, T_EQUALS);
311 if (lex_match_id (lexer, "ON"))
313 opts->read_names = true;
315 else if (lex_match_id (lexer, "OFF"))
317 opts->read_names = false;
321 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
322 "/READNAMES", "ON", "OFF");
328 lex_error (lexer, NULL);
341 set_type (struct data_parser *parser, const char *subcommand,
342 enum data_parser_type type, bool *has_type)
346 data_parser_set_type (parser, type);
349 else if (type != data_parser_get_type (parser))
351 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
352 "was stated or implied earlier in this command."),
354 type == DP_FIXED ? "FIXED" : "DELIMITED",
355 type == DP_FIXED ? "DELIMITED" : "FIXED");
362 parse_get_txt (struct lexer *lexer, struct dataset *ds)
364 struct data_parser *parser = NULL;
365 struct dictionary *dict = dict_create (get_default_encoding ());
366 struct file_handle *fh = NULL;
367 struct dfm_reader *reader = NULL;
368 char *encoding = NULL;
372 enum data_parser_type type;
375 if (! lex_force_match (lexer, T_SLASH))
378 if (!lex_force_match_id (lexer, "FILE"))
380 if (! lex_force_match (lexer, T_EQUALS))
382 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
386 parser = data_parser_create (dict);
388 data_parser_set_type (parser, DP_DELIMITED);
389 data_parser_set_span (parser, false);
390 data_parser_set_quotes (parser, ss_empty ());
391 data_parser_set_quote_escape (parser, true);
392 data_parser_set_empty_line_has_field (parser, true);
396 if (!lex_force_match (lexer, T_SLASH))
399 if (lex_match_id (lexer, "ENCODING"))
401 lex_match (lexer, T_EQUALS);
402 if (!lex_force_string (lexer))
406 encoding = ss_xstrdup (lex_tokss (lexer));
410 else if (lex_match_id (lexer, "ARRANGEMENT"))
414 lex_match (lexer, T_EQUALS);
415 if (lex_match_id (lexer, "FIXED"))
416 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
417 else if (lex_match_id (lexer, "DELIMITED"))
418 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
419 DP_DELIMITED, &has_type);
422 lex_error_expecting (lexer, "FIXED", "DELIMITED");
428 else if (lex_match_id (lexer, "FIRSTCASE"))
430 lex_match (lexer, T_EQUALS);
431 if (!lex_force_int_range (lexer, "FIRSTCASE", 1, INT_MAX))
433 data_parser_set_skip (parser, lex_integer (lexer) - 1);
436 else if (lex_match_id_n (lexer, "DELCASE", 4))
438 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
440 lex_match (lexer, T_EQUALS);
441 if (lex_match_id (lexer, "LINE"))
442 data_parser_set_span (parser, false);
443 else if (lex_match_id (lexer, "VARIABLES"))
445 data_parser_set_span (parser, true);
447 /* VARIABLES takes an integer argument, but for no
448 good reason. We just ignore it. */
449 if (!lex_force_int (lexer))
455 lex_error_expecting (lexer, "LINE", "VARIABLES");
459 else if (lex_match_id (lexer, "FIXCASE"))
461 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
463 lex_match (lexer, T_EQUALS);
464 if (!lex_force_int_range (lexer, "FIXCASE", 1, INT_MAX))
466 data_parser_set_records (parser, lex_integer (lexer));
469 else if (lex_match_id (lexer, "IMPORTCASES"))
471 lex_match (lexer, T_EQUALS);
472 if (lex_match (lexer, T_ALL))
476 else if (lex_match_id (lexer, "FIRST"))
478 if (!lex_force_int (lexer))
482 else if (lex_match_id (lexer, "PERCENT"))
484 if (!lex_force_int (lexer))
488 msg (SW, _("Ignoring obsolete IMPORTCASES subcommand. (N OF CASES "
489 "or SAMPLE may be used to substitute.)"));
491 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
493 struct string hard_seps = DS_EMPTY_INITIALIZER;
494 const char *soft_seps = "";
498 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
500 lex_match (lexer, T_EQUALS);
502 if (!lex_force_string (lexer))
505 /* XXX should support multibyte UTF-8 characters */
506 s = lex_tokss (lexer);
507 if (ss_match_string (&s, ss_cstr ("\\t")))
508 ds_put_cstr (&hard_seps, "\t");
509 if (ss_match_string (&s, ss_cstr ("\\\\")))
510 ds_put_cstr (&hard_seps, "\\");
511 while ((c = ss_get_byte (&s)) != EOF)
515 ds_put_byte (&hard_seps, c);
516 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
517 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
518 ds_destroy (&hard_seps);
522 else if (lex_match_id (lexer, "QUALIFIERS"))
524 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
526 lex_match (lexer, T_EQUALS);
528 if (!lex_force_string (lexer))
531 /* XXX should support multibyte UTF-8 characters */
532 if (settings_get_syntax () == COMPATIBLE
533 && ss_length (lex_tokss (lexer)) != 1)
535 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
536 "must contain exactly one character."));
540 data_parser_set_quotes (parser, lex_tokss (lexer));
543 else if (lex_match_id (lexer, "VARIABLES"))
547 lex_error_expecting (lexer, "VARIABLES");
551 lex_match (lexer, T_EQUALS);
554 type = data_parser_get_type (parser);
557 struct fmt_spec input, output;
561 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
563 if (!lex_force_int_range (lexer, NULL, record,
564 data_parser_get_records (parser)))
566 record = lex_integer (lexer);
570 const char * tstr = lex_tokcstr (lexer);
573 lex_error (lexer, NULL);
576 name = xstrdup (tstr);
577 if (!lex_force_id (lexer)
578 || !dict_id_is_valid (dict, name, true))
583 if (type == DP_DELIMITED)
585 if (!parse_format_specifier (lexer, &input)
586 || !fmt_check_input (&input))
590 output = fmt_for_output_from_input (&input,
591 settings_get_fmt_settings ());
595 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
596 enum fmt_type fmt_type;
600 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
603 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
604 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
606 if (!fmt_from_name (fmt_type_name, &fmt_type))
608 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
611 /* Compose input format. */
612 input.type = fmt_type;
613 input.w = lc - fc + 1;
615 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,
628 settings_get_fmt_settings ());
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);
637 if (type == DP_DELIMITED)
638 data_parser_add_delimited_field (parser, &input,
639 var_get_case_index (v),
642 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
647 while (lex_token (lexer) != T_ENDCMD);
649 reader = dfm_open_reader (fh, lexer, encoding);
653 data_parser_make_active_file (parser, ds, reader, dict, NULL, NULL);
659 data_parser_destroy (parser);
664 return CMD_CASCADING_FAILURE;
669 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
671 free (opts->cell_range);
672 free (opts->sheet_name);