1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008 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 <language/command.h>
28 #include <language/data-io/data-parser.h>
29 #include <language/data-io/data-reader.h>
30 #include <language/data-io/file-handle.h>
31 #include <language/data-io/placement-parser.h>
32 #include <language/lexer/format-parser.h>
33 #include <language/lexer/lexer.h>
34 #include <libpspp/message.h>
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) (msgid)
40 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
41 static int parse_get_txt (struct lexer *lexer, struct dataset *);
42 static int parse_get_psql (struct lexer *lexer, struct dataset *);
45 cmd_get_data (struct lexer *lexer, struct dataset *ds)
47 lex_force_match (lexer, '/');
49 if (!lex_force_match_id (lexer, "TYPE"))
52 lex_force_match (lexer, '=');
54 if (lex_match_id (lexer, "GNM"))
55 return parse_get_gnm (lexer, ds);
56 else if (lex_match_id (lexer, "TXT"))
57 return parse_get_txt (lexer, ds);
58 else if (lex_match_id (lexer, "PSQL"))
59 return parse_get_psql (lexer, ds);
61 msg (SE, _("Unsupported TYPE %s"), lex_tokid (lexer));
66 parse_get_psql (struct lexer *lexer, struct dataset *ds)
68 struct psql_read_info psql;
69 psql.allow_clear = false;
73 ds_init_empty (&psql.sql);
75 lex_force_match (lexer, '/');
77 if (!lex_force_match_id (lexer, "CONNECT"))
80 lex_force_match (lexer, '=');
82 if (!lex_force_string (lexer))
85 psql.conninfo = strdup (ds_cstr (lex_tokstr (lexer)));
89 while (lex_match (lexer, '/') )
91 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
93 lex_match (lexer, '=');
94 psql.str_width = lex_integer (lexer);
97 else if ( lex_match_id (lexer, "BSIZE"))
99 lex_match (lexer, '=');
100 psql.bsize = lex_integer (lexer);
103 else if ( lex_match_id (lexer, "UNENCRYPTED"))
105 psql.allow_clear = true;
107 else if (lex_match_id (lexer, "SQL"))
109 lex_match (lexer, '=');
110 if ( ! lex_force_string (lexer) )
113 ds_put_substring (&psql.sql, lex_tokstr (lexer)->ss);
118 struct dictionary *dict = NULL;
119 struct casereader *reader = psql_open_reader (&psql, &dict);
122 proc_set_active_file (ds, reader, dict);
125 ds_destroy (&psql.sql);
126 free (psql.conninfo);
132 ds_destroy (&psql.sql);
133 free (psql.conninfo);
139 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
141 struct gnumeric_read_info gri = {NULL, NULL, NULL, 1, true, -1};
143 lex_force_match (lexer, '/');
145 if (!lex_force_match_id (lexer, "FILE"))
148 lex_force_match (lexer, '=');
150 if (!lex_force_string (lexer))
153 gri.file_name = strdup (ds_cstr (lex_tokstr (lexer)));
157 while (lex_match (lexer, '/') )
159 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
161 lex_match (lexer, '=');
162 gri.asw = lex_integer (lexer);
164 else if (lex_match_id (lexer, "SHEET"))
166 lex_match (lexer, '=');
167 if (lex_match_id (lexer, "NAME"))
169 if ( ! lex_force_string (lexer) )
172 gri.sheet_name = strdup (ds_cstr (lex_tokstr (lexer)));
173 gri.sheet_index = -1;
175 else if (lex_match_id (lexer, "INDEX"))
177 gri.sheet_index = lex_integer (lexer);
182 else if (lex_match_id (lexer, "CELLRANGE"))
184 lex_match (lexer, '=');
186 if (lex_match_id (lexer, "FULL"))
188 gri.cell_range = NULL;
189 lex_put_back (lexer, T_ID);
191 else if (lex_match_id (lexer, "RANGE"))
193 if ( ! lex_force_string (lexer) )
196 gri.cell_range = strdup (ds_cstr (lex_tokstr (lexer)));
201 else if (lex_match_id (lexer, "READNAMES"))
203 lex_match (lexer, '=');
205 if ( lex_match_id (lexer, "ON"))
207 gri.read_names = true;
209 else if (lex_match_id (lexer, "OFF"))
211 gri.read_names = false;
215 lex_put_back (lexer, T_ID);
219 printf ("Unknown data file type \"\%s\"\n", lex_tokid (lexer));
226 struct dictionary *dict = NULL;
227 struct casereader *reader = gnumeric_open_reader (&gri, &dict);
230 proc_set_active_file (ds, reader, dict);
233 free (gri.file_name);
234 free (gri.sheet_name);
235 free (gri.cell_range);
240 free (gri.file_name);
241 free (gri.sheet_name);
242 free (gri.cell_range);
247 set_type (struct data_parser *parser, const char *subcommand,
248 enum data_parser_type type, bool *has_type)
252 data_parser_set_type (parser, type);
255 else if (type != data_parser_get_type (parser))
257 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
258 "was stated or implied earlier in this command."),
260 type == DP_FIXED ? "FIXED" : "DELIMITED",
261 type == DP_FIXED ? "DELIMITED" : "FIXED");
268 parse_get_txt (struct lexer *lexer, struct dataset *ds)
270 struct data_parser *parser = NULL;
271 struct dictionary *dict = NULL;
272 struct file_handle *fh = NULL;
273 struct dfm_reader *reader = NULL;
276 enum data_parser_type type;
279 lex_force_match (lexer, '/');
281 if (!lex_force_match_id (lexer, "FILE"))
283 lex_force_match (lexer, '=');
284 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
288 parser = data_parser_create ();
290 data_parser_set_type (parser, DP_DELIMITED);
291 data_parser_set_span (parser, false);
292 data_parser_set_quotes (parser, ss_empty ());
293 data_parser_set_empty_line_has_field (parser, true);
297 if (!lex_force_match (lexer, '/'))
300 if (lex_match_id (lexer, "ARRANGEMENT"))
304 lex_match (lexer, '=');
305 if (lex_match_id (lexer, "FIXED"))
306 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
307 else if (lex_match_id (lexer, "DELIMITED"))
308 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
309 DP_DELIMITED, &has_type);
312 lex_error (lexer, _("expecting FIXED or DELIMITED"));
318 else if (lex_match_id (lexer, "FIRSTCASE"))
320 lex_match (lexer, '=');
321 if (!lex_force_int (lexer))
323 if (lex_integer (lexer) < 1)
325 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
328 data_parser_set_skip (parser, lex_integer (lexer) - 1);
331 else if (lex_match_id_n (lexer, "DELCASE", 4))
333 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
335 lex_match (lexer, '=');
336 if (lex_match_id (lexer, "LINE"))
337 data_parser_set_span (parser, false);
338 else if (lex_match_id (lexer, "VARIABLES"))
340 data_parser_set_span (parser, true);
342 /* VARIABLES takes an integer argument, but for no
343 good reason. We just ignore it. */
344 if (!lex_force_int (lexer))
350 lex_error (lexer, _("expecting LINE or VARIABLES"));
354 else if (lex_match_id (lexer, "FIXCASE"))
356 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
358 lex_match (lexer, '=');
359 if (!lex_force_int (lexer))
361 if (lex_integer (lexer) < 1)
363 msg (SE, _("Value of FIXCASE must be at least 1."));
366 data_parser_set_records (parser, lex_integer (lexer));
369 else if (lex_match_id (lexer, "IMPORTCASES"))
371 lex_match (lexer, '=');
372 if (lex_match (lexer, T_ALL))
374 data_parser_set_case_limit (parser, -1);
375 data_parser_set_case_percent (parser, 100);
377 else if (lex_match_id (lexer, "FIRST"))
379 if (!lex_force_int (lexer))
381 if (lex_integer (lexer) < 1)
383 msg (SE, _("Value of FIRST must be at least 1."));
386 data_parser_set_case_limit (parser, lex_integer (lexer));
389 else if (lex_match_id (lexer, "PERCENT"))
391 if (!lex_force_int (lexer))
393 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
395 msg (SE, _("Value of PERCENT must be between 1 and 100."));
398 data_parser_set_case_percent (parser, lex_integer (lexer));
402 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
404 struct string hard_seps = DS_EMPTY_INITIALIZER;
405 const char *soft_seps = "";
409 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
411 lex_match (lexer, '=');
413 if (!lex_force_string (lexer))
416 s = ds_ss (lex_tokstr (lexer));
417 if (ss_match_string (&s, ss_cstr ("\\t")))
418 ds_put_cstr (&hard_seps, "\t");
419 if (ss_match_string (&s, ss_cstr ("\\\\")))
420 ds_put_cstr (&hard_seps, "\\");
421 while ((c = ss_get_char (&s)) != EOF)
425 ds_put_char (&hard_seps, c);
426 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
427 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
428 ds_destroy (&hard_seps);
432 else if (lex_match_id (lexer, "QUALIFIER"))
434 if (!set_type (parser, "QUALIFIER", DP_DELIMITED, &has_type))
436 lex_match (lexer, '=');
438 if (!lex_force_string (lexer))
441 data_parser_set_quotes (parser, ds_ss (lex_tokstr (lexer)));
444 else if (lex_match_id (lexer, "VARIABLES"))
448 lex_error (lexer, _("expecting VARIABLES"));
452 lex_match (lexer, '=');
454 dict = dict_create ();
456 type = data_parser_get_type (parser);
459 char name[VAR_NAME_LEN + 1];
460 struct fmt_spec input, output;
464 while (type == DP_FIXED && lex_match (lexer, '/'))
466 if (!lex_force_int (lexer))
468 if (lex_integer (lexer) < record)
470 msg (SE, _("The record number specified, %ld, is at or "
471 "before the previous record, %d. Data "
472 "fields must be listed in order of "
473 "increasing record number."),
474 lex_integer (lexer), record);
477 if (lex_integer (lexer) > data_parser_get_records (parser))
479 msg (SE, _("The record number specified, %ld, exceeds "
480 "the number of records per case specified "
482 lex_integer (lexer), data_parser_get_records (parser));
485 record = lex_integer (lexer);
489 if (!lex_force_id (lexer))
491 strcpy (name, lex_tokid (lexer));
494 if (type == DP_DELIMITED)
496 if (!parse_format_specifier (lexer, &input)
497 || !fmt_check_input (&input))
502 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
504 if (!parse_format_specifier_name (lexer, &input.type))
506 input.w = lc - fc + 1;
508 if (!fmt_check_input (&input))
511 output = fmt_for_output_from_input (&input);
513 v = dict_create_var (dict, name, fmt_var_width (&input));
516 msg (SE, _("%s is a duplicate variable name."), name);
519 var_set_both_formats (v, &output);
521 if (type == DP_DELIMITED)
522 data_parser_add_delimited_field (parser, &input,
523 var_get_case_index (v),
526 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
529 while (lex_token (lexer) != '.');
531 reader = dfm_open_reader (fh, lexer);
535 data_parser_make_active_file (parser, ds, reader, dict);
540 data_parser_destroy (parser);
543 return CMD_CASCADING_FAILURE;