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 lex_force_match (lexer, T_SLASH);
63 if (!lex_force_match_id (lexer, "TYPE"))
66 lex_force_match (lexer, T_EQUALS);
68 tok = strdup (lex_tokcstr (lexer));
69 if (lex_match_id (lexer, "TXT"))
72 return parse_get_txt (lexer, ds);
74 else if (lex_match_id (lexer, "PSQL"))
77 return parse_get_psql (lexer, ds);
79 else if (lex_match_id (lexer, "GNM") ||
80 lex_match_id (lexer, "ODS"))
82 char *filename = NULL;
83 struct casereader *reader = NULL;
84 struct dictionary *dict = NULL;
85 struct spreadsheet_read_options opts;
86 if (!parse_spreadsheet (lexer, &filename, &opts))
89 if ( 0 == strncasecmp (tok, "GNM", 3))
91 struct spreadsheet *spreadsheet = gnumeric_probe (filename, true);
92 if (spreadsheet == NULL)
94 reader = gnumeric_make_reader (spreadsheet, &opts);
95 dict = spreadsheet->dict;
97 else if (0 == strncasecmp (tok, "ODS", 3))
99 struct spreadsheet *spreadsheet = ods_probe (filename, true);
100 if (spreadsheet == NULL)
102 reader = ods_make_reader (spreadsheet, &opts);
103 dict = spreadsheet->dict;
108 dataset_set_dict (ds, dict);
109 dataset_set_source (ds, reader);
111 destroy_spreadsheet_read_info (&opts);
114 destroy_spreadsheet_read_info (&opts);
117 msg (SE, _("Unsupported TYPE %s."), tok);
128 parse_get_psql (struct lexer *lexer, struct dataset *ds)
130 struct psql_read_info psql;
131 psql.allow_clear = false;
132 psql.conninfo = NULL;
135 ds_init_empty (&psql.sql);
137 lex_force_match (lexer, T_SLASH);
139 if (!lex_force_match_id (lexer, "CONNECT"))
142 lex_force_match (lexer, T_EQUALS);
144 if (!lex_force_string (lexer))
147 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
151 while (lex_match (lexer, T_SLASH) )
153 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
155 lex_match (lexer, T_EQUALS);
156 psql.str_width = lex_integer (lexer);
159 else if ( lex_match_id (lexer, "BSIZE"))
161 lex_match (lexer, T_EQUALS);
162 psql.bsize = lex_integer (lexer);
165 else if ( lex_match_id (lexer, "UNENCRYPTED"))
167 psql.allow_clear = true;
169 else if (lex_match_id (lexer, "SQL"))
171 lex_match (lexer, T_EQUALS);
172 if ( ! lex_force_string (lexer) )
175 ds_put_substring (&psql.sql, lex_tokss (lexer));
180 struct dictionary *dict = NULL;
181 struct casereader *reader = psql_open_reader (&psql, &dict);
185 dataset_set_dict (ds, dict);
186 dataset_set_source (ds, reader);
190 ds_destroy (&psql.sql);
191 free (psql.conninfo);
197 ds_destroy (&psql.sql);
198 free (psql.conninfo);
204 parse_spreadsheet (struct lexer *lexer, char **filename,
205 struct spreadsheet_read_options *opts)
207 opts->sheet_index = 1;
208 opts->sheet_name = NULL;
209 opts->cell_range = NULL;
210 opts->read_names = true;
213 lex_force_match (lexer, T_SLASH);
215 if (!lex_force_match_id (lexer, "FILE"))
218 lex_force_match (lexer, T_EQUALS);
220 if (!lex_force_string (lexer))
223 *filename = utf8_to_filename (lex_tokcstr (lexer));
227 while (lex_match (lexer, T_SLASH) )
229 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
231 lex_match (lexer, T_EQUALS);
232 opts->asw = lex_integer (lexer);
235 else if (lex_match_id (lexer, "SHEET"))
237 lex_match (lexer, T_EQUALS);
238 if (lex_match_id (lexer, "NAME"))
240 if ( ! lex_force_string (lexer) )
243 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
244 opts->sheet_index = -1;
248 else if (lex_match_id (lexer, "INDEX"))
250 opts->sheet_index = lex_integer (lexer);
251 if (opts->sheet_index <= 0)
253 msg (SE, _("The sheet index must be greater than or equal to 1"));
260 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
261 "/SHEET", "NAME", "INDEX");
265 else if (lex_match_id (lexer, "CELLRANGE"))
267 lex_match (lexer, T_EQUALS);
269 if (lex_match_id (lexer, "FULL"))
271 opts->cell_range = NULL;
273 else if (lex_match_id (lexer, "RANGE"))
275 if ( ! lex_force_string (lexer) )
278 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
283 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
284 "/CELLRANGE", "FULL", "RANGE");
288 else if (lex_match_id (lexer, "READNAMES"))
290 lex_match (lexer, T_EQUALS);
292 if ( lex_match_id (lexer, "ON"))
294 opts->read_names = true;
296 else if (lex_match_id (lexer, "OFF"))
298 opts->read_names = false;
302 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
303 "/READNAMES", "ON", "OFF");
309 lex_error (lexer, NULL);
322 set_type (struct data_parser *parser, const char *subcommand,
323 enum data_parser_type type, bool *has_type)
327 data_parser_set_type (parser, type);
330 else if (type != data_parser_get_type (parser))
332 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
333 "was stated or implied earlier in this command."),
335 type == DP_FIXED ? "FIXED" : "DELIMITED",
336 type == DP_FIXED ? "DELIMITED" : "FIXED");
343 parse_get_txt (struct lexer *lexer, struct dataset *ds)
345 struct data_parser *parser = NULL;
346 struct dictionary *dict = dict_create (get_default_encoding ());
347 struct file_handle *fh = NULL;
348 struct dfm_reader *reader = NULL;
349 char *encoding = NULL;
353 enum data_parser_type type;
356 lex_force_match (lexer, T_SLASH);
358 if (!lex_force_match_id (lexer, "FILE"))
360 lex_force_match (lexer, T_EQUALS);
361 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
365 parser = data_parser_create (dict);
367 data_parser_set_type (parser, DP_DELIMITED);
368 data_parser_set_span (parser, false);
369 data_parser_set_quotes (parser, ss_empty ());
370 data_parser_set_empty_line_has_field (parser, true);
374 if (!lex_force_match (lexer, T_SLASH))
377 if (lex_match_id (lexer, "ENCODING"))
379 lex_match (lexer, T_EQUALS);
380 if (!lex_force_string (lexer))
384 encoding = ss_xstrdup (lex_tokss (lexer));
388 else if (lex_match_id (lexer, "ARRANGEMENT"))
392 lex_match (lexer, T_EQUALS);
393 if (lex_match_id (lexer, "FIXED"))
394 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
395 else if (lex_match_id (lexer, "DELIMITED"))
396 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
397 DP_DELIMITED, &has_type);
400 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
406 else if (lex_match_id (lexer, "FIRSTCASE"))
408 lex_match (lexer, T_EQUALS);
409 if (!lex_force_int (lexer))
411 if (lex_integer (lexer) < 1)
413 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
416 data_parser_set_skip (parser, lex_integer (lexer) - 1);
419 else if (lex_match_id_n (lexer, "DELCASE", 4))
421 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
423 lex_match (lexer, T_EQUALS);
424 if (lex_match_id (lexer, "LINE"))
425 data_parser_set_span (parser, false);
426 else if (lex_match_id (lexer, "VARIABLES"))
428 data_parser_set_span (parser, true);
430 /* VARIABLES takes an integer argument, but for no
431 good reason. We just ignore it. */
432 if (!lex_force_int (lexer))
438 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
442 else if (lex_match_id (lexer, "FIXCASE"))
444 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
446 lex_match (lexer, T_EQUALS);
447 if (!lex_force_int (lexer))
449 if (lex_integer (lexer) < 1)
451 msg (SE, _("Value of FIXCASE must be at least 1."));
454 data_parser_set_records (parser, lex_integer (lexer));
457 else if (lex_match_id (lexer, "IMPORTCASES"))
459 lex_match (lexer, T_EQUALS);
460 if (lex_match (lexer, T_ALL))
462 data_parser_set_case_limit (parser, -1);
463 data_parser_set_case_percent (parser, 100);
465 else if (lex_match_id (lexer, "FIRST"))
467 if (!lex_force_int (lexer))
469 if (lex_integer (lexer) < 1)
471 msg (SE, _("Value of FIRST must be at least 1."));
474 data_parser_set_case_limit (parser, lex_integer (lexer));
477 else if (lex_match_id (lexer, "PERCENT"))
479 if (!lex_force_int (lexer))
481 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
483 msg (SE, _("Value of PERCENT must be between 1 and 100."));
486 data_parser_set_case_percent (parser, lex_integer (lexer));
490 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
492 struct string hard_seps = DS_EMPTY_INITIALIZER;
493 const char *soft_seps = "";
497 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
499 lex_match (lexer, T_EQUALS);
501 if (!lex_force_string (lexer))
504 /* XXX should support multibyte UTF-8 characters */
505 s = lex_tokss (lexer);
506 if (ss_match_string (&s, ss_cstr ("\\t")))
507 ds_put_cstr (&hard_seps, "\t");
508 if (ss_match_string (&s, ss_cstr ("\\\\")))
509 ds_put_cstr (&hard_seps, "\\");
510 while ((c = ss_get_byte (&s)) != EOF)
514 ds_put_byte (&hard_seps, c);
515 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
516 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
517 ds_destroy (&hard_seps);
521 else if (lex_match_id (lexer, "QUALIFIERS"))
523 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
525 lex_match (lexer, T_EQUALS);
527 if (!lex_force_string (lexer))
530 /* XXX should support multibyte UTF-8 characters */
531 if (settings_get_syntax () == COMPATIBLE
532 && ss_length (lex_tokss (lexer)) != 1)
534 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
535 "must contain exactly one character."));
539 data_parser_set_quotes (parser, lex_tokss (lexer));
542 else if (settings_get_syntax () == ENHANCED
543 && lex_match_id (lexer, "ESCAPE"))
544 data_parser_set_quote_escape (parser, true);
545 else if (lex_match_id (lexer, "VARIABLES"))
549 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
553 lex_match (lexer, T_EQUALS);
556 type = data_parser_get_type (parser);
559 struct fmt_spec input, output;
563 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
565 if (!lex_force_int (lexer))
567 if (lex_integer (lexer) < record)
569 msg (SE, _("The record number specified, %ld, is at or "
570 "before the previous record, %d. Data "
571 "fields must be listed in order of "
572 "increasing record number."),
573 lex_integer (lexer), record);
576 if (lex_integer (lexer) > data_parser_get_records (parser))
578 msg (SE, _("The record number specified, %ld, exceeds "
579 "the number of records per case specified "
581 lex_integer (lexer), data_parser_get_records (parser));
584 record = lex_integer (lexer);
588 if (!lex_force_id (lexer)
589 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
591 name = xstrdup (lex_tokcstr (lexer));
594 if (type == DP_DELIMITED)
596 if (!parse_format_specifier (lexer, &input)
597 || !fmt_check_input (&input))
600 output = fmt_for_output_from_input (&input);
604 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
605 enum fmt_type fmt_type;
608 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
611 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
612 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
614 if (!fmt_from_name (fmt_type_name, &fmt_type))
616 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
620 /* Compose input format. */
621 input.type = fmt_type;
622 input.w = lc - fc + 1;
624 if (!fmt_check_input (&input))
627 /* Compose output format. */
630 output.type = fmt_type;
633 if (!fmt_check_output (&output))
637 output = fmt_for_output_from_input (&input);
640 v = dict_create_var (dict, name, fmt_var_width (&input));
643 msg (SE, _("%s is a duplicate variable name."), name);
646 var_set_both_formats (v, &output);
648 if (type == DP_DELIMITED)
649 data_parser_add_delimited_field (parser, &input,
650 var_get_case_index (v),
653 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
658 while (lex_token (lexer) != T_ENDCMD);
660 reader = dfm_open_reader (fh, lexer, encoding);
664 data_parser_make_active_file (parser, ds, reader, dict);
670 data_parser_destroy (parser);
675 return CMD_CASCADING_FAILURE;
680 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
682 free (opts->cell_range);