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 (lexer))
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 (lexer))
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 (lexer))
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 (lexer))
275 opts->sheet_index = lex_integer (lexer);
276 if (opts->sheet_index <= 0)
278 msg (SE, _("The sheet index must be greater than or equal to 1"));
285 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
286 "/SHEET", "NAME", "INDEX");
290 else if (lex_match_id (lexer, "CELLRANGE"))
292 lex_match (lexer, T_EQUALS);
294 if (lex_match_id (lexer, "FULL"))
296 opts->cell_range = NULL;
298 else if (lex_match_id (lexer, "RANGE"))
300 if (! lex_force_string (lexer))
303 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
308 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
309 "/CELLRANGE", "FULL", "RANGE");
313 else if (lex_match_id (lexer, "READNAMES"))
315 lex_match (lexer, T_EQUALS);
317 if (lex_match_id (lexer, "ON"))
319 opts->read_names = true;
321 else if (lex_match_id (lexer, "OFF"))
323 opts->read_names = false;
327 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
328 "/READNAMES", "ON", "OFF");
334 lex_error (lexer, NULL);
347 set_type (struct data_parser *parser, const char *subcommand,
348 enum data_parser_type type, bool *has_type)
352 data_parser_set_type (parser, type);
355 else if (type != data_parser_get_type (parser))
357 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
358 "was stated or implied earlier in this command."),
360 type == DP_FIXED ? "FIXED" : "DELIMITED",
361 type == DP_FIXED ? "DELIMITED" : "FIXED");
368 parse_get_txt (struct lexer *lexer, struct dataset *ds)
370 struct data_parser *parser = NULL;
371 struct dictionary *dict = dict_create (get_default_encoding ());
372 struct file_handle *fh = NULL;
373 struct dfm_reader *reader = NULL;
374 char *encoding = NULL;
378 enum data_parser_type type;
381 if (! lex_force_match (lexer, T_SLASH))
384 if (!lex_force_match_id (lexer, "FILE"))
386 if (! lex_force_match (lexer, T_EQUALS))
388 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
392 parser = data_parser_create (dict);
394 data_parser_set_type (parser, DP_DELIMITED);
395 data_parser_set_span (parser, false);
396 data_parser_set_quotes (parser, ss_empty ());
397 data_parser_set_quote_escape (parser, true);
398 data_parser_set_empty_line_has_field (parser, true);
402 if (!lex_force_match (lexer, T_SLASH))
405 if (lex_match_id (lexer, "ENCODING"))
407 lex_match (lexer, T_EQUALS);
408 if (!lex_force_string (lexer))
412 encoding = ss_xstrdup (lex_tokss (lexer));
416 else if (lex_match_id (lexer, "ARRANGEMENT"))
420 lex_match (lexer, T_EQUALS);
421 if (lex_match_id (lexer, "FIXED"))
422 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
423 else if (lex_match_id (lexer, "DELIMITED"))
424 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
425 DP_DELIMITED, &has_type);
428 lex_error_expecting (lexer, "FIXED", "DELIMITED");
434 else if (lex_match_id (lexer, "FIRSTCASE"))
436 lex_match (lexer, T_EQUALS);
437 if (!lex_force_int (lexer))
439 if (lex_integer (lexer) < 1)
441 msg (SE, _("Value of %s must be 1 or greater."), "FIRSTCASE");
444 data_parser_set_skip (parser, lex_integer (lexer) - 1);
447 else if (lex_match_id_n (lexer, "DELCASE", 4))
449 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
451 lex_match (lexer, T_EQUALS);
452 if (lex_match_id (lexer, "LINE"))
453 data_parser_set_span (parser, false);
454 else if (lex_match_id (lexer, "VARIABLES"))
456 data_parser_set_span (parser, true);
458 /* VARIABLES takes an integer argument, but for no
459 good reason. We just ignore it. */
460 if (!lex_force_int (lexer))
466 lex_error_expecting (lexer, "LINE", "VARIABLES");
470 else if (lex_match_id (lexer, "FIXCASE"))
472 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
474 lex_match (lexer, T_EQUALS);
475 if (!lex_force_int (lexer))
477 if (lex_integer (lexer) < 1)
479 msg (SE, _("Value of %s must be 1 or greater."), "FIXCASE");
482 data_parser_set_records (parser, lex_integer (lexer));
485 else if (lex_match_id (lexer, "IMPORTCASES"))
487 lex_match (lexer, T_EQUALS);
488 if (lex_match (lexer, T_ALL))
492 else if (lex_match_id (lexer, "FIRST"))
494 if (!lex_force_int (lexer))
498 else if (lex_match_id (lexer, "PERCENT"))
500 if (!lex_force_int (lexer))
504 msg (SW, _("Ignoring obsolete IMPORTCASES subcommand. (N OF CASES "
505 "or SAMPLE may be used to substitute.)"));
507 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
509 struct string hard_seps = DS_EMPTY_INITIALIZER;
510 const char *soft_seps = "";
514 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
516 lex_match (lexer, T_EQUALS);
518 if (!lex_force_string (lexer))
521 /* XXX should support multibyte UTF-8 characters */
522 s = lex_tokss (lexer);
523 if (ss_match_string (&s, ss_cstr ("\\t")))
524 ds_put_cstr (&hard_seps, "\t");
525 if (ss_match_string (&s, ss_cstr ("\\\\")))
526 ds_put_cstr (&hard_seps, "\\");
527 while ((c = ss_get_byte (&s)) != EOF)
531 ds_put_byte (&hard_seps, c);
532 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
533 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
534 ds_destroy (&hard_seps);
538 else if (lex_match_id (lexer, "QUALIFIERS"))
540 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
542 lex_match (lexer, T_EQUALS);
544 if (!lex_force_string (lexer))
547 /* XXX should support multibyte UTF-8 characters */
548 if (settings_get_syntax () == COMPATIBLE
549 && ss_length (lex_tokss (lexer)) != 1)
551 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
552 "must contain exactly one character."));
556 data_parser_set_quotes (parser, lex_tokss (lexer));
559 else if (lex_match_id (lexer, "VARIABLES"))
563 lex_error_expecting (lexer, "VARIABLES");
567 lex_match (lexer, T_EQUALS);
570 type = data_parser_get_type (parser);
573 struct fmt_spec input, output;
577 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
579 if (!lex_force_int (lexer))
581 if (lex_integer (lexer) < record)
583 msg (SE, _("The record number specified, %ld, is at or "
584 "before the previous record, %d. Data "
585 "fields must be listed in order of "
586 "increasing record number."),
587 lex_integer (lexer), record);
590 if (lex_integer (lexer) > data_parser_get_records (parser))
592 msg (SE, _("The record number specified, %ld, exceeds "
593 "the number of records per case specified "
595 lex_integer (lexer), data_parser_get_records (parser));
598 record = lex_integer (lexer);
602 const char * tstr = lex_tokcstr (lexer);
605 lex_error (lexer, NULL);
608 name = xstrdup (tstr);
609 if (!lex_force_id (lexer)
610 || !dict_id_is_valid (dict, name, true))
615 if (type == DP_DELIMITED)
617 if (!parse_format_specifier (lexer, &input)
618 || !fmt_check_input (&input))
622 output = fmt_for_output_from_input (&input);
626 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
627 enum fmt_type fmt_type;
630 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
633 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
634 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
636 if (!fmt_from_name (fmt_type_name, &fmt_type))
638 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
641 /* Compose input format. */
642 input.type = fmt_type;
643 input.w = lc - fc + 1;
645 if (!fmt_check_input (&input))
647 /* Compose output format. */
650 output.type = fmt_type;
653 if (!fmt_check_output (&output))
657 output = fmt_for_output_from_input (&input);
659 v = dict_create_var (dict, name, fmt_var_width (&input));
662 msg (SE, _("%s is a duplicate variable name."), name);
665 var_set_both_formats (v, &output);
666 if (type == DP_DELIMITED)
667 data_parser_add_delimited_field (parser, &input,
668 var_get_case_index (v),
671 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
676 while (lex_token (lexer) != T_ENDCMD);
678 reader = dfm_open_reader (fh, lexer, encoding);
682 data_parser_make_active_file (parser, ds, reader, dict, NULL, NULL);
688 data_parser_destroy (parser);
693 return CMD_CASCADING_FAILURE;
698 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
700 free (opts->cell_range);
701 free (opts->sheet_name);