1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012,
3 2013, 2015 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)
50 #ifdef ODF_READ_SUPPORT
51 static const bool odf_read_support = true;
53 static const bool odf_read_support = false;
54 struct spreadsheet *ods_probe (const char *filename, bool report_errors){}
55 struct casereader * ods_make_reader (struct spreadsheet *spreadsheet,
56 const struct spreadsheet_read_options *opts){}
57 void ods_unref (struct spreadsheet *r){}
60 #ifdef GNM_READ_SUPPORT
61 static const bool gnm_read_support = true;
63 static const bool gnm_read_support = false;
64 struct spreadsheet *gnumeric_probe (const char *filename, bool report_errors){}
65 struct casereader * gnumeric_make_reader (struct spreadsheet *spreadsheet,
66 const struct spreadsheet_read_options *opts){}
67 void gnumeric_unref (struct spreadsheet *r){}
71 static bool parse_spreadsheet (struct lexer *lexer, char **filename,
72 struct spreadsheet_read_options *opts);
74 static void destroy_spreadsheet_read_info (struct spreadsheet_read_options *);
76 static int parse_get_txt (struct lexer *lexer, struct dataset *);
77 static int parse_get_psql (struct lexer *lexer, struct dataset *);
80 cmd_get_data (struct lexer *lexer, struct dataset *ds)
83 struct spreadsheet_read_options opts;
85 opts.sheet_name = NULL;
86 opts.sheet_index = -1;
87 opts.cell_range = NULL;
88 opts.read_names = false;
91 lex_force_match (lexer, T_SLASH);
93 if (!lex_force_match_id (lexer, "TYPE"))
96 lex_force_match (lexer, T_EQUALS);
98 tok = strdup (lex_tokcstr (lexer));
99 if (lex_match_id (lexer, "TXT"))
102 return parse_get_txt (lexer, ds);
104 else if (lex_match_id (lexer, "PSQL"))
107 return parse_get_psql (lexer, ds);
109 else if (lex_match_id (lexer, "GNM") ||
110 lex_match_id (lexer, "ODS"))
112 char *filename = NULL;
113 struct casereader *reader = NULL;
114 struct dictionary *dict = NULL;
116 if (!parse_spreadsheet (lexer, &filename, &opts))
119 if ( gnm_read_support && 0 == strncasecmp (tok, "GNM", 3))
121 struct spreadsheet *spreadsheet = gnumeric_probe (filename, true);
122 if (spreadsheet == NULL)
124 reader = gnumeric_make_reader (spreadsheet, &opts);
125 dict = spreadsheet->dict;
126 gnumeric_unref (spreadsheet);
128 else if ( odf_read_support && 0 == strncasecmp (tok, "ODS", 3))
130 struct spreadsheet *spreadsheet = ods_probe (filename, true);
131 if (spreadsheet == NULL)
133 reader = ods_make_reader (spreadsheet, &opts);
134 dict = spreadsheet->dict;
135 ods_unref (spreadsheet);
142 dataset_set_dict (ds, dict);
143 dataset_set_source (ds, reader);
145 destroy_spreadsheet_read_info (&opts);
150 msg (SE, _("Unsupported TYPE %s."), tok);
154 destroy_spreadsheet_read_info (&opts);
160 parse_get_psql (struct lexer *lexer, struct dataset *ds)
162 struct psql_read_info psql;
163 psql.allow_clear = false;
164 psql.conninfo = NULL;
167 ds_init_empty (&psql.sql);
169 lex_force_match (lexer, T_SLASH);
171 if (!lex_force_match_id (lexer, "CONNECT"))
174 lex_force_match (lexer, T_EQUALS);
176 if (!lex_force_string (lexer))
179 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
183 while (lex_match (lexer, T_SLASH) )
185 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
187 lex_match (lexer, T_EQUALS);
188 psql.str_width = lex_integer (lexer);
191 else if ( lex_match_id (lexer, "BSIZE"))
193 lex_match (lexer, T_EQUALS);
194 psql.bsize = lex_integer (lexer);
197 else if ( lex_match_id (lexer, "UNENCRYPTED"))
199 psql.allow_clear = true;
201 else if (lex_match_id (lexer, "SQL"))
203 lex_match (lexer, T_EQUALS);
204 if ( ! lex_force_string (lexer) )
207 ds_put_substring (&psql.sql, lex_tokss (lexer));
212 struct dictionary *dict = NULL;
213 struct casereader *reader = psql_open_reader (&psql, &dict);
217 dataset_set_dict (ds, dict);
218 dataset_set_source (ds, reader);
222 ds_destroy (&psql.sql);
223 free (psql.conninfo);
229 ds_destroy (&psql.sql);
230 free (psql.conninfo);
236 parse_spreadsheet (struct lexer *lexer, char **filename,
237 struct spreadsheet_read_options *opts)
239 opts->sheet_index = 1;
240 opts->sheet_name = NULL;
241 opts->cell_range = NULL;
242 opts->read_names = true;
245 lex_force_match (lexer, T_SLASH);
247 if (!lex_force_match_id (lexer, "FILE"))
250 lex_force_match (lexer, T_EQUALS);
252 if (!lex_force_string (lexer))
255 *filename = utf8_to_filename (lex_tokcstr (lexer));
259 while (lex_match (lexer, T_SLASH) )
261 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
263 lex_match (lexer, T_EQUALS);
264 opts->asw = lex_integer (lexer);
267 else if (lex_match_id (lexer, "SHEET"))
269 lex_match (lexer, T_EQUALS);
270 if (lex_match_id (lexer, "NAME"))
272 if ( ! lex_force_string (lexer) )
275 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
276 opts->sheet_index = -1;
280 else if (lex_match_id (lexer, "INDEX"))
282 opts->sheet_index = lex_integer (lexer);
283 if (opts->sheet_index <= 0)
285 msg (SE, _("The sheet index must be greater than or equal to 1"));
292 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
293 "/SHEET", "NAME", "INDEX");
297 else if (lex_match_id (lexer, "CELLRANGE"))
299 lex_match (lexer, T_EQUALS);
301 if (lex_match_id (lexer, "FULL"))
303 opts->cell_range = NULL;
305 else if (lex_match_id (lexer, "RANGE"))
307 if ( ! lex_force_string (lexer) )
310 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
315 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
316 "/CELLRANGE", "FULL", "RANGE");
320 else if (lex_match_id (lexer, "READNAMES"))
322 lex_match (lexer, T_EQUALS);
324 if ( lex_match_id (lexer, "ON"))
326 opts->read_names = true;
328 else if (lex_match_id (lexer, "OFF"))
330 opts->read_names = false;
334 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
335 "/READNAMES", "ON", "OFF");
341 lex_error (lexer, NULL);
354 set_type (struct data_parser *parser, const char *subcommand,
355 enum data_parser_type type, bool *has_type)
359 data_parser_set_type (parser, type);
362 else if (type != data_parser_get_type (parser))
364 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
365 "was stated or implied earlier in this command."),
367 type == DP_FIXED ? "FIXED" : "DELIMITED",
368 type == DP_FIXED ? "DELIMITED" : "FIXED");
375 parse_get_txt (struct lexer *lexer, struct dataset *ds)
377 struct data_parser *parser = NULL;
378 struct dictionary *dict = dict_create (get_default_encoding ());
379 struct file_handle *fh = NULL;
380 struct dfm_reader *reader = NULL;
381 char *encoding = NULL;
385 enum data_parser_type type;
388 lex_force_match (lexer, T_SLASH);
390 if (!lex_force_match_id (lexer, "FILE"))
392 lex_force_match (lexer, T_EQUALS);
393 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
397 parser = data_parser_create (dict);
399 data_parser_set_type (parser, DP_DELIMITED);
400 data_parser_set_span (parser, false);
401 data_parser_set_quotes (parser, ss_empty ());
402 data_parser_set_quote_escape (parser, true);
403 data_parser_set_empty_line_has_field (parser, true);
407 if (!lex_force_match (lexer, T_SLASH))
410 if (lex_match_id (lexer, "ENCODING"))
412 lex_match (lexer, T_EQUALS);
413 if (!lex_force_string (lexer))
417 encoding = ss_xstrdup (lex_tokss (lexer));
421 else if (lex_match_id (lexer, "ARRANGEMENT"))
425 lex_match (lexer, T_EQUALS);
426 if (lex_match_id (lexer, "FIXED"))
427 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
428 else if (lex_match_id (lexer, "DELIMITED"))
429 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
430 DP_DELIMITED, &has_type);
433 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
439 else if (lex_match_id (lexer, "FIRSTCASE"))
441 lex_match (lexer, T_EQUALS);
442 if (!lex_force_int (lexer))
444 if (lex_integer (lexer) < 1)
446 msg (SE, _("Value of %s must be 1 or greater."), "FIRSTCASE");
449 data_parser_set_skip (parser, lex_integer (lexer) - 1);
452 else if (lex_match_id_n (lexer, "DELCASE", 4))
454 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
456 lex_match (lexer, T_EQUALS);
457 if (lex_match_id (lexer, "LINE"))
458 data_parser_set_span (parser, false);
459 else if (lex_match_id (lexer, "VARIABLES"))
461 data_parser_set_span (parser, true);
463 /* VARIABLES takes an integer argument, but for no
464 good reason. We just ignore it. */
465 if (!lex_force_int (lexer))
471 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
475 else if (lex_match_id (lexer, "FIXCASE"))
477 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
479 lex_match (lexer, T_EQUALS);
480 if (!lex_force_int (lexer))
482 if (lex_integer (lexer) < 1)
484 msg (SE, _("Value of %s must be 1 or greater."), "FIXCASE");
487 data_parser_set_records (parser, lex_integer (lexer));
490 else if (lex_match_id (lexer, "IMPORTCASES"))
492 lex_match (lexer, T_EQUALS);
493 if (lex_match (lexer, T_ALL))
495 data_parser_set_case_limit (parser, -1);
496 data_parser_set_case_percent (parser, 100);
498 else if (lex_match_id (lexer, "FIRST"))
500 if (!lex_force_int (lexer))
502 if (lex_integer (lexer) < 1)
504 msg (SE, _("Value of %s must be 1 or greater."), "FIRST");
507 data_parser_set_case_limit (parser, lex_integer (lexer));
510 else if (lex_match_id (lexer, "PERCENT"))
512 if (!lex_force_int (lexer))
514 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
516 msg (SE, _("Value of %s must be between 1 and 100."), "PERCENT");
519 data_parser_set_case_percent (parser, lex_integer (lexer));
523 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
525 struct string hard_seps = DS_EMPTY_INITIALIZER;
526 const char *soft_seps = "";
530 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
532 lex_match (lexer, T_EQUALS);
534 if (!lex_force_string (lexer))
537 /* XXX should support multibyte UTF-8 characters */
538 s = lex_tokss (lexer);
539 if (ss_match_string (&s, ss_cstr ("\\t")))
540 ds_put_cstr (&hard_seps, "\t");
541 if (ss_match_string (&s, ss_cstr ("\\\\")))
542 ds_put_cstr (&hard_seps, "\\");
543 while ((c = ss_get_byte (&s)) != EOF)
547 ds_put_byte (&hard_seps, c);
548 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
549 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
550 ds_destroy (&hard_seps);
554 else if (lex_match_id (lexer, "QUALIFIERS"))
556 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
558 lex_match (lexer, T_EQUALS);
560 if (!lex_force_string (lexer))
563 /* XXX should support multibyte UTF-8 characters */
564 if (settings_get_syntax () == COMPATIBLE
565 && ss_length (lex_tokss (lexer)) != 1)
567 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
568 "must contain exactly one character."));
572 data_parser_set_quotes (parser, lex_tokss (lexer));
575 else if (lex_match_id (lexer, "VARIABLES"))
579 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
583 lex_match (lexer, T_EQUALS);
586 type = data_parser_get_type (parser);
589 struct fmt_spec input, output;
593 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
595 if (!lex_force_int (lexer))
597 if (lex_integer (lexer) < record)
599 msg (SE, _("The record number specified, %ld, is at or "
600 "before the previous record, %d. Data "
601 "fields must be listed in order of "
602 "increasing record number."),
603 lex_integer (lexer), record);
606 if (lex_integer (lexer) > data_parser_get_records (parser))
608 msg (SE, _("The record number specified, %ld, exceeds "
609 "the number of records per case specified "
611 lex_integer (lexer), data_parser_get_records (parser));
614 record = lex_integer (lexer);
618 if (!lex_force_id (lexer)
619 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
621 name = xstrdup (lex_tokcstr (lexer));
624 if (type == DP_DELIMITED)
626 if (!parse_format_specifier (lexer, &input)
627 || !fmt_check_input (&input))
630 output = fmt_for_output_from_input (&input);
634 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
635 enum fmt_type fmt_type;
638 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
641 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
642 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
644 if (!fmt_from_name (fmt_type_name, &fmt_type))
646 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
650 /* Compose input format. */
651 input.type = fmt_type;
652 input.w = lc - fc + 1;
654 if (!fmt_check_input (&input))
657 /* Compose output format. */
660 output.type = fmt_type;
663 if (!fmt_check_output (&output))
667 output = fmt_for_output_from_input (&input);
670 v = dict_create_var (dict, name, fmt_var_width (&input));
673 msg (SE, _("%s is a duplicate variable name."), name);
676 var_set_both_formats (v, &output);
678 if (type == DP_DELIMITED)
679 data_parser_add_delimited_field (parser, &input,
680 var_get_case_index (v),
683 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
688 while (lex_token (lexer) != T_ENDCMD);
690 reader = dfm_open_reader (fh, lexer, encoding);
694 data_parser_make_active_file (parser, ds, reader, dict);
700 data_parser_destroy (parser);
705 return CMD_CASCADING_FAILURE;
710 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
712 free (opts->cell_range);
713 free (opts->sheet_name);