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;
72 ds_init_empty (&psql.sql);
74 lex_force_match (lexer, '/');
76 if (!lex_force_match_id (lexer, "CONNECT"))
79 lex_force_match (lexer, '=');
81 if (!lex_force_string (lexer))
84 psql.conninfo = strdup (ds_cstr (lex_tokstr (lexer)));
88 while (lex_match (lexer, '/') )
90 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
92 lex_match (lexer, '=');
93 psql.str_width = lex_integer (lexer);
96 else if ( lex_match_id (lexer, "UNENCRYPTED"))
98 psql.allow_clear = true;
100 else if (lex_match_id (lexer, "SQL"))
102 lex_match (lexer, '=');
103 if ( ! lex_force_string (lexer) )
106 ds_put_substring (&psql.sql, lex_tokstr (lexer)->ss);
111 struct dictionary *dict = NULL;
112 struct casereader *reader = psql_open_reader (&psql, &dict);
115 proc_set_active_file (ds, reader, dict);
118 ds_destroy (&psql.sql);
119 free (psql.conninfo);
125 ds_destroy (&psql.sql);
126 free (psql.conninfo);
132 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
134 struct gnumeric_read_info gri = {NULL, NULL, NULL, 1, true, -1};
136 lex_force_match (lexer, '/');
138 if (!lex_force_match_id (lexer, "FILE"))
141 lex_force_match (lexer, '=');
143 if (!lex_force_string (lexer))
146 gri.file_name = strdup (ds_cstr (lex_tokstr (lexer)));
150 while (lex_match (lexer, '/') )
152 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
154 lex_match (lexer, '=');
155 gri.asw = lex_integer (lexer);
157 else if (lex_match_id (lexer, "SHEET"))
159 lex_match (lexer, '=');
160 if (lex_match_id (lexer, "NAME"))
162 if ( ! lex_force_string (lexer) )
165 gri.sheet_name = strdup (ds_cstr (lex_tokstr (lexer)));
166 gri.sheet_index = -1;
168 else if (lex_match_id (lexer, "INDEX"))
170 gri.sheet_index = lex_integer (lexer);
175 else if (lex_match_id (lexer, "CELLRANGE"))
177 lex_match (lexer, '=');
179 if (lex_match_id (lexer, "FULL"))
181 gri.cell_range = NULL;
182 lex_put_back (lexer, T_ID);
184 else if (lex_match_id (lexer, "RANGE"))
186 if ( ! lex_force_string (lexer) )
189 gri.cell_range = strdup (ds_cstr (lex_tokstr (lexer)));
194 else if (lex_match_id (lexer, "READNAMES"))
196 lex_match (lexer, '=');
198 if ( lex_match_id (lexer, "ON"))
200 gri.read_names = true;
202 else if (lex_match_id (lexer, "OFF"))
204 gri.read_names = false;
208 lex_put_back (lexer, T_ID);
212 printf ("Unknown data file type \"\%s\"\n", lex_tokid (lexer));
219 struct dictionary *dict = NULL;
220 struct casereader *reader = gnumeric_open_reader (&gri, &dict);
223 proc_set_active_file (ds, reader, dict);
226 free (gri.file_name);
227 free (gri.sheet_name);
228 free (gri.cell_range);
233 free (gri.file_name);
234 free (gri.sheet_name);
235 free (gri.cell_range);
240 set_type (struct data_parser *parser, const char *subcommand,
241 enum data_parser_type type, bool *has_type)
245 data_parser_set_type (parser, type);
248 else if (type != data_parser_get_type (parser))
250 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
251 "was stated or implied earlier in this command."),
253 type == DP_FIXED ? "FIXED" : "DELIMITED",
254 type == DP_FIXED ? "DELIMITED" : "FIXED");
261 parse_get_txt (struct lexer *lexer, struct dataset *ds)
263 struct data_parser *parser = NULL;
264 struct dictionary *dict = NULL;
265 struct file_handle *fh = NULL;
266 struct dfm_reader *reader = NULL;
269 enum data_parser_type type;
272 lex_force_match (lexer, '/');
274 if (!lex_force_match_id (lexer, "FILE"))
276 lex_force_match (lexer, '=');
277 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
281 parser = data_parser_create ();
283 data_parser_set_type (parser, DP_DELIMITED);
284 data_parser_set_span (parser, false);
285 data_parser_set_quotes (parser, ss_empty ());
286 data_parser_set_empty_line_has_field (parser, true);
290 if (!lex_force_match (lexer, '/'))
293 if (lex_match_id (lexer, "ARRANGEMENT"))
297 lex_match (lexer, '=');
298 if (lex_match_id (lexer, "FIXED"))
299 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
300 else if (lex_match_id (lexer, "DELIMITED"))
301 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
302 DP_DELIMITED, &has_type);
305 lex_error (lexer, _("expecting FIXED or DELIMITED"));
311 else if (lex_match_id (lexer, "FIRSTCASE"))
313 lex_match (lexer, '=');
314 if (!lex_force_int (lexer))
316 if (lex_integer (lexer) < 1)
318 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
321 data_parser_set_skip (parser, lex_integer (lexer) - 1);
324 else if (lex_match_id_n (lexer, "DELCASE", 4))
326 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
328 lex_match (lexer, '=');
329 if (lex_match_id (lexer, "LINE"))
330 data_parser_set_span (parser, false);
331 else if (lex_match_id (lexer, "VARIABLES"))
333 data_parser_set_span (parser, true);
335 /* VARIABLES takes an integer argument, but for no
336 good reason. We just ignore it. */
337 if (!lex_force_int (lexer))
343 lex_error (lexer, _("expecting LINE or VARIABLES"));
347 else if (lex_match_id (lexer, "FIXCASE"))
349 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
351 lex_match (lexer, '=');
352 if (!lex_force_int (lexer))
354 if (lex_integer (lexer) < 1)
356 msg (SE, _("Value of FIXCASE must be at least 1."));
359 data_parser_set_records (parser, lex_integer (lexer));
362 else if (lex_match_id (lexer, "IMPORTCASES"))
364 lex_match (lexer, '=');
365 if (lex_match (lexer, T_ALL))
367 data_parser_set_case_limit (parser, -1);
368 data_parser_set_case_percent (parser, 100);
370 else if (lex_match_id (lexer, "FIRST"))
372 if (!lex_force_int (lexer))
374 if (lex_integer (lexer) < 1)
376 msg (SE, _("Value of FIRST must be at least 1."));
379 data_parser_set_case_limit (parser, lex_integer (lexer));
382 else if (lex_match_id (lexer, "PERCENT"))
384 if (!lex_force_int (lexer))
386 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
388 msg (SE, _("Value of PERCENT must be between 1 and 100."));
391 data_parser_set_case_percent (parser, lex_integer (lexer));
395 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
397 struct string hard_seps = DS_EMPTY_INITIALIZER;
398 const char *soft_seps = "";
402 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
404 lex_match (lexer, '=');
406 if (!lex_force_string (lexer))
409 s = ds_ss (lex_tokstr (lexer));
410 if (ss_match_string (&s, ss_cstr ("\\t")))
411 ds_put_cstr (&hard_seps, "\t");
412 if (ss_match_string (&s, ss_cstr ("\\\\")))
413 ds_put_cstr (&hard_seps, "\\");
414 while ((c = ss_get_char (&s)) != EOF)
418 ds_put_char (&hard_seps, c);
419 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
420 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
421 ds_destroy (&hard_seps);
425 else if (lex_match_id (lexer, "QUALIFIER"))
427 if (!set_type (parser, "QUALIFIER", DP_DELIMITED, &has_type))
429 lex_match (lexer, '=');
431 if (!lex_force_string (lexer))
434 data_parser_set_quotes (parser, ds_ss (lex_tokstr (lexer)));
437 else if (lex_match_id (lexer, "VARIABLES"))
441 lex_error (lexer, _("expecting VARIABLES"));
445 lex_match (lexer, '=');
447 dict = dict_create ();
449 type = data_parser_get_type (parser);
452 char name[VAR_NAME_LEN + 1];
453 struct fmt_spec input, output;
457 while (type == DP_FIXED && lex_match (lexer, '/'))
459 if (!lex_force_int (lexer))
461 if (lex_integer (lexer) < record)
463 msg (SE, _("The record number specified, %ld, is at or "
464 "before the previous record, %d. Data "
465 "fields must be listed in order of "
466 "increasing record number."),
467 lex_integer (lexer), record);
470 if (lex_integer (lexer) > data_parser_get_records (parser))
472 msg (SE, _("The record number specified, %ld, exceeds "
473 "the number of records per case specified "
475 lex_integer (lexer), data_parser_get_records (parser));
478 record = lex_integer (lexer);
482 if (!lex_force_id (lexer))
484 strcpy (name, lex_tokid (lexer));
487 if (type == DP_DELIMITED)
489 if (!parse_format_specifier (lexer, &input)
490 || !fmt_check_input (&input))
495 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
497 if (!parse_format_specifier_name (lexer, &input.type))
499 input.w = lc - fc + 1;
501 if (!fmt_check_input (&input))
504 output = fmt_for_output_from_input (&input);
506 v = dict_create_var (dict, name, fmt_var_width (&input));
509 msg (SE, _("%s is a duplicate variable name."), name);
512 var_set_both_formats (v, &output);
514 if (type == DP_DELIMITED)
515 data_parser_add_delimited_field (parser, &input,
516 var_get_case_index (v),
519 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
522 while (lex_token (lexer) != '.');
524 reader = dfm_open_reader (fh, lexer);
528 data_parser_make_active_file (parser, ds, reader, dict);
533 data_parser_destroy (parser);
536 return CMD_CASCADING_FAILURE;