1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012,
3 2013, 2015, 2016 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 if (!parse_spreadsheet (lexer, &filename, &opts))
116 struct spreadsheet *spreadsheet = NULL;
117 if ( gnm_read_support && 0 == strncasecmp (tok, "GNM", 3))
118 spreadsheet = gnumeric_probe (filename, true);
119 else if ( odf_read_support && 0 == strncasecmp (tok, "ODS", 3))
120 spreadsheet = ods_probe (filename, true);
123 if (spreadsheet == NULL)
126 struct casereader *reader = spreadsheet_make_reader (spreadsheet, &opts);
129 dataset_set_dict (ds, dict_clone (spreadsheet->dict));
130 dataset_set_source (ds, reader);
132 destroy_spreadsheet_read_info (&opts);
133 spreadsheet_unref (spreadsheet);
136 spreadsheet_unref (spreadsheet);
139 msg (SE, _("Unsupported TYPE %s."), tok);
143 destroy_spreadsheet_read_info (&opts);
149 parse_get_psql (struct lexer *lexer, struct dataset *ds)
151 struct psql_read_info psql;
152 psql.allow_clear = false;
153 psql.conninfo = NULL;
156 ds_init_empty (&psql.sql);
158 lex_force_match (lexer, T_SLASH);
160 if (!lex_force_match_id (lexer, "CONNECT"))
163 lex_force_match (lexer, T_EQUALS);
165 if (!lex_force_string (lexer))
168 psql.conninfo = ss_xstrdup (lex_tokss (lexer));
172 while (lex_match (lexer, T_SLASH) )
174 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
176 lex_match (lexer, T_EQUALS);
177 psql.str_width = lex_integer (lexer);
180 else if ( lex_match_id (lexer, "BSIZE"))
182 lex_match (lexer, T_EQUALS);
183 psql.bsize = lex_integer (lexer);
186 else if ( lex_match_id (lexer, "UNENCRYPTED"))
188 psql.allow_clear = true;
190 else if (lex_match_id (lexer, "SQL"))
192 lex_match (lexer, T_EQUALS);
193 if ( ! lex_force_string (lexer) )
196 ds_put_substring (&psql.sql, lex_tokss (lexer));
201 struct dictionary *dict = NULL;
202 struct casereader *reader = psql_open_reader (&psql, &dict);
206 dataset_set_dict (ds, dict);
207 dataset_set_source (ds, reader);
211 ds_destroy (&psql.sql);
212 free (psql.conninfo);
218 ds_destroy (&psql.sql);
219 free (psql.conninfo);
225 parse_spreadsheet (struct lexer *lexer, char **filename,
226 struct spreadsheet_read_options *opts)
228 opts->sheet_index = 1;
229 opts->sheet_name = NULL;
230 opts->cell_range = NULL;
231 opts->read_names = true;
234 lex_force_match (lexer, T_SLASH);
236 if (!lex_force_match_id (lexer, "FILE"))
239 lex_force_match (lexer, T_EQUALS);
241 if (!lex_force_string (lexer))
244 *filename = utf8_to_filename (lex_tokcstr (lexer));
248 while (lex_match (lexer, T_SLASH) )
250 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
252 lex_match (lexer, T_EQUALS);
253 opts->asw = lex_integer (lexer);
256 else if (lex_match_id (lexer, "SHEET"))
258 lex_match (lexer, T_EQUALS);
259 if (lex_match_id (lexer, "NAME"))
261 if ( ! lex_force_string (lexer) )
264 opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
265 opts->sheet_index = -1;
269 else if (lex_match_id (lexer, "INDEX"))
271 opts->sheet_index = lex_integer (lexer);
272 if (opts->sheet_index <= 0)
274 msg (SE, _("The sheet index must be greater than or equal to 1"));
281 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
282 "/SHEET", "NAME", "INDEX");
286 else if (lex_match_id (lexer, "CELLRANGE"))
288 lex_match (lexer, T_EQUALS);
290 if (lex_match_id (lexer, "FULL"))
292 opts->cell_range = NULL;
294 else if (lex_match_id (lexer, "RANGE"))
296 if ( ! lex_force_string (lexer) )
299 opts->cell_range = ss_xstrdup (lex_tokss (lexer));
304 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
305 "/CELLRANGE", "FULL", "RANGE");
309 else if (lex_match_id (lexer, "READNAMES"))
311 lex_match (lexer, T_EQUALS);
313 if ( lex_match_id (lexer, "ON"))
315 opts->read_names = true;
317 else if (lex_match_id (lexer, "OFF"))
319 opts->read_names = false;
323 msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
324 "/READNAMES", "ON", "OFF");
330 lex_error (lexer, NULL);
343 set_type (struct data_parser *parser, const char *subcommand,
344 enum data_parser_type type, bool *has_type)
348 data_parser_set_type (parser, type);
351 else if (type != data_parser_get_type (parser))
353 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
354 "was stated or implied earlier in this command."),
356 type == DP_FIXED ? "FIXED" : "DELIMITED",
357 type == DP_FIXED ? "DELIMITED" : "FIXED");
364 parse_get_txt (struct lexer *lexer, struct dataset *ds)
366 struct data_parser *parser = NULL;
367 struct dictionary *dict = dict_create (get_default_encoding ());
368 struct file_handle *fh = NULL;
369 struct dfm_reader *reader = NULL;
370 char *encoding = NULL;
374 enum data_parser_type type;
377 lex_force_match (lexer, T_SLASH);
379 if (!lex_force_match_id (lexer, "FILE"))
381 lex_force_match (lexer, T_EQUALS);
382 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
386 parser = data_parser_create (dict);
388 data_parser_set_type (parser, DP_DELIMITED);
389 data_parser_set_span (parser, false);
390 data_parser_set_quotes (parser, ss_empty ());
391 data_parser_set_quote_escape (parser, true);
392 data_parser_set_empty_line_has_field (parser, true);
396 if (!lex_force_match (lexer, T_SLASH))
399 if (lex_match_id (lexer, "ENCODING"))
401 lex_match (lexer, T_EQUALS);
402 if (!lex_force_string (lexer))
406 encoding = ss_xstrdup (lex_tokss (lexer));
410 else if (lex_match_id (lexer, "ARRANGEMENT"))
414 lex_match (lexer, T_EQUALS);
415 if (lex_match_id (lexer, "FIXED"))
416 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
417 else if (lex_match_id (lexer, "DELIMITED"))
418 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
419 DP_DELIMITED, &has_type);
422 lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
428 else if (lex_match_id (lexer, "FIRSTCASE"))
430 lex_match (lexer, T_EQUALS);
431 if (!lex_force_int (lexer))
433 if (lex_integer (lexer) < 1)
435 msg (SE, _("Value of %s must be 1 or greater."), "FIRSTCASE");
438 data_parser_set_skip (parser, lex_integer (lexer) - 1);
441 else if (lex_match_id_n (lexer, "DELCASE", 4))
443 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
445 lex_match (lexer, T_EQUALS);
446 if (lex_match_id (lexer, "LINE"))
447 data_parser_set_span (parser, false);
448 else if (lex_match_id (lexer, "VARIABLES"))
450 data_parser_set_span (parser, true);
452 /* VARIABLES takes an integer argument, but for no
453 good reason. We just ignore it. */
454 if (!lex_force_int (lexer))
460 lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
464 else if (lex_match_id (lexer, "FIXCASE"))
466 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
468 lex_match (lexer, T_EQUALS);
469 if (!lex_force_int (lexer))
471 if (lex_integer (lexer) < 1)
473 msg (SE, _("Value of %s must be 1 or greater."), "FIXCASE");
476 data_parser_set_records (parser, lex_integer (lexer));
479 else if (lex_match_id (lexer, "IMPORTCASES"))
481 lex_match (lexer, T_EQUALS);
482 if (lex_match (lexer, T_ALL))
486 else if (lex_match_id (lexer, "FIRST"))
488 if (!lex_force_int (lexer))
492 else if (lex_match_id (lexer, "PERCENT"))
494 if (!lex_force_int (lexer))
498 msg (SW, _("Ignoring obsolete IMPORTCASES subcommand. (N OF CASES "
499 "or SAMPLE may be used to substitute.)"));
501 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
503 struct string hard_seps = DS_EMPTY_INITIALIZER;
504 const char *soft_seps = "";
508 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
510 lex_match (lexer, T_EQUALS);
512 if (!lex_force_string (lexer))
515 /* XXX should support multibyte UTF-8 characters */
516 s = lex_tokss (lexer);
517 if (ss_match_string (&s, ss_cstr ("\\t")))
518 ds_put_cstr (&hard_seps, "\t");
519 if (ss_match_string (&s, ss_cstr ("\\\\")))
520 ds_put_cstr (&hard_seps, "\\");
521 while ((c = ss_get_byte (&s)) != EOF)
525 ds_put_byte (&hard_seps, c);
526 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
527 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
528 ds_destroy (&hard_seps);
532 else if (lex_match_id (lexer, "QUALIFIERS"))
534 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
536 lex_match (lexer, T_EQUALS);
538 if (!lex_force_string (lexer))
541 /* XXX should support multibyte UTF-8 characters */
542 if (settings_get_syntax () == COMPATIBLE
543 && ss_length (lex_tokss (lexer)) != 1)
545 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
546 "must contain exactly one character."));
550 data_parser_set_quotes (parser, lex_tokss (lexer));
553 else if (lex_match_id (lexer, "VARIABLES"))
557 lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
561 lex_match (lexer, T_EQUALS);
564 type = data_parser_get_type (parser);
567 struct fmt_spec input, output;
571 while (type == DP_FIXED && lex_match (lexer, T_SLASH))
573 if (!lex_force_int (lexer))
575 if (lex_integer (lexer) < record)
577 msg (SE, _("The record number specified, %ld, is at or "
578 "before the previous record, %d. Data "
579 "fields must be listed in order of "
580 "increasing record number."),
581 lex_integer (lexer), record);
584 if (lex_integer (lexer) > data_parser_get_records (parser))
586 msg (SE, _("The record number specified, %ld, exceeds "
587 "the number of records per case specified "
589 lex_integer (lexer), data_parser_get_records (parser));
592 record = lex_integer (lexer);
596 if (!lex_force_id (lexer)
597 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
599 name = xstrdup (lex_tokcstr (lexer));
602 if (type == DP_DELIMITED)
604 if (!parse_format_specifier (lexer, &input)
605 || !fmt_check_input (&input))
608 output = fmt_for_output_from_input (&input);
612 char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
613 enum fmt_type fmt_type;
616 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
619 /* Accept a format (e.g. F8.2) or just a type name (e.g. DOLLAR). */
620 if (!parse_abstract_format_specifier (lexer, fmt_type_name, &w, &d))
622 if (!fmt_from_name (fmt_type_name, &fmt_type))
624 msg (SE, _("Unknown format type `%s'."), fmt_type_name);
628 /* Compose input format. */
629 input.type = fmt_type;
630 input.w = lc - fc + 1;
632 if (!fmt_check_input (&input))
635 /* Compose output format. */
638 output.type = fmt_type;
641 if (!fmt_check_output (&output))
645 output = fmt_for_output_from_input (&input);
648 v = dict_create_var (dict, name, fmt_var_width (&input));
651 msg (SE, _("%s is a duplicate variable name."), name);
654 var_set_both_formats (v, &output);
656 if (type == DP_DELIMITED)
657 data_parser_add_delimited_field (parser, &input,
658 var_get_case_index (v),
661 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
666 while (lex_token (lexer) != T_ENDCMD);
668 reader = dfm_open_reader (fh, lexer, encoding);
672 data_parser_make_active_file (parser, ds, reader, dict);
678 data_parser_destroy (parser);
683 return CMD_CASCADING_FAILURE;
688 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
690 free (opts->cell_range);
691 free (opts->sheet_name);