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)
60 struct spreadsheet_read_options opts;
62 lex_force_match (lexer, T_SLASH);
64 if (!lex_force_match_id (lexer, "TYPE"))
67 lex_force_match (lexer, T_EQUALS);
69 tok = strdup (lex_tokcstr (lexer));
70 if (lex_match_id (lexer, "TXT"))
73 return parse_get_txt (lexer, ds);
75 else if (lex_match_id (lexer, "PSQL"))
78 return parse_get_psql (lexer, ds);
80 else if (lex_match_id (lexer, "GNM") ||
81 lex_match_id (lexer, "ODS"))
83 char *filename = NULL;
84 struct casereader *reader = NULL;
85 struct dictionary *dict = NULL;
87 if (!parse_spreadsheet (lexer, &filename, &opts))
90 if ( 0 == strncasecmp (tok, "GNM", 3))
92 struct spreadsheet *spreadsheet = gnumeric_probe (filename, true);
93 if (spreadsheet == NULL)
95 reader = gnumeric_make_reader (spreadsheet, &opts);
96 dict = spreadsheet->dict;
97 gnumeric_destroy (spreadsheet);
99 else if (0 == strncasecmp (tok, "ODS", 3))
101 struct spreadsheet *spreadsheet = ods_probe (filename, true);
102 if (spreadsheet == NULL)
104 reader = ods_make_reader (spreadsheet, &opts);
105 dict = spreadsheet->dict;
106 ods_destroy (spreadsheet);
113 dataset_set_dict (ds, dict);
114 dataset_set_source (ds, reader);
116 destroy_spreadsheet_read_info (&opts);
121 msg (SE, _("Unsupported TYPE %s."), tok);
125 destroy_spreadsheet_read_info (&opts);
131 parse_get_psql (struct lexer *lexer, struct dataset *ds)
133 struct psql_read_info psql;
134 psql.allow_clear = false;
135 psql.conninfo = NULL;
138 ds_init_empty (&psql.sql);
140 lex_force_match (lexer, T_SLASH);
142 if (!lex_force_match_id (lexer, "CONNECT"))
145 lex_force_match (lexer, T_EQUALS);
147 if (!lex_force_string (lexer))
150 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
154 while (lex_match (lexer, T_SLASH) )
156 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
158 lex_match (lexer, T_EQUALS);
159 psql.str_width = lex_integer (lexer);
162 else if ( lex_match_id (lexer, "BSIZE"))
164 lex_match (lexer, T_EQUALS);
165 psql.bsize = lex_integer (lexer);
168 else if ( lex_match_id (lexer, "UNENCRYPTED"))
170 psql.allow_clear = true;
172 else if (lex_match_id (lexer, "SQL"))
174 lex_match (lexer, T_EQUALS);
175 if ( ! lex_force_string (lexer) )
178 ds_put_substring (&psql.sql, lex_tokss (lexer));
183 struct dictionary *dict = NULL;
184 struct casereader *reader = psql_open_reader (&psql, &dict);
188 dataset_set_dict (ds, dict);
189 dataset_set_source (ds, reader);
193 ds_destroy (&psql.sql);
194 free (psql.conninfo);
200 ds_destroy (&psql.sql);
201 free (psql.conninfo);
207 parse_spreadsheet (struct lexer *lexer, char **filename,
208 struct spreadsheet_read_options *opts)
210 opts->sheet_index = 1;
211 opts->sheet_name = NULL;
212 opts->cell_range = NULL;
213 opts->read_names = true;
216 lex_force_match (lexer, T_SLASH);
218 if (!lex_force_match_id (lexer, "FILE"))
221 lex_force_match (lexer, T_EQUALS);
223 if (!lex_force_string (lexer))
226 *filename = utf8_to_filename (lex_tokcstr (lexer));
230 while (lex_match (lexer, T_SLASH) )
232 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
234 lex_match (lexer, T_EQUALS);
235 opts->asw = lex_integer (lexer);
238 else if (lex_match_id (lexer, "SHEET"))
240 lex_match (lexer, T_EQUALS);
241 if (lex_match_id (lexer, "NAME"))
243 if ( ! lex_force_string (lexer) )
246 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
247 opts->sheet_index = -1;
251 else if (lex_match_id (lexer, "INDEX"))
253 opts->sheet_index = lex_integer (lexer);
254 if (opts->sheet_index <= 0)
256 msg (SE, _("The sheet index must be greater than or equal to 1"));
263 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
264 "/SHEET", "NAME", "INDEX");
268 else if (lex_match_id (lexer, "CELLRANGE"))
270 lex_match (lexer, T_EQUALS);
272 if (lex_match_id (lexer, "FULL"))
274 opts->cell_range = NULL;
276 else if (lex_match_id (lexer, "RANGE"))
278 if ( ! lex_force_string (lexer) )
281 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
286 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
287 "/CELLRANGE", "FULL", "RANGE");
291 else if (lex_match_id (lexer, "READNAMES"))
293 lex_match (lexer, T_EQUALS);
295 if ( lex_match_id (lexer, "ON"))
297 opts->read_names = true;
299 else if (lex_match_id (lexer, "OFF"))
301 opts->read_names = false;
305 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
306 "/READNAMES", "ON", "OFF");
312 lex_error (lexer, NULL);
325 set_type (struct data_parser *parser, const char *subcommand,
326 enum data_parser_type type, bool *has_type)
330 data_parser_set_type (parser, type);
333 else if (type != data_parser_get_type (parser))
335 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
336 "was stated or implied earlier in this command."),
338 type == DP_FIXED ? "FIXED" : "DELIMITED",
339 type == DP_FIXED ? "DELIMITED" : "FIXED");
346 parse_get_txt (struct lexer *lexer, struct dataset *ds)
348 struct data_parser *parser = NULL;
349 struct dictionary *dict = dict_create (get_default_encoding ());
350 struct file_handle *fh = NULL;
351 struct dfm_reader *reader = NULL;
352 char *encoding = NULL;
356 enum data_parser_type type;
359 lex_force_match (lexer, T_SLASH);
361 if (!lex_force_match_id (lexer, "FILE"))
363 lex_force_match (lexer, T_EQUALS);
364 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
368 parser = data_parser_create (dict);
370 data_parser_set_type (parser, DP_DELIMITED);
371 data_parser_set_span (parser, false);
372 data_parser_set_quotes (parser, ss_empty ());
373 data_parser_set_empty_line_has_field (parser, true);
377 if (!lex_force_match (lexer, T_SLASH))
380 if (lex_match_id (lexer, "ENCODING"))
382 lex_match (lexer, T_EQUALS);
383 if (!lex_force_string (lexer))
387 encoding = ss_xstrdup (lex_tokss (lexer));
391 else if (lex_match_id (lexer, "ARRANGEMENT"))
395 lex_match (lexer, T_EQUALS);
396 if (lex_match_id (lexer, "FIXED"))
397 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
398 else if (lex_match_id (lexer, "DELIMITED"))
399 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
400 DP_DELIMITED, &has_type);
403 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
409 else if (lex_match_id (lexer, "FIRSTCASE"))
411 lex_match (lexer, T_EQUALS);
412 if (!lex_force_int (lexer))
414 if (lex_integer (lexer) < 1)
416 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
419 data_parser_set_skip (parser, lex_integer (lexer) - 1);
422 else if (lex_match_id_n (lexer, "DELCASE", 4))
424 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
426 lex_match (lexer, T_EQUALS);
427 if (lex_match_id (lexer, "LINE"))
428 data_parser_set_span (parser, false);
429 else if (lex_match_id (lexer, "VARIABLES"))
431 data_parser_set_span (parser, true);
433 /* VARIABLES takes an integer argument, but for no
434 good reason. We just ignore it. */
435 if (!lex_force_int (lexer))
441 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
445 else if (lex_match_id (lexer, "FIXCASE"))
447 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
449 lex_match (lexer, T_EQUALS);
450 if (!lex_force_int (lexer))
452 if (lex_integer (lexer) < 1)
454 msg (SE, _("Value of FIXCASE must be at least 1."));
457 data_parser_set_records (parser, lex_integer (lexer));
460 else if (lex_match_id (lexer, "IMPORTCASES"))
462 lex_match (lexer, T_EQUALS);
463 if (lex_match (lexer, T_ALL))
465 data_parser_set_case_limit (parser, -1);
466 data_parser_set_case_percent (parser, 100);
468 else if (lex_match_id (lexer, "FIRST"))
470 if (!lex_force_int (lexer))
472 if (lex_integer (lexer) < 1)
474 msg (SE, _("Value of FIRST must be at least 1."));
477 data_parser_set_case_limit (parser, lex_integer (lexer));
480 else if (lex_match_id (lexer, "PERCENT"))
482 if (!lex_force_int (lexer))
484 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
486 msg (SE, _("Value of PERCENT must be between 1 and 100."));
489 data_parser_set_case_percent (parser, lex_integer (lexer));
493 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
495 struct string hard_seps = DS_EMPTY_INITIALIZER;
496 const char *soft_seps = "";
500 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
502 lex_match (lexer, T_EQUALS);
504 if (!lex_force_string (lexer))
507 /* XXX should support multibyte UTF-8 characters */
508 s = lex_tokss (lexer);
509 if (ss_match_string (&s, ss_cstr ("\\t")))
510 ds_put_cstr (&hard_seps, "\t");
511 if (ss_match_string (&s, ss_cstr ("\\\\")))
512 ds_put_cstr (&hard_seps, "\\");
513 while ((c = ss_get_byte (&s)) != EOF)
517 ds_put_byte (&hard_seps, c);
518 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
519 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
520 ds_destroy (&hard_seps);
524 else if (lex_match_id (lexer, "QUALIFIERS"))
526 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
528 lex_match (lexer, T_EQUALS);
530 if (!lex_force_string (lexer))
533 /* XXX should support multibyte UTF-8 characters */
534 if (settings_get_syntax () == COMPATIBLE
535 && ss_length (lex_tokss (lexer)) != 1)
537 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
538 "must contain exactly one character."));
542 data_parser_set_quotes (parser, lex_tokss (lexer));
545 else if (settings_get_syntax () == ENHANCED
546 && lex_match_id (lexer, "ESCAPE"))
547 data_parser_set_quote_escape (parser, true);
548 else if (lex_match_id (lexer, "VARIABLES"))
552 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
556 lex_match (lexer, T_EQUALS);
559 type = data_parser_get_type (parser);
562 struct fmt_spec input, output;
566 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
568 if (!lex_force_int (lexer))
570 if (lex_integer (lexer) < record)
572 msg (SE, _("The record number specified, %ld, is at or "
573 "before the previous record, %d. Data "
574 "fields must be listed in order of "
575 "increasing record number."),
576 lex_integer (lexer), record);
579 if (lex_integer (lexer) > data_parser_get_records (parser))
581 msg (SE, _("The record number specified, %ld, exceeds "
582 "the number of records per case specified "
584 lex_integer (lexer), data_parser_get_records (parser));
587 record = lex_integer (lexer);
591 if (!lex_force_id (lexer)
592 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
594 name = xstrdup (lex_tokcstr (lexer));
597 if (type == DP_DELIMITED)
599 if (!parse_format_specifier (lexer, &input)
600 || !fmt_check_input (&input))
603 output = fmt_for_output_from_input (&input);
607 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
608 enum fmt_type fmt_type;
611 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
614 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
615 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
617 if (!fmt_from_name (fmt_type_name, &fmt_type))
619 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
623 /* Compose input format. */
624 input.type = fmt_type;
625 input.w = lc - fc + 1;
627 if (!fmt_check_input (&input))
630 /* Compose output format. */
633 output.type = fmt_type;
636 if (!fmt_check_output (&output))
640 output = fmt_for_output_from_input (&input);
643 v = dict_create_var (dict, name, fmt_var_width (&input));
646 msg (SE, _("%s is a duplicate variable name."), name);
649 var_set_both_formats (v, &output);
651 if (type == DP_DELIMITED)
652 data_parser_add_delimited_field (parser, &input,
653 var_get_case_index (v),
656 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
661 while (lex_token (lexer) != T_ENDCMD);
663 reader = dfm_open_reader (fh, lexer, encoding);
667 data_parser_make_active_file (parser, ds, reader, dict);
673 data_parser_destroy (parser);
678 return CMD_CASCADING_FAILURE;
683 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
685 free (opts->cell_range);
686 free (opts->sheet_name);