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)
50 #ifdef ODF_READ_SUPPORT
51 static const bool odf_read_support = true;
53 static const bool odf_read_support = false;
54 struct spreadsheet *ods_probe (const char *filename, bool report_errors){}
57 #ifdef GNM_READ_SUPPORT
58 static const bool gnm_read_support = true;
60 static const bool gnm_read_support = false;
61 struct spreadsheet *gnumeric_probe (const char *filename, bool report_errors){}
64 static bool parse_spreadsheet (struct lexer *lexer, char **filename,
65 struct spreadsheet_read_options *opts);
67 static void destroy_spreadsheet_read_info (struct spreadsheet_read_options *);
69 static int parse_get_txt (struct lexer *lexer, struct dataset *);
70 static int parse_get_psql (struct lexer *lexer, struct dataset *);
73 cmd_get_data (struct lexer *lexer, struct dataset *ds)
76 struct spreadsheet_read_options opts;
78 opts.sheet_name = NULL;
79 opts.sheet_index = -1;
80 opts.cell_range = NULL;
81 opts.read_names = false;
84 if (! lex_force_match (lexer, T_SLASH))
87 if (!lex_force_match_id (lexer, "TYPE"))
90 if (!lex_force_match (lexer, T_EQUALS))
93 const char *s = lex_tokcstr (lexer);
98 if (lex_match_id (lexer, "TXT"))
101 return parse_get_txt (lexer, ds);
103 else if (lex_match_id (lexer, "PSQL"))
106 return parse_get_psql (lexer, ds);
108 else if (lex_match_id (lexer, "GNM") ||
109 lex_match_id (lexer, "ODS"))
111 char *filename = NULL;
112 if (!parse_spreadsheet (lexer, &filename, &opts))
115 struct spreadsheet *spreadsheet = NULL;
116 if ( gnm_read_support && 0 == strncasecmp (tok, "GNM", 3))
117 spreadsheet = gnumeric_probe (filename, true);
118 else if ( odf_read_support && 0 == strncasecmp (tok, "ODS", 3))
119 spreadsheet = ods_probe (filename, true);
122 if (spreadsheet == NULL)
125 struct casereader *reader = spreadsheet_make_reader (spreadsheet, &opts);
128 dataset_set_dict (ds, dict_clone (spreadsheet->dict));
129 dataset_set_source (ds, reader);
131 destroy_spreadsheet_read_info (&opts);
132 spreadsheet_unref (spreadsheet);
135 spreadsheet_unref (spreadsheet);
138 msg (SE, _("Unsupported TYPE %s."), tok);
142 destroy_spreadsheet_read_info (&opts);
148 parse_get_psql (struct lexer *lexer, struct dataset *ds)
150 struct psql_read_info psql;
151 psql.allow_clear = false;
152 psql.conninfo = NULL;
155 ds_init_empty (&psql.sql);
157 if (! lex_force_match (lexer, T_SLASH))
160 if (!lex_force_match_id (lexer, "CONNECT"))
163 if (! lex_force_match (lexer, T_EQUALS))
166 if (!lex_force_string (lexer))
169 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
173 while (lex_match (lexer, T_SLASH) )
175 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
177 lex_match (lexer, T_EQUALS);
178 if (lex_force_int (lexer))
180 psql.str_width = lex_integer (lexer);
184 else if ( lex_match_id (lexer, "BSIZE"))
186 lex_match (lexer, T_EQUALS);
187 if (lex_force_int (lexer))
189 psql.bsize = lex_integer (lexer);
193 else if ( lex_match_id (lexer, "UNENCRYPTED"))
195 psql.allow_clear = true;
197 else if (lex_match_id (lexer, "SQL"))
199 lex_match (lexer, T_EQUALS);
200 if ( ! lex_force_string (lexer) )
203 ds_put_substring (&psql.sql, lex_tokss (lexer));
208 struct dictionary *dict = NULL;
209 struct casereader *reader = psql_open_reader (&psql, &dict);
213 dataset_set_dict (ds, dict);
214 dataset_set_source (ds, reader);
218 ds_destroy (&psql.sql);
219 free (psql.conninfo);
225 ds_destroy (&psql.sql);
226 free (psql.conninfo);
232 parse_spreadsheet (struct lexer *lexer, char **filename,
233 struct spreadsheet_read_options *opts)
235 opts->sheet_index = 1;
236 opts->sheet_name = NULL;
237 opts->cell_range = NULL;
238 opts->read_names = true;
241 if (! lex_force_match (lexer, T_SLASH))
244 if (!lex_force_match_id (lexer, "FILE"))
247 if (! lex_force_match (lexer, T_EQUALS))
250 if (!lex_force_string (lexer))
253 *filename = utf8_to_filename (lex_tokcstr (lexer));
257 while (lex_match (lexer, T_SLASH) )
259 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
261 lex_match (lexer, T_EQUALS);
262 if (lex_force_int (lexer))
264 opts->asw = lex_integer (lexer);
268 else if (lex_match_id (lexer, "SHEET"))
270 lex_match (lexer, T_EQUALS);
271 if (lex_match_id (lexer, "NAME"))
273 if ( ! lex_force_string (lexer) )
276 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
277 opts->sheet_index = -1;
281 else if (lex_match_id (lexer, "INDEX"))
283 if (!lex_force_int (lexer))
286 opts->sheet_index = lex_integer (lexer);
287 if (opts->sheet_index <= 0)
289 msg (SE, _("The sheet index must be greater than or equal to 1"));
296 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
297 "/SHEET", "NAME", "INDEX");
301 else if (lex_match_id (lexer, "CELLRANGE"))
303 lex_match (lexer, T_EQUALS);
305 if (lex_match_id (lexer, "FULL"))
307 opts->cell_range = NULL;
309 else if (lex_match_id (lexer, "RANGE"))
311 if ( ! lex_force_string (lexer) )
314 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
319 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
320 "/CELLRANGE", "FULL", "RANGE");
324 else if (lex_match_id (lexer, "READNAMES"))
326 lex_match (lexer, T_EQUALS);
328 if ( lex_match_id (lexer, "ON"))
330 opts->read_names = true;
332 else if (lex_match_id (lexer, "OFF"))
334 opts->read_names = false;
338 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
339 "/READNAMES", "ON", "OFF");
345 lex_error (lexer, NULL);
358 set_type (struct data_parser *parser, const char *subcommand,
359 enum data_parser_type type, bool *has_type)
363 data_parser_set_type (parser, type);
366 else if (type != data_parser_get_type (parser))
368 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
369 "was stated or implied earlier in this command."),
371 type == DP_FIXED ? "FIXED" : "DELIMITED",
372 type == DP_FIXED ? "DELIMITED" : "FIXED");
379 parse_get_txt (struct lexer *lexer, struct dataset *ds)
381 struct data_parser *parser = NULL;
382 struct dictionary *dict = dict_create (get_default_encoding ());
383 struct file_handle *fh = NULL;
384 struct dfm_reader *reader = NULL;
385 char *encoding = NULL;
389 enum data_parser_type type;
392 if (! lex_force_match (lexer, T_SLASH))
395 if (!lex_force_match_id (lexer, "FILE"))
397 if (! lex_force_match (lexer, T_EQUALS))
399 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
403 parser = data_parser_create (dict);
405 data_parser_set_type (parser, DP_DELIMITED);
406 data_parser_set_span (parser, false);
407 data_parser_set_quotes (parser, ss_empty ());
408 data_parser_set_quote_escape (parser, true);
409 data_parser_set_empty_line_has_field (parser, true);
413 if (!lex_force_match (lexer, T_SLASH))
416 if (lex_match_id (lexer, "ENCODING"))
418 lex_match (lexer, T_EQUALS);
419 if (!lex_force_string (lexer))
423 encoding = ss_xstrdup (lex_tokss (lexer));
427 else if (lex_match_id (lexer, "ARRANGEMENT"))
431 lex_match (lexer, T_EQUALS);
432 if (lex_match_id (lexer, "FIXED"))
433 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
434 else if (lex_match_id (lexer, "DELIMITED"))
435 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
436 DP_DELIMITED, &has_type);
439 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
445 else if (lex_match_id (lexer, "FIRSTCASE"))
447 lex_match (lexer, T_EQUALS);
448 if (!lex_force_int (lexer))
450 if (lex_integer (lexer) < 1)
452 msg (SE, _("Value of %s must be 1 or greater."), "FIRSTCASE");
455 data_parser_set_skip (parser, lex_integer (lexer) - 1);
458 else if (lex_match_id_n (lexer, "DELCASE", 4))
460 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
462 lex_match (lexer, T_EQUALS);
463 if (lex_match_id (lexer, "LINE"))
464 data_parser_set_span (parser, false);
465 else if (lex_match_id (lexer, "VARIABLES"))
467 data_parser_set_span (parser, true);
469 /* VARIABLES takes an integer argument, but for no
470 good reason. We just ignore it. */
471 if (!lex_force_int (lexer))
477 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
481 else if (lex_match_id (lexer, "FIXCASE"))
483 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
485 lex_match (lexer, T_EQUALS);
486 if (!lex_force_int (lexer))
488 if (lex_integer (lexer) < 1)
490 msg (SE, _("Value of %s must be 1 or greater."), "FIXCASE");
493 data_parser_set_records (parser, lex_integer (lexer));
496 else if (lex_match_id (lexer, "IMPORTCASES"))
498 lex_match (lexer, T_EQUALS);
499 if (lex_match (lexer, T_ALL))
503 else if (lex_match_id (lexer, "FIRST"))
505 if (!lex_force_int (lexer))
509 else if (lex_match_id (lexer, "PERCENT"))
511 if (!lex_force_int (lexer))
515 msg (SW, _("Ignoring obsolete IMPORTCASES subcommand. (N OF CASES "
516 "or SAMPLE may be used to substitute.)"));
518 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
520 struct string hard_seps = DS_EMPTY_INITIALIZER;
521 const char *soft_seps = "";
525 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
527 lex_match (lexer, T_EQUALS);
529 if (!lex_force_string (lexer))
532 /* XXX should support multibyte UTF-8 characters */
533 s = lex_tokss (lexer);
534 if (ss_match_string (&s, ss_cstr ("\\t")))
535 ds_put_cstr (&hard_seps, "\t");
536 if (ss_match_string (&s, ss_cstr ("\\\\")))
537 ds_put_cstr (&hard_seps, "\\");
538 while ((c = ss_get_byte (&s)) != EOF)
542 ds_put_byte (&hard_seps, c);
543 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
544 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
545 ds_destroy (&hard_seps);
549 else if (lex_match_id (lexer, "QUALIFIERS"))
551 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
553 lex_match (lexer, T_EQUALS);
555 if (!lex_force_string (lexer))
558 /* XXX should support multibyte UTF-8 characters */
559 if (settings_get_syntax () == COMPATIBLE
560 && ss_length (lex_tokss (lexer)) != 1)
562 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
563 "must contain exactly one character."));
567 data_parser_set_quotes (parser, lex_tokss (lexer));
570 else if (lex_match_id (lexer, "VARIABLES"))
574 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
578 lex_match (lexer, T_EQUALS);
581 type = data_parser_get_type (parser);
584 struct fmt_spec input, output;
588 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
590 if (!lex_force_int (lexer))
592 if (lex_integer (lexer) < record)
594 msg (SE, _("The record number specified, %ld, is at or "
595 "before the previous record, %d. Data "
596 "fields must be listed in order of "
597 "increasing record number."),
598 lex_integer (lexer), record);
601 if (lex_integer (lexer) > data_parser_get_records (parser))
603 msg (SE, _("The record number specified, %ld, exceeds "
604 "the number of records per case specified "
606 lex_integer (lexer), data_parser_get_records (parser));
609 record = lex_integer (lexer);
613 name = xstrdup (lex_tokcstr (lexer));
614 if (!lex_force_id (lexer)
615 || !dict_id_is_valid (dict, name, true))
620 if (type == DP_DELIMITED)
622 if (!parse_format_specifier (lexer, &input)
623 || !fmt_check_input (&input))
627 output = fmt_for_output_from_input (&input);
631 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
632 enum fmt_type fmt_type;
635 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
638 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
639 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
641 if (!fmt_from_name (fmt_type_name, &fmt_type))
643 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
646 /* Compose input format. */
647 input.type = fmt_type;
648 input.w = lc - fc + 1;
650 if (!fmt_check_input (&input))
652 /* Compose output format. */
655 output.type = fmt_type;
658 if (!fmt_check_output (&output))
662 output = fmt_for_output_from_input (&input);
664 v = dict_create_var (dict, name, fmt_var_width (&input));
667 msg (SE, _("%s is a duplicate variable name."), name);
670 var_set_both_formats (v, &output);
671 if (type == DP_DELIMITED)
672 data_parser_add_delimited_field (parser, &input,
673 var_get_case_index (v),
676 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
681 while (lex_token (lexer) != T_ENDCMD);
683 reader = dfm_open_reader (fh, lexer, encoding);
687 data_parser_make_active_file (parser, ds, reader, dict);
693 data_parser_destroy (parser);
698 return CMD_CASCADING_FAILURE;
703 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
705 free (opts->cell_range);
706 free (opts->sheet_name);