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, struct spreadsheet_read_info *sri,
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 struct casereader *reader = NULL;
82 struct dictionary *dict = NULL;
83 struct spreadsheet_read_info sri;
84 struct spreadsheet_read_options opts;
85 if (!parse_spreadsheet (lexer, &sri, &opts))
88 if ( 0 == strncasecmp (tok, "GNM", 3))
89 reader = gnumeric_open_reader (&sri, &opts, &dict);
90 else if (0 == strncasecmp (tok, "ODS", 3))
91 reader = ods_open_reader (&sri, &opts, &dict);
95 dataset_set_dict (ds, dict);
96 dataset_set_source (ds, reader);
97 destroy_spreadsheet_read_info (&sri, &opts);
101 destroy_spreadsheet_read_info (&sri, &opts);
104 msg (SE, _("Unsupported TYPE %s."), tok);
112 parse_get_psql (struct lexer *lexer, struct dataset *ds)
114 struct psql_read_info psql;
115 psql.allow_clear = false;
116 psql.conninfo = NULL;
119 ds_init_empty (&psql.sql);
121 lex_force_match (lexer, T_SLASH);
123 if (!lex_force_match_id (lexer, "CONNECT"))
126 lex_force_match (lexer, T_EQUALS);
128 if (!lex_force_string (lexer))
131 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
135 while (lex_match (lexer, T_SLASH) )
137 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
139 lex_match (lexer, T_EQUALS);
140 psql.str_width = lex_integer (lexer);
143 else if ( lex_match_id (lexer, "BSIZE"))
145 lex_match (lexer, T_EQUALS);
146 psql.bsize = lex_integer (lexer);
149 else if ( lex_match_id (lexer, "UNENCRYPTED"))
151 psql.allow_clear = true;
153 else if (lex_match_id (lexer, "SQL"))
155 lex_match (lexer, T_EQUALS);
156 if ( ! lex_force_string (lexer) )
159 ds_put_substring (&psql.sql, lex_tokss (lexer));
164 struct dictionary *dict = NULL;
165 struct casereader *reader = psql_open_reader (&psql, &dict);
169 dataset_set_dict (ds, dict);
170 dataset_set_source (ds, reader);
174 ds_destroy (&psql.sql);
175 free (psql.conninfo);
181 ds_destroy (&psql.sql);
182 free (psql.conninfo);
188 parse_spreadsheet (struct lexer *lexer, struct spreadsheet_read_info *sri,
189 struct spreadsheet_read_options *opts)
191 opts->sheet_index = 1;
192 opts->sheet_name = NULL;
193 opts->cell_range = NULL;
194 sri->read_names = true;
196 sri->file_name = NULL;
198 lex_force_match (lexer, T_SLASH);
200 if (!lex_force_match_id (lexer, "FILE"))
203 lex_force_match (lexer, T_EQUALS);
205 if (!lex_force_string (lexer))
208 sri->file_name = utf8_to_filename (lex_tokcstr (lexer));
212 while (lex_match (lexer, T_SLASH) )
214 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
216 lex_match (lexer, T_EQUALS);
217 sri->asw = lex_integer (lexer);
220 else if (lex_match_id (lexer, "SHEET"))
222 lex_match (lexer, T_EQUALS);
223 if (lex_match_id (lexer, "NAME"))
225 if ( ! lex_force_string (lexer) )
228 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
229 opts->sheet_index = -1;
233 else if (lex_match_id (lexer, "INDEX"))
235 sri->sheet_index = lex_integer (lexer);
236 if (sri->sheet_index <= 0)
238 msg (SE, _("The sheet index must be greater than or equal to 1"));
245 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
246 "/SHEET", "NAME", "INDEX");
250 else if (lex_match_id (lexer, "CELLRANGE"))
252 lex_match (lexer, T_EQUALS);
254 if (lex_match_id (lexer, "FULL"))
256 opts->cell_range = NULL;
258 else if (lex_match_id (lexer, "RANGE"))
260 if ( ! lex_force_string (lexer) )
263 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
268 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
269 "/CELLRANGE", "FULL", "RANGE");
273 else if (lex_match_id (lexer, "READNAMES"))
275 lex_match (lexer, T_EQUALS);
277 if ( lex_match_id (lexer, "ON"))
279 sri->read_names = true;
281 else if (lex_match_id (lexer, "OFF"))
283 sri->read_names = false;
287 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
288 "/READNAMES", "ON", "OFF");
294 lex_error (lexer, NULL);
302 destroy_spreadsheet_read_info (sri, opts);
308 set_type (struct data_parser *parser, const char *subcommand,
309 enum data_parser_type type, bool *has_type)
313 data_parser_set_type (parser, type);
316 else if (type != data_parser_get_type (parser))
318 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
319 "was stated or implied earlier in this command."),
321 type == DP_FIXED ? "FIXED" : "DELIMITED",
322 type == DP_FIXED ? "DELIMITED" : "FIXED");
329 parse_get_txt (struct lexer *lexer, struct dataset *ds)
331 struct data_parser *parser = NULL;
332 struct dictionary *dict = dict_create (get_default_encoding ());
333 struct file_handle *fh = NULL;
334 struct dfm_reader *reader = NULL;
335 char *encoding = NULL;
339 enum data_parser_type type;
342 lex_force_match (lexer, T_SLASH);
344 if (!lex_force_match_id (lexer, "FILE"))
346 lex_force_match (lexer, T_EQUALS);
347 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
351 parser = data_parser_create (dict);
353 data_parser_set_type (parser, DP_DELIMITED);
354 data_parser_set_span (parser, false);
355 data_parser_set_quotes (parser, ss_empty ());
356 data_parser_set_empty_line_has_field (parser, true);
360 if (!lex_force_match (lexer, T_SLASH))
363 if (lex_match_id (lexer, "ENCODING"))
365 lex_match (lexer, T_EQUALS);
366 if (!lex_force_string (lexer))
370 encoding = ss_xstrdup (lex_tokss (lexer));
374 else if (lex_match_id (lexer, "ARRANGEMENT"))
378 lex_match (lexer, T_EQUALS);
379 if (lex_match_id (lexer, "FIXED"))
380 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
381 else if (lex_match_id (lexer, "DELIMITED"))
382 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
383 DP_DELIMITED, &has_type);
386 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
392 else if (lex_match_id (lexer, "FIRSTCASE"))
394 lex_match (lexer, T_EQUALS);
395 if (!lex_force_int (lexer))
397 if (lex_integer (lexer) < 1)
399 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
402 data_parser_set_skip (parser, lex_integer (lexer) - 1);
405 else if (lex_match_id_n (lexer, "DELCASE", 4))
407 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
409 lex_match (lexer, T_EQUALS);
410 if (lex_match_id (lexer, "LINE"))
411 data_parser_set_span (parser, false);
412 else if (lex_match_id (lexer, "VARIABLES"))
414 data_parser_set_span (parser, true);
416 /* VARIABLES takes an integer argument, but for no
417 good reason. We just ignore it. */
418 if (!lex_force_int (lexer))
424 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
428 else if (lex_match_id (lexer, "FIXCASE"))
430 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
432 lex_match (lexer, T_EQUALS);
433 if (!lex_force_int (lexer))
435 if (lex_integer (lexer) < 1)
437 msg (SE, _("Value of FIXCASE must be at least 1."));
440 data_parser_set_records (parser, lex_integer (lexer));
443 else if (lex_match_id (lexer, "IMPORTCASES"))
445 lex_match (lexer, T_EQUALS);
446 if (lex_match (lexer, T_ALL))
448 data_parser_set_case_limit (parser, -1);
449 data_parser_set_case_percent (parser, 100);
451 else if (lex_match_id (lexer, "FIRST"))
453 if (!lex_force_int (lexer))
455 if (lex_integer (lexer) < 1)
457 msg (SE, _("Value of FIRST must be at least 1."));
460 data_parser_set_case_limit (parser, lex_integer (lexer));
463 else if (lex_match_id (lexer, "PERCENT"))
465 if (!lex_force_int (lexer))
467 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
469 msg (SE, _("Value of PERCENT must be between 1 and 100."));
472 data_parser_set_case_percent (parser, lex_integer (lexer));
476 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
478 struct string hard_seps = DS_EMPTY_INITIALIZER;
479 const char *soft_seps = "";
483 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
485 lex_match (lexer, T_EQUALS);
487 if (!lex_force_string (lexer))
490 /* XXX should support multibyte UTF-8 characters */
491 s = lex_tokss (lexer);
492 if (ss_match_string (&s, ss_cstr ("\\t")))
493 ds_put_cstr (&hard_seps, "\t");
494 if (ss_match_string (&s, ss_cstr ("\\\\")))
495 ds_put_cstr (&hard_seps, "\\");
496 while ((c = ss_get_byte (&s)) != EOF)
500 ds_put_byte (&hard_seps, c);
501 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
502 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
503 ds_destroy (&hard_seps);
507 else if (lex_match_id (lexer, "QUALIFIERS"))
509 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
511 lex_match (lexer, T_EQUALS);
513 if (!lex_force_string (lexer))
516 /* XXX should support multibyte UTF-8 characters */
517 if (settings_get_syntax () == COMPATIBLE
518 && ss_length (lex_tokss (lexer)) != 1)
520 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
521 "must contain exactly one character."));
525 data_parser_set_quotes (parser, lex_tokss (lexer));
528 else if (settings_get_syntax () == ENHANCED
529 && lex_match_id (lexer, "ESCAPE"))
530 data_parser_set_quote_escape (parser, true);
531 else if (lex_match_id (lexer, "VARIABLES"))
535 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
539 lex_match (lexer, T_EQUALS);
542 type = data_parser_get_type (parser);
545 struct fmt_spec input, output;
549 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
551 if (!lex_force_int (lexer))
553 if (lex_integer (lexer) < record)
555 msg (SE, _("The record number specified, %ld, is at or "
556 "before the previous record, %d. Data "
557 "fields must be listed in order of "
558 "increasing record number."),
559 lex_integer (lexer), record);
562 if (lex_integer (lexer) > data_parser_get_records (parser))
564 msg (SE, _("The record number specified, %ld, exceeds "
565 "the number of records per case specified "
567 lex_integer (lexer), data_parser_get_records (parser));
570 record = lex_integer (lexer);
574 if (!lex_force_id (lexer)
575 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
577 name = xstrdup (lex_tokcstr (lexer));
580 if (type == DP_DELIMITED)
582 if (!parse_format_specifier (lexer, &input)
583 || !fmt_check_input (&input))
586 output = fmt_for_output_from_input (&input);
590 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
591 enum fmt_type fmt_type;
594 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
597 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
598 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
600 if (!fmt_from_name (fmt_type_name, &fmt_type))
602 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
606 /* Compose input format. */
607 input.type = fmt_type;
608 input.w = lc - fc + 1;
610 if (!fmt_check_input (&input))
613 /* Compose output format. */
616 output.type = fmt_type;
619 if (!fmt_check_output (&output))
623 output = fmt_for_output_from_input (&input);
626 v = dict_create_var (dict, name, fmt_var_width (&input));
629 msg (SE, _("%s is a duplicate variable name."), name);
632 var_set_both_formats (v, &output);
634 if (type == DP_DELIMITED)
635 data_parser_add_delimited_field (parser, &input,
636 var_get_case_index (v),
639 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
644 while (lex_token (lexer) != T_ENDCMD);
646 reader = dfm_open_reader (fh, lexer, encoding);
650 data_parser_make_active_file (parser, ds, reader, dict);
656 data_parser_destroy (parser);
661 return CMD_CASCADING_FAILURE;
666 destroy_spreadsheet_read_info (struct spreadsheet_read_info *sri,
667 struct spreadsheet_read_options *opts)
669 free (opts->sheet_name);
670 free (opts->cell_range);
671 free (sri->file_name);