1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011 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/i18n.h"
39 #include "libpspp/message.h"
41 #include "gl/xalloc.h"
44 #define _(msgid) gettext (msgid)
45 #define N_(msgid) (msgid)
47 static struct spreadsheet_read_info *parse_spreadsheet (struct lexer *lexer);
48 static void destroy_spreadsheet_read_info (struct spreadsheet_read_info *);
50 static int parse_get_txt (struct lexer *lexer, struct dataset *);
51 static int parse_get_psql (struct lexer *lexer, struct dataset *);
54 cmd_get_data (struct lexer *lexer, struct dataset *ds)
57 lex_force_match (lexer, T_SLASH);
59 if (!lex_force_match_id (lexer, "TYPE"))
62 lex_force_match (lexer, T_EQUALS);
64 tok = strdup (lex_tokcstr (lexer));
65 if (lex_match_id (lexer, "TXT"))
67 return parse_get_txt (lexer, ds);
69 else if (lex_match_id (lexer, "PSQL"))
71 return parse_get_psql (lexer, ds);
73 else if (lex_match_id (lexer, "GNM") ||
74 lex_match_id (lexer, "ODS"))
76 struct casereader *reader = NULL;
77 struct dictionary *dict = NULL;
78 struct spreadsheet_read_info *sri = parse_spreadsheet (lexer);
82 if ( 0 == strncasecmp (tok, "GNM", 3))
83 reader = gnumeric_open_reader (sri, &dict);
84 else if (0 == strncasecmp (tok, "ODS", 3))
85 reader = ods_open_reader (sri, &dict);
89 dataset_set_dict (ds, dict);
90 dataset_set_source (ds, reader);
91 destroy_spreadsheet_read_info (sri);
95 destroy_spreadsheet_read_info (sri);
98 msg (SE, _("Unsupported TYPE %s."), tok);
106 parse_get_psql (struct lexer *lexer, struct dataset *ds)
108 struct psql_read_info psql;
109 psql.allow_clear = false;
110 psql.conninfo = NULL;
113 ds_init_empty (&psql.sql);
115 lex_force_match (lexer, T_SLASH);
117 if (!lex_force_match_id (lexer, "CONNECT"))
120 lex_force_match (lexer, T_EQUALS);
122 if (!lex_force_string (lexer))
125 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
129 while (lex_match (lexer, T_SLASH) )
131 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
133 lex_match (lexer, T_EQUALS);
134 psql.str_width = lex_integer (lexer);
137 else if ( lex_match_id (lexer, "BSIZE"))
139 lex_match (lexer, T_EQUALS);
140 psql.bsize = lex_integer (lexer);
143 else if ( lex_match_id (lexer, "UNENCRYPTED"))
145 psql.allow_clear = true;
147 else if (lex_match_id (lexer, "SQL"))
149 lex_match (lexer, T_EQUALS);
150 if ( ! lex_force_string (lexer) )
153 ds_put_substring (&psql.sql, lex_tokss (lexer));
158 struct dictionary *dict = NULL;
159 struct casereader *reader = psql_open_reader (&psql, &dict);
163 dataset_set_dict (ds, dict);
164 dataset_set_source (ds, reader);
168 ds_destroy (&psql.sql);
169 free (psql.conninfo);
175 ds_destroy (&psql.sql);
176 free (psql.conninfo);
181 static struct spreadsheet_read_info *
182 parse_spreadsheet (struct lexer *lexer)
184 struct spreadsheet_read_info *sri = xzalloc (sizeof *sri);
185 sri->sheet_index = 1;
186 sri->read_names = true;
189 lex_force_match (lexer, T_SLASH);
191 if (!lex_force_match_id (lexer, "FILE"))
194 lex_force_match (lexer, T_EQUALS);
196 if (!lex_force_string (lexer))
199 sri->file_name = utf8_to_filename (lex_tokcstr (lexer));
203 while (lex_match (lexer, T_SLASH) )
205 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
207 lex_match (lexer, T_EQUALS);
208 sri->asw = lex_integer (lexer);
211 else if (lex_match_id (lexer, "SHEET"))
213 lex_match (lexer, T_EQUALS);
214 if (lex_match_id (lexer, "NAME"))
216 if ( ! lex_force_string (lexer) )
219 sri->sheet_name = ss_xstrdup (lex_tokss (lexer));
220 sri->sheet_index = -1;
224 else if (lex_match_id (lexer, "INDEX"))
226 sri->sheet_index = lex_integer (lexer);
232 else if (lex_match_id (lexer, "CELLRANGE"))
234 lex_match (lexer, T_EQUALS);
236 if (lex_match_id (lexer, "FULL"))
238 sri->cell_range = NULL;
240 else if (lex_match_id (lexer, "RANGE"))
242 if ( ! lex_force_string (lexer) )
245 sri->cell_range = ss_xstrdup (lex_tokss (lexer));
251 else if (lex_match_id (lexer, "READNAMES"))
253 lex_match (lexer, T_EQUALS);
255 if ( lex_match_id (lexer, "ON"))
257 sri->read_names = true;
259 else if (lex_match_id (lexer, "OFF"))
261 sri->read_names = false;
268 lex_error (lexer, NULL);
276 destroy_spreadsheet_read_info (sri);
282 set_type (struct data_parser *parser, const char *subcommand,
283 enum data_parser_type type, bool *has_type)
287 data_parser_set_type (parser, type);
290 else if (type != data_parser_get_type (parser))
292 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
293 "was stated or implied earlier in this command."),
295 type == DP_FIXED ? "FIXED" : "DELIMITED",
296 type == DP_FIXED ? "DELIMITED" : "FIXED");
303 parse_get_txt (struct lexer *lexer, struct dataset *ds)
305 struct data_parser *parser = NULL;
306 struct dictionary *dict = dict_create (get_default_encoding ());
307 struct file_handle *fh = NULL;
308 struct dfm_reader *reader = NULL;
312 enum data_parser_type type;
315 lex_force_match (lexer, T_SLASH);
317 if (!lex_force_match_id (lexer, "FILE"))
319 lex_force_match (lexer, T_EQUALS);
320 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
324 parser = data_parser_create (dict);
326 data_parser_set_type (parser, DP_DELIMITED);
327 data_parser_set_span (parser, false);
328 data_parser_set_quotes (parser, ss_empty ());
329 data_parser_set_empty_line_has_field (parser, true);
333 if (!lex_force_match (lexer, T_SLASH))
336 if (lex_match_id (lexer, "ARRANGEMENT"))
340 lex_match (lexer, T_EQUALS);
341 if (lex_match_id (lexer, "FIXED"))
342 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
343 else if (lex_match_id (lexer, "DELIMITED"))
344 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
345 DP_DELIMITED, &has_type);
348 lex_error (lexer, _("expecting %s or %s"), "FIXED", "DELIMITED");
354 else if (lex_match_id (lexer, "FIRSTCASE"))
356 lex_match (lexer, T_EQUALS);
357 if (!lex_force_int (lexer))
359 if (lex_integer (lexer) < 1)
361 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
364 data_parser_set_skip (parser, lex_integer (lexer) - 1);
367 else if (lex_match_id_n (lexer, "DELCASE", 4))
369 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
371 lex_match (lexer, T_EQUALS);
372 if (lex_match_id (lexer, "LINE"))
373 data_parser_set_span (parser, false);
374 else if (lex_match_id (lexer, "VARIABLES"))
376 data_parser_set_span (parser, true);
378 /* VARIABLES takes an integer argument, but for no
379 good reason. We just ignore it. */
380 if (!lex_force_int (lexer))
386 lex_error (lexer, _("expecting %s or %s"), "LINE", "VARIABLES");
390 else if (lex_match_id (lexer, "FIXCASE"))
392 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
394 lex_match (lexer, T_EQUALS);
395 if (!lex_force_int (lexer))
397 if (lex_integer (lexer) < 1)
399 msg (SE, _("Value of FIXCASE must be at least 1."));
402 data_parser_set_records (parser, lex_integer (lexer));
405 else if (lex_match_id (lexer, "IMPORTCASES"))
407 lex_match (lexer, T_EQUALS);
408 if (lex_match (lexer, T_ALL))
410 data_parser_set_case_limit (parser, -1);
411 data_parser_set_case_percent (parser, 100);
413 else if (lex_match_id (lexer, "FIRST"))
415 if (!lex_force_int (lexer))
417 if (lex_integer (lexer) < 1)
419 msg (SE, _("Value of FIRST must be at least 1."));
422 data_parser_set_case_limit (parser, lex_integer (lexer));
425 else if (lex_match_id (lexer, "PERCENT"))
427 if (!lex_force_int (lexer))
429 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
431 msg (SE, _("Value of PERCENT must be between 1 and 100."));
434 data_parser_set_case_percent (parser, lex_integer (lexer));
438 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
440 struct string hard_seps = DS_EMPTY_INITIALIZER;
441 const char *soft_seps = "";
445 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
447 lex_match (lexer, T_EQUALS);
449 if (!lex_force_string (lexer))
452 /* XXX should support multibyte UTF-8 characters */
453 s = lex_tokss (lexer);
454 if (ss_match_string (&s, ss_cstr ("\\t")))
455 ds_put_cstr (&hard_seps, "\t");
456 if (ss_match_string (&s, ss_cstr ("\\\\")))
457 ds_put_cstr (&hard_seps, "\\");
458 while ((c = ss_get_byte (&s)) != EOF)
462 ds_put_byte (&hard_seps, c);
463 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
464 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
465 ds_destroy (&hard_seps);
469 else if (lex_match_id (lexer, "QUALIFIERS"))
471 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
473 lex_match (lexer, T_EQUALS);
475 if (!lex_force_string (lexer))
478 /* XXX should support multibyte UTF-8 characters */
479 if (settings_get_syntax () == COMPATIBLE
480 && ss_length (lex_tokss (lexer)) != 1)
482 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
483 "must contain exactly one character."));
487 data_parser_set_quotes (parser, lex_tokss (lexer));
490 else if (settings_get_syntax () == ENHANCED
491 && lex_match_id (lexer, "ESCAPE"))
492 data_parser_set_quote_escape (parser, true);
493 else if (lex_match_id (lexer, "VARIABLES"))
497 lex_error (lexer, _("expecting %s"), "VARIABLES");
501 lex_match (lexer, T_EQUALS);
504 type = data_parser_get_type (parser);
507 struct fmt_spec input, output;
511 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
513 if (!lex_force_int (lexer))
515 if (lex_integer (lexer) < record)
517 msg (SE, _("The record number specified, %ld, is at or "
518 "before the previous record, %d. Data "
519 "fields must be listed in order of "
520 "increasing record number."),
521 lex_integer (lexer), record);
524 if (lex_integer (lexer) > data_parser_get_records (parser))
526 msg (SE, _("The record number specified, %ld, exceeds "
527 "the number of records per case specified "
529 lex_integer (lexer), data_parser_get_records (parser));
532 record = lex_integer (lexer);
536 if (!lex_force_id (lexer)
537 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
539 name = xstrdup (lex_tokcstr (lexer));
542 if (type == DP_DELIMITED)
544 if (!parse_format_specifier (lexer, &input)
545 || !fmt_check_input (&input))
548 output = fmt_for_output_from_input (&input);
552 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
553 enum fmt_type fmt_type;
556 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
559 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
560 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
562 if (!fmt_from_name (fmt_type_name, &fmt_type))
564 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
568 /* Compose input format. */
569 input.type = fmt_type;
570 input.w = lc - fc + 1;
572 if (!fmt_check_input (&input))
575 /* Compose output format. */
578 output.type = fmt_type;
581 if (!fmt_check_output (&output))
585 output = fmt_for_output_from_input (&input);
588 v = dict_create_var (dict, name, fmt_var_width (&input));
591 msg (SE, _("%s is a duplicate variable name."), name);
594 var_set_both_formats (v, &output);
596 if (type == DP_DELIMITED)
597 data_parser_add_delimited_field (parser, &input,
598 var_get_case_index (v),
601 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
606 while (lex_token (lexer) != T_ENDCMD);
608 reader = dfm_open_reader (fh, lexer);
612 data_parser_make_active_file (parser, ds, reader, dict);
617 data_parser_destroy (parser);
621 return CMD_CASCADING_FAILURE;
626 destroy_spreadsheet_read_info (struct spreadsheet_read_info *sri)
631 free (sri->sheet_name);
632 free (sri->cell_range);
633 free (sri->file_name);