1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012,
3 2013 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 lex_force_match (lexer, T_SLASH);
71 if (!lex_force_match_id (lexer, "TYPE"))
74 lex_force_match (lexer, T_EQUALS);
76 tok = strdup (lex_tokcstr (lexer));
77 if (lex_match_id (lexer, "TXT"))
80 return parse_get_txt (lexer, ds);
82 else if (lex_match_id (lexer, "PSQL"))
85 return parse_get_psql (lexer, ds);
87 else if (lex_match_id (lexer, "GNM") ||
88 lex_match_id (lexer, "ODS"))
90 char *filename = NULL;
91 struct casereader *reader = NULL;
92 struct dictionary *dict = NULL;
94 if (!parse_spreadsheet (lexer, &filename, &opts))
97 if ( 0 == strncasecmp (tok, "GNM", 3))
99 struct spreadsheet *spreadsheet = gnumeric_probe (filename, true);
100 if (spreadsheet == NULL)
102 reader = gnumeric_make_reader (spreadsheet, &opts);
103 dict = spreadsheet->dict;
104 gnumeric_destroy (spreadsheet);
106 else if (0 == strncasecmp (tok, "ODS", 3))
108 struct spreadsheet *spreadsheet = ods_probe (filename, true);
109 if (spreadsheet == NULL)
111 reader = ods_make_reader (spreadsheet, &opts);
112 dict = spreadsheet->dict;
113 ods_destroy (spreadsheet);
120 dataset_set_dict (ds, dict);
121 dataset_set_source (ds, reader);
123 destroy_spreadsheet_read_info (&opts);
128 msg (SE, _("Unsupported TYPE %s."), tok);
132 destroy_spreadsheet_read_info (&opts);
138 parse_get_psql (struct lexer *lexer, struct dataset *ds)
140 struct psql_read_info psql;
141 psql.allow_clear = false;
142 psql.conninfo = NULL;
145 ds_init_empty (&psql.sql);
147 lex_force_match (lexer, T_SLASH);
149 if (!lex_force_match_id (lexer, "CONNECT"))
152 lex_force_match (lexer, T_EQUALS);
154 if (!lex_force_string (lexer))
157 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
161 while (lex_match (lexer, T_SLASH) )
163 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
165 lex_match (lexer, T_EQUALS);
166 psql.str_width = lex_integer (lexer);
169 else if ( lex_match_id (lexer, "BSIZE"))
171 lex_match (lexer, T_EQUALS);
172 psql.bsize = lex_integer (lexer);
175 else if ( lex_match_id (lexer, "UNENCRYPTED"))
177 psql.allow_clear = true;
179 else if (lex_match_id (lexer, "SQL"))
181 lex_match (lexer, T_EQUALS);
182 if ( ! lex_force_string (lexer) )
185 ds_put_substring (&psql.sql, lex_tokss (lexer));
190 struct dictionary *dict = NULL;
191 struct casereader *reader = psql_open_reader (&psql, &dict);
195 dataset_set_dict (ds, dict);
196 dataset_set_source (ds, reader);
200 ds_destroy (&psql.sql);
201 free (psql.conninfo);
207 ds_destroy (&psql.sql);
208 free (psql.conninfo);
214 parse_spreadsheet (struct lexer *lexer, char **filename,
215 struct spreadsheet_read_options *opts)
217 opts->sheet_index = 1;
218 opts->sheet_name = NULL;
219 opts->cell_range = NULL;
220 opts->read_names = true;
223 lex_force_match (lexer, T_SLASH);
225 if (!lex_force_match_id (lexer, "FILE"))
228 lex_force_match (lexer, T_EQUALS);
230 if (!lex_force_string (lexer))
233 *filename = utf8_to_filename (lex_tokcstr (lexer));
237 while (lex_match (lexer, T_SLASH) )
239 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
241 lex_match (lexer, T_EQUALS);
242 opts->asw = lex_integer (lexer);
245 else if (lex_match_id (lexer, "SHEET"))
247 lex_match (lexer, T_EQUALS);
248 if (lex_match_id (lexer, "NAME"))
250 if ( ! lex_force_string (lexer) )
253 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
254 opts->sheet_index = -1;
258 else if (lex_match_id (lexer, "INDEX"))
260 opts->sheet_index = lex_integer (lexer);
261 if (opts->sheet_index <= 0)
263 msg (SE, _("The sheet index must be greater than or equal to 1"));
270 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
271 "/SHEET", "NAME", "INDEX");
275 else if (lex_match_id (lexer, "CELLRANGE"))
277 lex_match (lexer, T_EQUALS);
279 if (lex_match_id (lexer, "FULL"))
281 opts->cell_range = NULL;
283 else if (lex_match_id (lexer, "RANGE"))
285 if ( ! lex_force_string (lexer) )
288 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
293 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
294 "/CELLRANGE", "FULL", "RANGE");
298 else if (lex_match_id (lexer, "READNAMES"))
300 lex_match (lexer, T_EQUALS);
302 if ( lex_match_id (lexer, "ON"))
304 opts->read_names = true;
306 else if (lex_match_id (lexer, "OFF"))
308 opts->read_names = false;
312 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
313 "/READNAMES", "ON", "OFF");
319 lex_error (lexer, NULL);
332 set_type (struct data_parser *parser, const char *subcommand,
333 enum data_parser_type type, bool *has_type)
337 data_parser_set_type (parser, type);
340 else if (type != data_parser_get_type (parser))
342 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
343 "was stated or implied earlier in this command."),
345 type == DP_FIXED ? "FIXED" : "DELIMITED",
346 type == DP_FIXED ? "DELIMITED" : "FIXED");
353 parse_get_txt (struct lexer *lexer, struct dataset *ds)
355 struct data_parser *parser = NULL;
356 struct dictionary *dict = dict_create (get_default_encoding ());
357 struct file_handle *fh = NULL;
358 struct dfm_reader *reader = NULL;
359 char *encoding = NULL;
363 enum data_parser_type type;
366 lex_force_match (lexer, T_SLASH);
368 if (!lex_force_match_id (lexer, "FILE"))
370 lex_force_match (lexer, T_EQUALS);
371 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
375 parser = data_parser_create (dict);
377 data_parser_set_type (parser, DP_DELIMITED);
378 data_parser_set_span (parser, false);
379 data_parser_set_quotes (parser, ss_empty ());
380 data_parser_set_empty_line_has_field (parser, true);
384 if (!lex_force_match (lexer, T_SLASH))
387 if (lex_match_id (lexer, "ENCODING"))
389 lex_match (lexer, T_EQUALS);
390 if (!lex_force_string (lexer))
394 encoding = ss_xstrdup (lex_tokss (lexer));
398 else if (lex_match_id (lexer, "ARRANGEMENT"))
402 lex_match (lexer, T_EQUALS);
403 if (lex_match_id (lexer, "FIXED"))
404 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
405 else if (lex_match_id (lexer, "DELIMITED"))
406 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
407 DP_DELIMITED, &has_type);
410 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
416 else if (lex_match_id (lexer, "FIRSTCASE"))
418 lex_match (lexer, T_EQUALS);
419 if (!lex_force_int (lexer))
421 if (lex_integer (lexer) < 1)
423 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
426 data_parser_set_skip (parser, lex_integer (lexer) - 1);
429 else if (lex_match_id_n (lexer, "DELCASE", 4))
431 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
433 lex_match (lexer, T_EQUALS);
434 if (lex_match_id (lexer, "LINE"))
435 data_parser_set_span (parser, false);
436 else if (lex_match_id (lexer, "VARIABLES"))
438 data_parser_set_span (parser, true);
440 /* VARIABLES takes an integer argument, but for no
441 good reason. We just ignore it. */
442 if (!lex_force_int (lexer))
448 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
452 else if (lex_match_id (lexer, "FIXCASE"))
454 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
456 lex_match (lexer, T_EQUALS);
457 if (!lex_force_int (lexer))
459 if (lex_integer (lexer) < 1)
461 msg (SE, _("Value of FIXCASE must be at least 1."));
464 data_parser_set_records (parser, lex_integer (lexer));
467 else if (lex_match_id (lexer, "IMPORTCASES"))
469 lex_match (lexer, T_EQUALS);
470 if (lex_match (lexer, T_ALL))
472 data_parser_set_case_limit (parser, -1);
473 data_parser_set_case_percent (parser, 100);
475 else if (lex_match_id (lexer, "FIRST"))
477 if (!lex_force_int (lexer))
479 if (lex_integer (lexer) < 1)
481 msg (SE, _("Value of FIRST must be at least 1."));
484 data_parser_set_case_limit (parser, lex_integer (lexer));
487 else if (lex_match_id (lexer, "PERCENT"))
489 if (!lex_force_int (lexer))
491 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
493 msg (SE, _("Value of PERCENT must be between 1 and 100."));
496 data_parser_set_case_percent (parser, lex_integer (lexer));
500 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
502 struct string hard_seps = DS_EMPTY_INITIALIZER;
503 const char *soft_seps = "";
507 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
509 lex_match (lexer, T_EQUALS);
511 if (!lex_force_string (lexer))
514 /* XXX should support multibyte UTF-8 characters */
515 s = lex_tokss (lexer);
516 if (ss_match_string (&s, ss_cstr ("\\t")))
517 ds_put_cstr (&hard_seps, "\t");
518 if (ss_match_string (&s, ss_cstr ("\\\\")))
519 ds_put_cstr (&hard_seps, "\\");
520 while ((c = ss_get_byte (&s)) != EOF)
524 ds_put_byte (&hard_seps, c);
525 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
526 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
527 ds_destroy (&hard_seps);
531 else if (lex_match_id (lexer, "QUALIFIERS"))
533 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
535 lex_match (lexer, T_EQUALS);
537 if (!lex_force_string (lexer))
540 /* XXX should support multibyte UTF-8 characters */
541 if (settings_get_syntax () == COMPATIBLE
542 && ss_length (lex_tokss (lexer)) != 1)
544 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
545 "must contain exactly one character."));
549 data_parser_set_quotes (parser, lex_tokss (lexer));
552 else if (settings_get_syntax () == ENHANCED
553 && lex_match_id (lexer, "ESCAPE"))
554 data_parser_set_quote_escape (parser, true);
555 else if (lex_match_id (lexer, "VARIABLES"))
559 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
563 lex_match (lexer, T_EQUALS);
566 type = data_parser_get_type (parser);
569 struct fmt_spec input, output;
573 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
575 if (!lex_force_int (lexer))
577 if (lex_integer (lexer) < record)
579 msg (SE, _("The record number specified, %ld, is at or "
580 "before the previous record, %d. Data "
581 "fields must be listed in order of "
582 "increasing record number."),
583 lex_integer (lexer), record);
586 if (lex_integer (lexer) > data_parser_get_records (parser))
588 msg (SE, _("The record number specified, %ld, exceeds "
589 "the number of records per case specified "
591 lex_integer (lexer), data_parser_get_records (parser));
594 record = lex_integer (lexer);
598 if (!lex_force_id (lexer)
599 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
601 name = xstrdup (lex_tokcstr (lexer));
604 if (type == DP_DELIMITED)
606 if (!parse_format_specifier (lexer, &input)
607 || !fmt_check_input (&input))
610 output = fmt_for_output_from_input (&input);
614 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
615 enum fmt_type fmt_type;
618 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
621 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
622 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
624 if (!fmt_from_name (fmt_type_name, &fmt_type))
626 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
630 /* Compose input format. */
631 input.type = fmt_type;
632 input.w = lc - fc + 1;
634 if (!fmt_check_input (&input))
637 /* Compose output format. */
640 output.type = fmt_type;
643 if (!fmt_check_output (&output))
647 output = fmt_for_output_from_input (&input);
650 v = dict_create_var (dict, name, fmt_var_width (&input));
653 msg (SE, _("%s is a duplicate variable name."), name);
656 var_set_both_formats (v, &output);
658 if (type == DP_DELIMITED)
659 data_parser_add_delimited_field (parser, &input,
660 var_get_case_index (v),
663 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
668 while (lex_token (lexer) != T_ENDCMD);
670 reader = dfm_open_reader (fh, lexer, encoding);
674 data_parser_make_active_file (parser, ds, reader, dict);
680 data_parser_destroy (parser);
685 return CMD_CASCADING_FAILURE;
690 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
692 free (opts->cell_range);
693 free (opts->sheet_name);