1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 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 struct spreadsheet_read_info *parse_spreadsheet (struct lexer *lexer);
49 static void destroy_spreadsheet_read_info (struct spreadsheet_read_info *);
51 static int parse_get_txt (struct lexer *lexer, struct dataset *);
52 static int parse_get_psql (struct lexer *lexer, struct dataset *);
55 cmd_get_data (struct lexer *lexer, struct dataset *ds)
58 lex_force_match (lexer, T_SLASH);
60 if (!lex_force_match_id (lexer, "TYPE"))
63 lex_force_match (lexer, T_EQUALS);
65 tok = strdup (lex_tokcstr (lexer));
66 if (lex_match_id (lexer, "TXT"))
69 return parse_get_txt (lexer, ds);
71 else if (lex_match_id (lexer, "PSQL"))
74 return parse_get_psql (lexer, ds);
76 else if (lex_match_id (lexer, "GNM") ||
77 lex_match_id (lexer, "ODS"))
79 struct casereader *reader = NULL;
80 struct dictionary *dict = NULL;
81 struct spreadsheet_read_info *sri = parse_spreadsheet (lexer);
85 if ( 0 == strncasecmp (tok, "GNM", 3))
86 reader = gnumeric_open_reader (sri, &dict);
87 else if (0 == strncasecmp (tok, "ODS", 3))
88 reader = ods_open_reader (sri, &dict);
92 dataset_set_dict (ds, dict);
93 dataset_set_source (ds, reader);
94 destroy_spreadsheet_read_info (sri);
98 destroy_spreadsheet_read_info (sri);
101 msg (SE, _("Unsupported TYPE %s."), tok);
109 parse_get_psql (struct lexer *lexer, struct dataset *ds)
111 struct psql_read_info psql;
112 psql.allow_clear = false;
113 psql.conninfo = NULL;
116 ds_init_empty (&psql.sql);
118 lex_force_match (lexer, T_SLASH);
120 if (!lex_force_match_id (lexer, "CONNECT"))
123 lex_force_match (lexer, T_EQUALS);
125 if (!lex_force_string (lexer))
128 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
132 while (lex_match (lexer, T_SLASH) )
134 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
136 lex_match (lexer, T_EQUALS);
137 psql.str_width = lex_integer (lexer);
140 else if ( lex_match_id (lexer, "BSIZE"))
142 lex_match (lexer, T_EQUALS);
143 psql.bsize = lex_integer (lexer);
146 else if ( lex_match_id (lexer, "UNENCRYPTED"))
148 psql.allow_clear = true;
150 else if (lex_match_id (lexer, "SQL"))
152 lex_match (lexer, T_EQUALS);
153 if ( ! lex_force_string (lexer) )
156 ds_put_substring (&psql.sql, lex_tokss (lexer));
161 struct dictionary *dict = NULL;
162 struct casereader *reader = psql_open_reader (&psql, &dict);
166 dataset_set_dict (ds, dict);
167 dataset_set_source (ds, reader);
171 ds_destroy (&psql.sql);
172 free (psql.conninfo);
178 ds_destroy (&psql.sql);
179 free (psql.conninfo);
184 static struct spreadsheet_read_info *
185 parse_spreadsheet (struct lexer *lexer)
187 struct spreadsheet_read_info *sri = xzalloc (sizeof *sri);
188 sri->sheet_index = 1;
189 sri->read_names = true;
192 lex_force_match (lexer, T_SLASH);
194 if (!lex_force_match_id (lexer, "FILE"))
197 lex_force_match (lexer, T_EQUALS);
199 if (!lex_force_string (lexer))
202 sri->file_name = utf8_to_filename (lex_tokcstr (lexer));
206 while (lex_match (lexer, T_SLASH) )
208 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
210 lex_match (lexer, T_EQUALS);
211 sri->asw = lex_integer (lexer);
214 else if (lex_match_id (lexer, "SHEET"))
216 lex_match (lexer, T_EQUALS);
217 if (lex_match_id (lexer, "NAME"))
219 if ( ! lex_force_string (lexer) )
222 sri->sheet_name = ss_xstrdup (lex_tokss (lexer));
223 sri->sheet_index = -1;
227 else if (lex_match_id (lexer, "INDEX"))
229 sri->sheet_index = lex_integer (lexer);
235 else if (lex_match_id (lexer, "CELLRANGE"))
237 lex_match (lexer, T_EQUALS);
239 if (lex_match_id (lexer, "FULL"))
241 sri->cell_range = NULL;
243 else if (lex_match_id (lexer, "RANGE"))
245 if ( ! lex_force_string (lexer) )
248 sri->cell_range = ss_xstrdup (lex_tokss (lexer));
254 else if (lex_match_id (lexer, "READNAMES"))
256 lex_match (lexer, T_EQUALS);
258 if ( lex_match_id (lexer, "ON"))
260 sri->read_names = true;
262 else if (lex_match_id (lexer, "OFF"))
264 sri->read_names = false;
271 lex_error (lexer, NULL);
279 destroy_spreadsheet_read_info (sri);
285 set_type (struct data_parser *parser, const char *subcommand,
286 enum data_parser_type type, bool *has_type)
290 data_parser_set_type (parser, type);
293 else if (type != data_parser_get_type (parser))
295 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
296 "was stated or implied earlier in this command."),
298 type == DP_FIXED ? "FIXED" : "DELIMITED",
299 type == DP_FIXED ? "DELIMITED" : "FIXED");
306 parse_get_txt (struct lexer *lexer, struct dataset *ds)
308 struct data_parser *parser = NULL;
309 struct dictionary *dict = dict_create (get_default_encoding ());
310 struct file_handle *fh = NULL;
311 struct dfm_reader *reader = NULL;
312 char *encoding = NULL;
316 enum data_parser_type type;
319 lex_force_match (lexer, T_SLASH);
321 if (!lex_force_match_id (lexer, "FILE"))
323 lex_force_match (lexer, T_EQUALS);
324 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
328 parser = data_parser_create (dict);
330 data_parser_set_type (parser, DP_DELIMITED);
331 data_parser_set_span (parser, false);
332 data_parser_set_quotes (parser, ss_empty ());
333 data_parser_set_empty_line_has_field (parser, true);
337 if (!lex_force_match (lexer, T_SLASH))
340 if (lex_match_id (lexer, "ENCODING"))
342 lex_match (lexer, T_EQUALS);
343 if (!lex_force_string (lexer))
347 encoding = ss_xstrdup (lex_tokss (lexer));
351 else if (lex_match_id (lexer, "ARRANGEMENT"))
355 lex_match (lexer, T_EQUALS);
356 if (lex_match_id (lexer, "FIXED"))
357 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
358 else if (lex_match_id (lexer, "DELIMITED"))
359 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
360 DP_DELIMITED, &has_type);
363 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
369 else if (lex_match_id (lexer, "FIRSTCASE"))
371 lex_match (lexer, T_EQUALS);
372 if (!lex_force_int (lexer))
374 if (lex_integer (lexer) < 1)
376 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
379 data_parser_set_skip (parser, lex_integer (lexer) - 1);
382 else if (lex_match_id_n (lexer, "DELCASE", 4))
384 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
386 lex_match (lexer, T_EQUALS);
387 if (lex_match_id (lexer, "LINE"))
388 data_parser_set_span (parser, false);
389 else if (lex_match_id (lexer, "VARIABLES"))
391 data_parser_set_span (parser, true);
393 /* VARIABLES takes an integer argument, but for no
394 good reason. We just ignore it. */
395 if (!lex_force_int (lexer))
401 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
405 else if (lex_match_id (lexer, "FIXCASE"))
407 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
409 lex_match (lexer, T_EQUALS);
410 if (!lex_force_int (lexer))
412 if (lex_integer (lexer) < 1)
414 msg (SE, _("Value of FIXCASE must be at least 1."));
417 data_parser_set_records (parser, lex_integer (lexer));
420 else if (lex_match_id (lexer, "IMPORTCASES"))
422 lex_match (lexer, T_EQUALS);
423 if (lex_match (lexer, T_ALL))
425 data_parser_set_case_limit (parser, -1);
426 data_parser_set_case_percent (parser, 100);
428 else if (lex_match_id (lexer, "FIRST"))
430 if (!lex_force_int (lexer))
432 if (lex_integer (lexer) < 1)
434 msg (SE, _("Value of FIRST must be at least 1."));
437 data_parser_set_case_limit (parser, lex_integer (lexer));
440 else if (lex_match_id (lexer, "PERCENT"))
442 if (!lex_force_int (lexer))
444 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
446 msg (SE, _("Value of PERCENT must be between 1 and 100."));
449 data_parser_set_case_percent (parser, lex_integer (lexer));
453 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
455 struct string hard_seps = DS_EMPTY_INITIALIZER;
456 const char *soft_seps = "";
460 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
462 lex_match (lexer, T_EQUALS);
464 if (!lex_force_string (lexer))
467 /* XXX should support multibyte UTF-8 characters */
468 s = lex_tokss (lexer);
469 if (ss_match_string (&s, ss_cstr ("\\t")))
470 ds_put_cstr (&hard_seps, "\t");
471 if (ss_match_string (&s, ss_cstr ("\\\\")))
472 ds_put_cstr (&hard_seps, "\\");
473 while ((c = ss_get_byte (&s)) != EOF)
477 ds_put_byte (&hard_seps, c);
478 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
479 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
480 ds_destroy (&hard_seps);
484 else if (lex_match_id (lexer, "QUALIFIERS"))
486 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
488 lex_match (lexer, T_EQUALS);
490 if (!lex_force_string (lexer))
493 /* XXX should support multibyte UTF-8 characters */
494 if (settings_get_syntax () == COMPATIBLE
495 && ss_length (lex_tokss (lexer)) != 1)
497 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
498 "must contain exactly one character."));
502 data_parser_set_quotes (parser, lex_tokss (lexer));
505 else if (settings_get_syntax () == ENHANCED
506 && lex_match_id (lexer, "ESCAPE"))
507 data_parser_set_quote_escape (parser, true);
508 else if (lex_match_id (lexer, "VARIABLES"))
512 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
516 lex_match (lexer, T_EQUALS);
519 type = data_parser_get_type (parser);
522 struct fmt_spec input, output;
526 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
528 if (!lex_force_int (lexer))
530 if (lex_integer (lexer) < record)
532 msg (SE, _("The record number specified, %ld, is at or "
533 "before the previous record, %d. Data "
534 "fields must be listed in order of "
535 "increasing record number."),
536 lex_integer (lexer), record);
539 if (lex_integer (lexer) > data_parser_get_records (parser))
541 msg (SE, _("The record number specified, %ld, exceeds "
542 "the number of records per case specified "
544 lex_integer (lexer), data_parser_get_records (parser));
547 record = lex_integer (lexer);
551 if (!lex_force_id (lexer)
552 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
554 name = xstrdup (lex_tokcstr (lexer));
557 if (type == DP_DELIMITED)
559 if (!parse_format_specifier (lexer, &input)
560 || !fmt_check_input (&input))
563 output = fmt_for_output_from_input (&input);
567 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
568 enum fmt_type fmt_type;
571 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
574 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
575 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
577 if (!fmt_from_name (fmt_type_name, &fmt_type))
579 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
583 /* Compose input format. */
584 input.type = fmt_type;
585 input.w = lc - fc + 1;
587 if (!fmt_check_input (&input))
590 /* Compose output format. */
593 output.type = fmt_type;
596 if (!fmt_check_output (&output))
600 output = fmt_for_output_from_input (&input);
603 v = dict_create_var (dict, name, fmt_var_width (&input));
606 msg (SE, _("%s is a duplicate variable name."), name);
609 var_set_both_formats (v, &output);
611 if (type == DP_DELIMITED)
612 data_parser_add_delimited_field (parser, &input,
613 var_get_case_index (v),
616 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
621 while (lex_token (lexer) != T_ENDCMD);
623 reader = dfm_open_reader (fh, lexer, encoding);
627 data_parser_make_active_file (parser, ds, reader, dict);
633 data_parser_destroy (parser);
638 return CMD_CASCADING_FAILURE;
643 destroy_spreadsheet_read_info (struct spreadsheet_read_info *sri)
648 free (sri->sheet_name);
649 free (sri->cell_range);
650 free (sri->file_name);