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))
90 struct spreadsheet *spreadsheet = gnumeric_probe (sri.file_name);
91 reader = gnumeric_make_reader (spreadsheet, &sri, &opts);
92 dict = spreadsheet->dict;
94 else if (0 == strncasecmp (tok, "ODS", 3))
96 reader = ods_open_reader (&sri, &opts, &dict);
101 dataset_set_dict (ds, dict);
102 dataset_set_source (ds, reader);
103 destroy_spreadsheet_read_info (&sri, &opts);
107 destroy_spreadsheet_read_info (&sri, &opts);
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, struct spreadsheet_read_info *sri,
195 struct spreadsheet_read_options *opts)
197 opts->sheet_index = 1;
198 opts->sheet_name = NULL;
199 opts->cell_range = NULL;
200 sri->read_names = true;
202 sri->file_name = NULL;
204 lex_force_match (lexer, T_SLASH);
206 if (!lex_force_match_id (lexer, "FILE"))
209 lex_force_match (lexer, T_EQUALS);
211 if (!lex_force_string (lexer))
214 sri->file_name = utf8_to_filename (lex_tokcstr (lexer));
218 while (lex_match (lexer, T_SLASH) )
220 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
222 lex_match (lexer, T_EQUALS);
223 sri->asw = lex_integer (lexer);
226 else if (lex_match_id (lexer, "SHEET"))
228 lex_match (lexer, T_EQUALS);
229 if (lex_match_id (lexer, "NAME"))
231 if ( ! lex_force_string (lexer) )
234 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
235 opts->sheet_index = -1;
239 else if (lex_match_id (lexer, "INDEX"))
241 opts->sheet_index = lex_integer (lexer);
242 if (opts->sheet_index <= 0)
244 msg (SE, _("The sheet index must be greater than or equal to 1"));
251 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
252 "/SHEET", "NAME", "INDEX");
256 else if (lex_match_id (lexer, "CELLRANGE"))
258 lex_match (lexer, T_EQUALS);
260 if (lex_match_id (lexer, "FULL"))
262 opts->cell_range = NULL;
264 else if (lex_match_id (lexer, "RANGE"))
266 if ( ! lex_force_string (lexer) )
269 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
274 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
275 "/CELLRANGE", "FULL", "RANGE");
279 else if (lex_match_id (lexer, "READNAMES"))
281 lex_match (lexer, T_EQUALS);
283 if ( lex_match_id (lexer, "ON"))
285 sri->read_names = true;
287 else if (lex_match_id (lexer, "OFF"))
289 sri->read_names = false;
293 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
294 "/READNAMES", "ON", "OFF");
300 lex_error (lexer, NULL);
308 destroy_spreadsheet_read_info (sri, opts);
314 set_type (struct data_parser *parser, const char *subcommand,
315 enum data_parser_type type, bool *has_type)
319 data_parser_set_type (parser, type);
322 else if (type != data_parser_get_type (parser))
324 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
325 "was stated or implied earlier in this command."),
327 type == DP_FIXED ? "FIXED" : "DELIMITED",
328 type == DP_FIXED ? "DELIMITED" : "FIXED");
335 parse_get_txt (struct lexer *lexer, struct dataset *ds)
337 struct data_parser *parser = NULL;
338 struct dictionary *dict = dict_create (get_default_encoding ());
339 struct file_handle *fh = NULL;
340 struct dfm_reader *reader = NULL;
341 char *encoding = NULL;
345 enum data_parser_type type;
348 lex_force_match (lexer, T_SLASH);
350 if (!lex_force_match_id (lexer, "FILE"))
352 lex_force_match (lexer, T_EQUALS);
353 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
357 parser = data_parser_create (dict);
359 data_parser_set_type (parser, DP_DELIMITED);
360 data_parser_set_span (parser, false);
361 data_parser_set_quotes (parser, ss_empty ());
362 data_parser_set_empty_line_has_field (parser, true);
366 if (!lex_force_match (lexer, T_SLASH))
369 if (lex_match_id (lexer, "ENCODING"))
371 lex_match (lexer, T_EQUALS);
372 if (!lex_force_string (lexer))
376 encoding = ss_xstrdup (lex_tokss (lexer));
380 else if (lex_match_id (lexer, "ARRANGEMENT"))
384 lex_match (lexer, T_EQUALS);
385 if (lex_match_id (lexer, "FIXED"))
386 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
387 else if (lex_match_id (lexer, "DELIMITED"))
388 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
389 DP_DELIMITED, &has_type);
392 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
398 else if (lex_match_id (lexer, "FIRSTCASE"))
400 lex_match (lexer, T_EQUALS);
401 if (!lex_force_int (lexer))
403 if (lex_integer (lexer) < 1)
405 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
408 data_parser_set_skip (parser, lex_integer (lexer) - 1);
411 else if (lex_match_id_n (lexer, "DELCASE", 4))
413 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
415 lex_match (lexer, T_EQUALS);
416 if (lex_match_id (lexer, "LINE"))
417 data_parser_set_span (parser, false);
418 else if (lex_match_id (lexer, "VARIABLES"))
420 data_parser_set_span (parser, true);
422 /* VARIABLES takes an integer argument, but for no
423 good reason. We just ignore it. */
424 if (!lex_force_int (lexer))
430 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
434 else if (lex_match_id (lexer, "FIXCASE"))
436 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
438 lex_match (lexer, T_EQUALS);
439 if (!lex_force_int (lexer))
441 if (lex_integer (lexer) < 1)
443 msg (SE, _("Value of FIXCASE must be at least 1."));
446 data_parser_set_records (parser, lex_integer (lexer));
449 else if (lex_match_id (lexer, "IMPORTCASES"))
451 lex_match (lexer, T_EQUALS);
452 if (lex_match (lexer, T_ALL))
454 data_parser_set_case_limit (parser, -1);
455 data_parser_set_case_percent (parser, 100);
457 else if (lex_match_id (lexer, "FIRST"))
459 if (!lex_force_int (lexer))
461 if (lex_integer (lexer) < 1)
463 msg (SE, _("Value of FIRST must be at least 1."));
466 data_parser_set_case_limit (parser, lex_integer (lexer));
469 else if (lex_match_id (lexer, "PERCENT"))
471 if (!lex_force_int (lexer))
473 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
475 msg (SE, _("Value of PERCENT must be between 1 and 100."));
478 data_parser_set_case_percent (parser, lex_integer (lexer));
482 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
484 struct string hard_seps = DS_EMPTY_INITIALIZER;
485 const char *soft_seps = "";
489 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
491 lex_match (lexer, T_EQUALS);
493 if (!lex_force_string (lexer))
496 /* XXX should support multibyte UTF-8 characters */
497 s = lex_tokss (lexer);
498 if (ss_match_string (&s, ss_cstr ("\\t")))
499 ds_put_cstr (&hard_seps, "\t");
500 if (ss_match_string (&s, ss_cstr ("\\\\")))
501 ds_put_cstr (&hard_seps, "\\");
502 while ((c = ss_get_byte (&s)) != EOF)
506 ds_put_byte (&hard_seps, c);
507 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
508 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
509 ds_destroy (&hard_seps);
513 else if (lex_match_id (lexer, "QUALIFIERS"))
515 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
517 lex_match (lexer, T_EQUALS);
519 if (!lex_force_string (lexer))
522 /* XXX should support multibyte UTF-8 characters */
523 if (settings_get_syntax () == COMPATIBLE
524 && ss_length (lex_tokss (lexer)) != 1)
526 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
527 "must contain exactly one character."));
531 data_parser_set_quotes (parser, lex_tokss (lexer));
534 else if (settings_get_syntax () == ENHANCED
535 && lex_match_id (lexer, "ESCAPE"))
536 data_parser_set_quote_escape (parser, true);
537 else if (lex_match_id (lexer, "VARIABLES"))
541 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
545 lex_match (lexer, T_EQUALS);
548 type = data_parser_get_type (parser);
551 struct fmt_spec input, output;
555 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
557 if (!lex_force_int (lexer))
559 if (lex_integer (lexer) < record)
561 msg (SE, _("The record number specified, %ld, is at or "
562 "before the previous record, %d. Data "
563 "fields must be listed in order of "
564 "increasing record number."),
565 lex_integer (lexer), record);
568 if (lex_integer (lexer) > data_parser_get_records (parser))
570 msg (SE, _("The record number specified, %ld, exceeds "
571 "the number of records per case specified "
573 lex_integer (lexer), data_parser_get_records (parser));
576 record = lex_integer (lexer);
580 if (!lex_force_id (lexer)
581 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
583 name = xstrdup (lex_tokcstr (lexer));
586 if (type == DP_DELIMITED)
588 if (!parse_format_specifier (lexer, &input)
589 || !fmt_check_input (&input))
592 output = fmt_for_output_from_input (&input);
596 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
597 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);
612 /* Compose input format. */
613 input.type = fmt_type;
614 input.w = lc - fc + 1;
616 if (!fmt_check_input (&input))
619 /* Compose output format. */
622 output.type = fmt_type;
625 if (!fmt_check_output (&output))
629 output = fmt_for_output_from_input (&input);
632 v = dict_create_var (dict, name, fmt_var_width (&input));
635 msg (SE, _("%s is a duplicate variable name."), name);
638 var_set_both_formats (v, &output);
640 if (type == DP_DELIMITED)
641 data_parser_add_delimited_field (parser, &input,
642 var_get_case_index (v),
645 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
650 while (lex_token (lexer) != T_ENDCMD);
652 reader = dfm_open_reader (fh, lexer, encoding);
656 data_parser_make_active_file (parser, ds, reader, dict);
662 data_parser_destroy (parser);
667 return CMD_CASCADING_FAILURE;
672 destroy_spreadsheet_read_info (struct spreadsheet_read_info *sri,
673 struct spreadsheet_read_options *opts)
675 free (opts->sheet_name);
676 free (opts->cell_range);
677 free (sri->file_name);