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_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);
115 msg (SE, _("Unsupported TYPE %s."), tok);
123 parse_get_psql (struct lexer *lexer, struct dataset *ds)
125 struct psql_read_info psql;
126 psql.allow_clear = false;
127 psql.conninfo = NULL;
130 ds_init_empty (&psql.sql);
132 lex_force_match (lexer, T_SLASH);
134 if (!lex_force_match_id (lexer, "CONNECT"))
137 lex_force_match (lexer, T_EQUALS);
139 if (!lex_force_string (lexer))
142 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
146 while (lex_match (lexer, T_SLASH) )
148 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
150 lex_match (lexer, T_EQUALS);
151 psql.str_width = lex_integer (lexer);
154 else if ( lex_match_id (lexer, "BSIZE"))
156 lex_match (lexer, T_EQUALS);
157 psql.bsize = lex_integer (lexer);
160 else if ( lex_match_id (lexer, "UNENCRYPTED"))
162 psql.allow_clear = true;
164 else if (lex_match_id (lexer, "SQL"))
166 lex_match (lexer, T_EQUALS);
167 if ( ! lex_force_string (lexer) )
170 ds_put_substring (&psql.sql, lex_tokss (lexer));
175 struct dictionary *dict = NULL;
176 struct casereader *reader = psql_open_reader (&psql, &dict);
180 dataset_set_dict (ds, dict);
181 dataset_set_source (ds, reader);
185 ds_destroy (&psql.sql);
186 free (psql.conninfo);
192 ds_destroy (&psql.sql);
193 free (psql.conninfo);
199 parse_spreadsheet (struct lexer *lexer, char **filename,
200 struct spreadsheet_read_options *opts)
202 opts->sheet_index = 1;
203 opts->sheet_name = NULL;
204 opts->cell_range = NULL;
205 opts->read_names = true;
208 lex_force_match (lexer, T_SLASH);
210 if (!lex_force_match_id (lexer, "FILE"))
213 lex_force_match (lexer, T_EQUALS);
215 if (!lex_force_string (lexer))
218 *filename = utf8_to_filename (lex_tokcstr (lexer));
222 while (lex_match (lexer, T_SLASH) )
224 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
226 lex_match (lexer, T_EQUALS);
227 opts->asw = lex_integer (lexer);
230 else if (lex_match_id (lexer, "SHEET"))
232 lex_match (lexer, T_EQUALS);
233 if (lex_match_id (lexer, "NAME"))
235 if ( ! lex_force_string (lexer) )
238 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
239 opts->sheet_index = -1;
243 else if (lex_match_id (lexer, "INDEX"))
245 opts->sheet_index = lex_integer (lexer);
246 if (opts->sheet_index <= 0)
248 msg (SE, _("The sheet index must be greater than or equal to 1"));
255 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
256 "/SHEET", "NAME", "INDEX");
260 else if (lex_match_id (lexer, "CELLRANGE"))
262 lex_match (lexer, T_EQUALS);
264 if (lex_match_id (lexer, "FULL"))
266 opts->cell_range = NULL;
268 else if (lex_match_id (lexer, "RANGE"))
270 if ( ! lex_force_string (lexer) )
273 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
278 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
279 "/CELLRANGE", "FULL", "RANGE");
283 else if (lex_match_id (lexer, "READNAMES"))
285 lex_match (lexer, T_EQUALS);
287 if ( lex_match_id (lexer, "ON"))
289 opts->read_names = true;
291 else if (lex_match_id (lexer, "OFF"))
293 opts->read_names = false;
297 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
298 "/READNAMES", "ON", "OFF");
304 lex_error (lexer, NULL);
317 set_type (struct data_parser *parser, const char *subcommand,
318 enum data_parser_type type, bool *has_type)
322 data_parser_set_type (parser, type);
325 else if (type != data_parser_get_type (parser))
327 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
328 "was stated or implied earlier in this command."),
330 type == DP_FIXED ? "FIXED" : "DELIMITED",
331 type == DP_FIXED ? "DELIMITED" : "FIXED");
338 parse_get_txt (struct lexer *lexer, struct dataset *ds)
340 struct data_parser *parser = NULL;
341 struct dictionary *dict = dict_create (get_default_encoding ());
342 struct file_handle *fh = NULL;
343 struct dfm_reader *reader = NULL;
344 char *encoding = NULL;
348 enum data_parser_type type;
351 lex_force_match (lexer, T_SLASH);
353 if (!lex_force_match_id (lexer, "FILE"))
355 lex_force_match (lexer, T_EQUALS);
356 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
360 parser = data_parser_create (dict);
362 data_parser_set_type (parser, DP_DELIMITED);
363 data_parser_set_span (parser, false);
364 data_parser_set_quotes (parser, ss_empty ());
365 data_parser_set_empty_line_has_field (parser, true);
369 if (!lex_force_match (lexer, T_SLASH))
372 if (lex_match_id (lexer, "ENCODING"))
374 lex_match (lexer, T_EQUALS);
375 if (!lex_force_string (lexer))
379 encoding = ss_xstrdup (lex_tokss (lexer));
383 else if (lex_match_id (lexer, "ARRANGEMENT"))
387 lex_match (lexer, T_EQUALS);
388 if (lex_match_id (lexer, "FIXED"))
389 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
390 else if (lex_match_id (lexer, "DELIMITED"))
391 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
392 DP_DELIMITED, &has_type);
395 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
401 else if (lex_match_id (lexer, "FIRSTCASE"))
403 lex_match (lexer, T_EQUALS);
404 if (!lex_force_int (lexer))
406 if (lex_integer (lexer) < 1)
408 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
411 data_parser_set_skip (parser, lex_integer (lexer) - 1);
414 else if (lex_match_id_n (lexer, "DELCASE", 4))
416 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
418 lex_match (lexer, T_EQUALS);
419 if (lex_match_id (lexer, "LINE"))
420 data_parser_set_span (parser, false);
421 else if (lex_match_id (lexer, "VARIABLES"))
423 data_parser_set_span (parser, true);
425 /* VARIABLES takes an integer argument, but for no
426 good reason. We just ignore it. */
427 if (!lex_force_int (lexer))
433 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
437 else if (lex_match_id (lexer, "FIXCASE"))
439 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
441 lex_match (lexer, T_EQUALS);
442 if (!lex_force_int (lexer))
444 if (lex_integer (lexer) < 1)
446 msg (SE, _("Value of FIXCASE must be at least 1."));
449 data_parser_set_records (parser, lex_integer (lexer));
452 else if (lex_match_id (lexer, "IMPORTCASES"))
454 lex_match (lexer, T_EQUALS);
455 if (lex_match (lexer, T_ALL))
457 data_parser_set_case_limit (parser, -1);
458 data_parser_set_case_percent (parser, 100);
460 else if (lex_match_id (lexer, "FIRST"))
462 if (!lex_force_int (lexer))
464 if (lex_integer (lexer) < 1)
466 msg (SE, _("Value of FIRST must be at least 1."));
469 data_parser_set_case_limit (parser, lex_integer (lexer));
472 else if (lex_match_id (lexer, "PERCENT"))
474 if (!lex_force_int (lexer))
476 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
478 msg (SE, _("Value of PERCENT must be between 1 and 100."));
481 data_parser_set_case_percent (parser, lex_integer (lexer));
485 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
487 struct string hard_seps = DS_EMPTY_INITIALIZER;
488 const char *soft_seps = "";
492 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
494 lex_match (lexer, T_EQUALS);
496 if (!lex_force_string (lexer))
499 /* XXX should support multibyte UTF-8 characters */
500 s = lex_tokss (lexer);
501 if (ss_match_string (&s, ss_cstr ("\\t")))
502 ds_put_cstr (&hard_seps, "\t");
503 if (ss_match_string (&s, ss_cstr ("\\\\")))
504 ds_put_cstr (&hard_seps, "\\");
505 while ((c = ss_get_byte (&s)) != EOF)
509 ds_put_byte (&hard_seps, c);
510 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
511 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
512 ds_destroy (&hard_seps);
516 else if (lex_match_id (lexer, "QUALIFIERS"))
518 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
520 lex_match (lexer, T_EQUALS);
522 if (!lex_force_string (lexer))
525 /* XXX should support multibyte UTF-8 characters */
526 if (settings_get_syntax () == COMPATIBLE
527 && ss_length (lex_tokss (lexer)) != 1)
529 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
530 "must contain exactly one character."));
534 data_parser_set_quotes (parser, lex_tokss (lexer));
537 else if (settings_get_syntax () == ENHANCED
538 && lex_match_id (lexer, "ESCAPE"))
539 data_parser_set_quote_escape (parser, true);
540 else if (lex_match_id (lexer, "VARIABLES"))
544 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
548 lex_match (lexer, T_EQUALS);
551 type = data_parser_get_type (parser);
554 struct fmt_spec input, output;
558 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
560 if (!lex_force_int (lexer))
562 if (lex_integer (lexer) < record)
564 msg (SE, _("The record number specified, %ld, is at or "
565 "before the previous record, %d. Data "
566 "fields must be listed in order of "
567 "increasing record number."),
568 lex_integer (lexer), record);
571 if (lex_integer (lexer) > data_parser_get_records (parser))
573 msg (SE, _("The record number specified, %ld, exceeds "
574 "the number of records per case specified "
576 lex_integer (lexer), data_parser_get_records (parser));
579 record = lex_integer (lexer);
583 if (!lex_force_id (lexer)
584 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
586 name = xstrdup (lex_tokcstr (lexer));
589 if (type == DP_DELIMITED)
591 if (!parse_format_specifier (lexer, &input)
592 || !fmt_check_input (&input))
595 output = fmt_for_output_from_input (&input);
599 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
600 enum fmt_type fmt_type;
603 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
606 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
607 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
609 if (!fmt_from_name (fmt_type_name, &fmt_type))
611 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
615 /* Compose input format. */
616 input.type = fmt_type;
617 input.w = lc - fc + 1;
619 if (!fmt_check_input (&input))
622 /* Compose output format. */
625 output.type = fmt_type;
628 if (!fmt_check_output (&output))
632 output = fmt_for_output_from_input (&input);
635 v = dict_create_var (dict, name, fmt_var_width (&input));
638 msg (SE, _("%s is a duplicate variable name."), name);
641 var_set_both_formats (v, &output);
643 if (type == DP_DELIMITED)
644 data_parser_add_delimited_field (parser, &input,
645 var_get_case_index (v),
648 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
653 while (lex_token (lexer) != T_ENDCMD);
655 reader = dfm_open_reader (fh, lexer, encoding);
659 data_parser_make_active_file (parser, ds, reader, dict);
665 data_parser_destroy (parser);
670 return CMD_CASCADING_FAILURE;
675 destroy_spreadsheet_read_info (UNUSED struct spreadsheet_read_info *sri,
676 UNUSED struct spreadsheet_read_options *opts)