1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009 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/>. */
21 #include <data/gnumeric-reader.h>
22 #include <data/psql-reader.h>
24 #include <data/dictionary.h>
25 #include <data/format.h>
26 #include <data/procedure.h>
27 #include <data/settings.h>
28 #include <language/command.h>
29 #include <language/data-io/data-parser.h>
30 #include <language/data-io/data-reader.h>
31 #include <language/data-io/file-handle.h>
32 #include <language/data-io/placement-parser.h>
33 #include <language/lexer/format-parser.h>
34 #include <language/lexer/lexer.h>
35 #include <libpspp/message.h>
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) (msgid)
43 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
44 static int parse_get_txt (struct lexer *lexer, struct dataset *);
45 static int parse_get_psql (struct lexer *lexer, struct dataset *);
48 cmd_get_data (struct lexer *lexer, struct dataset *ds)
50 lex_force_match (lexer, '/');
52 if (!lex_force_match_id (lexer, "TYPE"))
55 lex_force_match (lexer, '=');
57 if (lex_match_id (lexer, "GNM"))
58 return parse_get_gnm (lexer, ds);
59 else if (lex_match_id (lexer, "TXT"))
60 return parse_get_txt (lexer, ds);
61 else if (lex_match_id (lexer, "PSQL"))
62 return parse_get_psql (lexer, ds);
64 msg (SE, _("Unsupported TYPE %s"), lex_tokid (lexer));
69 parse_get_psql (struct lexer *lexer, struct dataset *ds)
71 struct psql_read_info psql;
72 psql.allow_clear = false;
76 ds_init_empty (&psql.sql);
78 lex_force_match (lexer, '/');
80 if (!lex_force_match_id (lexer, "CONNECT"))
83 lex_force_match (lexer, '=');
85 if (!lex_force_string (lexer))
88 psql.conninfo = xstrdup (ds_cstr (lex_tokstr (lexer)));
92 while (lex_match (lexer, '/') )
94 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
96 lex_match (lexer, '=');
97 psql.str_width = lex_integer (lexer);
100 else if ( lex_match_id (lexer, "BSIZE"))
102 lex_match (lexer, '=');
103 psql.bsize = lex_integer (lexer);
106 else if ( lex_match_id (lexer, "UNENCRYPTED"))
108 psql.allow_clear = true;
110 else if (lex_match_id (lexer, "SQL"))
112 lex_match (lexer, '=');
113 if ( ! lex_force_string (lexer) )
116 ds_put_substring (&psql.sql, lex_tokstr (lexer)->ss);
121 struct dictionary *dict = NULL;
122 struct casereader *reader = psql_open_reader (&psql, &dict);
125 proc_set_active_file (ds, reader, dict);
128 ds_destroy (&psql.sql);
129 free (psql.conninfo);
135 ds_destroy (&psql.sql);
136 free (psql.conninfo);
142 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
144 struct gnumeric_read_info gri = {NULL, NULL, NULL, 1, true, -1};
146 lex_force_match (lexer, '/');
148 if (!lex_force_match_id (lexer, "FILE"))
151 lex_force_match (lexer, '=');
153 if (!lex_force_string (lexer))
156 gri.file_name = xstrdup (ds_cstr (lex_tokstr (lexer)));
160 while (lex_match (lexer, '/') )
162 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
164 lex_match (lexer, '=');
165 gri.asw = lex_integer (lexer);
167 else if (lex_match_id (lexer, "SHEET"))
169 lex_match (lexer, '=');
170 if (lex_match_id (lexer, "NAME"))
172 if ( ! lex_force_string (lexer) )
175 gri.sheet_name = xstrdup (ds_cstr (lex_tokstr (lexer)));
176 gri.sheet_index = -1;
178 else if (lex_match_id (lexer, "INDEX"))
180 gri.sheet_index = lex_integer (lexer);
185 else if (lex_match_id (lexer, "CELLRANGE"))
187 lex_match (lexer, '=');
189 if (lex_match_id (lexer, "FULL"))
191 gri.cell_range = NULL;
192 lex_put_back (lexer, T_ID);
194 else if (lex_match_id (lexer, "RANGE"))
196 if ( ! lex_force_string (lexer) )
199 gri.cell_range = xstrdup (ds_cstr (lex_tokstr (lexer)));
204 else if (lex_match_id (lexer, "READNAMES"))
206 lex_match (lexer, '=');
208 if ( lex_match_id (lexer, "ON"))
210 gri.read_names = true;
212 else if (lex_match_id (lexer, "OFF"))
214 gri.read_names = false;
218 lex_put_back (lexer, T_ID);
222 printf ("Unknown data file type \"\%s\"\n", lex_tokid (lexer));
229 struct dictionary *dict = NULL;
230 struct casereader *reader = gnumeric_open_reader (&gri, &dict);
233 proc_set_active_file (ds, reader, dict);
236 free (gri.file_name);
237 free (gri.sheet_name);
238 free (gri.cell_range);
243 free (gri.file_name);
244 free (gri.sheet_name);
245 free (gri.cell_range);
250 set_type (struct data_parser *parser, const char *subcommand,
251 enum data_parser_type type, bool *has_type)
255 data_parser_set_type (parser, type);
258 else if (type != data_parser_get_type (parser))
260 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
261 "was stated or implied earlier in this command."),
263 type == DP_FIXED ? "FIXED" : "DELIMITED",
264 type == DP_FIXED ? "DELIMITED" : "FIXED");
271 parse_get_txt (struct lexer *lexer, struct dataset *ds)
273 struct data_parser *parser = NULL;
274 struct dictionary *dict = dict_create ();
275 struct file_handle *fh = NULL;
276 struct dfm_reader *reader = NULL;
279 enum data_parser_type type;
282 lex_force_match (lexer, '/');
284 if (!lex_force_match_id (lexer, "FILE"))
286 lex_force_match (lexer, '=');
287 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
291 parser = data_parser_create (dict);
293 data_parser_set_type (parser, DP_DELIMITED);
294 data_parser_set_span (parser, false);
295 data_parser_set_quotes (parser, ss_empty ());
296 data_parser_set_empty_line_has_field (parser, true);
300 if (!lex_force_match (lexer, '/'))
303 if (lex_match_id (lexer, "ARRANGEMENT"))
307 lex_match (lexer, '=');
308 if (lex_match_id (lexer, "FIXED"))
309 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
310 else if (lex_match_id (lexer, "DELIMITED"))
311 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
312 DP_DELIMITED, &has_type);
315 lex_error (lexer, _("expecting FIXED or DELIMITED"));
321 else if (lex_match_id (lexer, "FIRSTCASE"))
323 lex_match (lexer, '=');
324 if (!lex_force_int (lexer))
326 if (lex_integer (lexer) < 1)
328 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
331 data_parser_set_skip (parser, lex_integer (lexer) - 1);
334 else if (lex_match_id_n (lexer, "DELCASE", 4))
336 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
338 lex_match (lexer, '=');
339 if (lex_match_id (lexer, "LINE"))
340 data_parser_set_span (parser, false);
341 else if (lex_match_id (lexer, "VARIABLES"))
343 data_parser_set_span (parser, true);
345 /* VARIABLES takes an integer argument, but for no
346 good reason. We just ignore it. */
347 if (!lex_force_int (lexer))
353 lex_error (lexer, _("expecting LINE or VARIABLES"));
357 else if (lex_match_id (lexer, "FIXCASE"))
359 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
361 lex_match (lexer, '=');
362 if (!lex_force_int (lexer))
364 if (lex_integer (lexer) < 1)
366 msg (SE, _("Value of FIXCASE must be at least 1."));
369 data_parser_set_records (parser, lex_integer (lexer));
372 else if (lex_match_id (lexer, "IMPORTCASES"))
374 lex_match (lexer, '=');
375 if (lex_match (lexer, T_ALL))
377 data_parser_set_case_limit (parser, -1);
378 data_parser_set_case_percent (parser, 100);
380 else if (lex_match_id (lexer, "FIRST"))
382 if (!lex_force_int (lexer))
384 if (lex_integer (lexer) < 1)
386 msg (SE, _("Value of FIRST must be at least 1."));
389 data_parser_set_case_limit (parser, lex_integer (lexer));
392 else if (lex_match_id (lexer, "PERCENT"))
394 if (!lex_force_int (lexer))
396 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
398 msg (SE, _("Value of PERCENT must be between 1 and 100."));
401 data_parser_set_case_percent (parser, lex_integer (lexer));
405 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
407 struct string hard_seps = DS_EMPTY_INITIALIZER;
408 const char *soft_seps = "";
412 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
414 lex_match (lexer, '=');
416 if (!lex_force_string (lexer))
419 s = ds_ss (lex_tokstr (lexer));
420 if (ss_match_string (&s, ss_cstr ("\\t")))
421 ds_put_cstr (&hard_seps, "\t");
422 if (ss_match_string (&s, ss_cstr ("\\\\")))
423 ds_put_cstr (&hard_seps, "\\");
424 while ((c = ss_get_char (&s)) != EOF)
428 ds_put_char (&hard_seps, c);
429 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
430 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
431 ds_destroy (&hard_seps);
435 else if (lex_match_id (lexer, "QUALIFIERS"))
437 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
439 lex_match (lexer, '=');
441 if (!lex_force_string (lexer))
444 if (settings_get_syntax () == COMPATIBLE
445 && ds_length (lex_tokstr (lexer)) != 1)
447 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
448 "must contain exactly one character."));
452 data_parser_set_quotes (parser, ds_ss (lex_tokstr (lexer)));
455 else if (settings_get_syntax () == ENHANCED
456 && lex_match_id (lexer, "ESCAPE"))
457 data_parser_set_quote_escape (parser, true);
458 else if (lex_match_id (lexer, "VARIABLES"))
462 lex_error (lexer, _("expecting VARIABLES"));
466 lex_match (lexer, '=');
470 type = data_parser_get_type (parser);
473 char name[VAR_NAME_LEN + 1];
474 struct fmt_spec input, output;
478 while (type == DP_FIXED && lex_match (lexer, '/'))
480 if (!lex_force_int (lexer))
482 if (lex_integer (lexer) < record)
484 msg (SE, _("The record number specified, %ld, is at or "
485 "before the previous record, %d. Data "
486 "fields must be listed in order of "
487 "increasing record number."),
488 lex_integer (lexer), record);
491 if (lex_integer (lexer) > data_parser_get_records (parser))
493 msg (SE, _("The record number specified, %ld, exceeds "
494 "the number of records per case specified "
496 lex_integer (lexer), data_parser_get_records (parser));
499 record = lex_integer (lexer);
503 if (!lex_force_id (lexer))
505 strcpy (name, lex_tokid (lexer));
508 if (type == DP_DELIMITED)
510 if (!parse_format_specifier (lexer, &input)
511 || !fmt_check_input (&input))
516 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
518 if (!parse_format_specifier_name (lexer, &input.type))
520 input.w = lc - fc + 1;
522 if (!fmt_check_input (&input))
525 output = fmt_for_output_from_input (&input);
527 v = dict_create_var (dict, name, fmt_var_width (&input));
530 msg (SE, _("%s is a duplicate variable name."), name);
533 var_set_both_formats (v, &output);
535 if (type == DP_DELIMITED)
536 data_parser_add_delimited_field (parser, &input,
537 var_get_case_index (v),
540 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
543 while (lex_token (lexer) != '.');
545 reader = dfm_open_reader (fh, lexer);
549 data_parser_make_active_file (parser, ds, reader, dict);
554 data_parser_destroy (parser);
557 return CMD_CASCADING_FAILURE;