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 <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>
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) (msgid)
41 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
42 static int parse_get_txt (struct lexer *lexer, struct dataset *);
43 static int parse_get_psql (struct lexer *lexer, struct dataset *);
46 cmd_get_data (struct lexer *lexer, struct dataset *ds)
48 lex_force_match (lexer, '/');
50 if (!lex_force_match_id (lexer, "TYPE"))
53 lex_force_match (lexer, '=');
55 if (lex_match_id (lexer, "GNM"))
56 return parse_get_gnm (lexer, ds);
57 else if (lex_match_id (lexer, "TXT"))
58 return parse_get_txt (lexer, ds);
59 else if (lex_match_id (lexer, "PSQL"))
60 return parse_get_psql (lexer, ds);
62 msg (SE, _("Unsupported TYPE %s"), lex_tokid (lexer));
67 parse_get_psql (struct lexer *lexer, struct dataset *ds)
69 struct psql_read_info psql;
70 psql.allow_clear = false;
74 ds_init_empty (&psql.sql);
76 lex_force_match (lexer, '/');
78 if (!lex_force_match_id (lexer, "CONNECT"))
81 lex_force_match (lexer, '=');
83 if (!lex_force_string (lexer))
86 psql.conninfo = strdup (ds_cstr (lex_tokstr (lexer)));
90 while (lex_match (lexer, '/') )
92 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
94 lex_match (lexer, '=');
95 psql.str_width = lex_integer (lexer);
98 else if ( lex_match_id (lexer, "BSIZE"))
100 lex_match (lexer, '=');
101 psql.bsize = lex_integer (lexer);
104 else if ( lex_match_id (lexer, "UNENCRYPTED"))
106 psql.allow_clear = true;
108 else if (lex_match_id (lexer, "SQL"))
110 lex_match (lexer, '=');
111 if ( ! lex_force_string (lexer) )
114 ds_put_substring (&psql.sql, lex_tokstr (lexer)->ss);
119 struct dictionary *dict = NULL;
120 struct casereader *reader = psql_open_reader (&psql, &dict);
123 proc_set_active_file (ds, reader, dict);
126 ds_destroy (&psql.sql);
127 free (psql.conninfo);
133 ds_destroy (&psql.sql);
134 free (psql.conninfo);
140 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
142 struct gnumeric_read_info gri = {NULL, NULL, NULL, 1, true, -1};
144 lex_force_match (lexer, '/');
146 if (!lex_force_match_id (lexer, "FILE"))
149 lex_force_match (lexer, '=');
151 if (!lex_force_string (lexer))
154 gri.file_name = strdup (ds_cstr (lex_tokstr (lexer)));
158 while (lex_match (lexer, '/') )
160 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
162 lex_match (lexer, '=');
163 gri.asw = lex_integer (lexer);
165 else if (lex_match_id (lexer, "SHEET"))
167 lex_match (lexer, '=');
168 if (lex_match_id (lexer, "NAME"))
170 if ( ! lex_force_string (lexer) )
173 gri.sheet_name = strdup (ds_cstr (lex_tokstr (lexer)));
174 gri.sheet_index = -1;
176 else if (lex_match_id (lexer, "INDEX"))
178 gri.sheet_index = lex_integer (lexer);
183 else if (lex_match_id (lexer, "CELLRANGE"))
185 lex_match (lexer, '=');
187 if (lex_match_id (lexer, "FULL"))
189 gri.cell_range = NULL;
190 lex_put_back (lexer, T_ID);
192 else if (lex_match_id (lexer, "RANGE"))
194 if ( ! lex_force_string (lexer) )
197 gri.cell_range = strdup (ds_cstr (lex_tokstr (lexer)));
202 else if (lex_match_id (lexer, "READNAMES"))
204 lex_match (lexer, '=');
206 if ( lex_match_id (lexer, "ON"))
208 gri.read_names = true;
210 else if (lex_match_id (lexer, "OFF"))
212 gri.read_names = false;
216 lex_put_back (lexer, T_ID);
220 printf ("Unknown data file type \"\%s\"\n", lex_tokid (lexer));
227 struct dictionary *dict = NULL;
228 struct casereader *reader = gnumeric_open_reader (&gri, &dict);
231 proc_set_active_file (ds, reader, dict);
234 free (gri.file_name);
235 free (gri.sheet_name);
236 free (gri.cell_range);
241 free (gri.file_name);
242 free (gri.sheet_name);
243 free (gri.cell_range);
248 set_type (struct data_parser *parser, const char *subcommand,
249 enum data_parser_type type, bool *has_type)
253 data_parser_set_type (parser, type);
256 else if (type != data_parser_get_type (parser))
258 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
259 "was stated or implied earlier in this command."),
261 type == DP_FIXED ? "FIXED" : "DELIMITED",
262 type == DP_FIXED ? "DELIMITED" : "FIXED");
269 parse_get_txt (struct lexer *lexer, struct dataset *ds)
271 struct data_parser *parser = NULL;
272 struct dictionary *dict = NULL;
273 struct file_handle *fh = NULL;
274 struct dfm_reader *reader = NULL;
277 enum data_parser_type type;
280 lex_force_match (lexer, '/');
282 if (!lex_force_match_id (lexer, "FILE"))
284 lex_force_match (lexer, '=');
285 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
289 parser = data_parser_create ();
291 data_parser_set_type (parser, DP_DELIMITED);
292 data_parser_set_span (parser, false);
293 data_parser_set_quotes (parser, ss_empty ());
294 data_parser_set_empty_line_has_field (parser, true);
298 if (!lex_force_match (lexer, '/'))
301 if (lex_match_id (lexer, "ARRANGEMENT"))
305 lex_match (lexer, '=');
306 if (lex_match_id (lexer, "FIXED"))
307 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
308 else if (lex_match_id (lexer, "DELIMITED"))
309 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
310 DP_DELIMITED, &has_type);
313 lex_error (lexer, _("expecting FIXED or DELIMITED"));
319 else if (lex_match_id (lexer, "FIRSTCASE"))
321 lex_match (lexer, '=');
322 if (!lex_force_int (lexer))
324 if (lex_integer (lexer) < 1)
326 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
329 data_parser_set_skip (parser, lex_integer (lexer) - 1);
332 else if (lex_match_id_n (lexer, "DELCASE", 4))
334 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
336 lex_match (lexer, '=');
337 if (lex_match_id (lexer, "LINE"))
338 data_parser_set_span (parser, false);
339 else if (lex_match_id (lexer, "VARIABLES"))
341 data_parser_set_span (parser, true);
343 /* VARIABLES takes an integer argument, but for no
344 good reason. We just ignore it. */
345 if (!lex_force_int (lexer))
351 lex_error (lexer, _("expecting LINE or VARIABLES"));
355 else if (lex_match_id (lexer, "FIXCASE"))
357 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
359 lex_match (lexer, '=');
360 if (!lex_force_int (lexer))
362 if (lex_integer (lexer) < 1)
364 msg (SE, _("Value of FIXCASE must be at least 1."));
367 data_parser_set_records (parser, lex_integer (lexer));
370 else if (lex_match_id (lexer, "IMPORTCASES"))
372 lex_match (lexer, '=');
373 if (lex_match (lexer, T_ALL))
375 data_parser_set_case_limit (parser, -1);
376 data_parser_set_case_percent (parser, 100);
378 else if (lex_match_id (lexer, "FIRST"))
380 if (!lex_force_int (lexer))
382 if (lex_integer (lexer) < 1)
384 msg (SE, _("Value of FIRST must be at least 1."));
387 data_parser_set_case_limit (parser, lex_integer (lexer));
390 else if (lex_match_id (lexer, "PERCENT"))
392 if (!lex_force_int (lexer))
394 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
396 msg (SE, _("Value of PERCENT must be between 1 and 100."));
399 data_parser_set_case_percent (parser, lex_integer (lexer));
403 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
405 struct string hard_seps = DS_EMPTY_INITIALIZER;
406 const char *soft_seps = "";
410 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
412 lex_match (lexer, '=');
414 if (!lex_force_string (lexer))
417 s = ds_ss (lex_tokstr (lexer));
418 if (ss_match_string (&s, ss_cstr ("\\t")))
419 ds_put_cstr (&hard_seps, "\t");
420 if (ss_match_string (&s, ss_cstr ("\\\\")))
421 ds_put_cstr (&hard_seps, "\\");
422 while ((c = ss_get_char (&s)) != EOF)
426 ds_put_char (&hard_seps, c);
427 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
428 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
429 ds_destroy (&hard_seps);
433 else if (lex_match_id (lexer, "QUALIFIERS"))
435 if (!set_type (parser, "QUALIFIERS", DP_DELIMITED, &has_type))
437 lex_match (lexer, '=');
439 if (!lex_force_string (lexer))
442 if (settings_get_syntax () == COMPATIBLE
443 && ds_length (lex_tokstr (lexer)) != 1)
445 msg (SE, _("In compatible syntax mode, the QUALIFIER string "
446 "must contain exactly one character."));
450 data_parser_set_quotes (parser, ds_ss (lex_tokstr (lexer)));
453 else if (settings_get_syntax () == ENHANCED
454 && lex_match_id (lexer, "ESCAPE"))
455 data_parser_set_quote_escape (parser, true);
456 else if (lex_match_id (lexer, "VARIABLES"))
460 lex_error (lexer, _("expecting VARIABLES"));
464 lex_match (lexer, '=');
466 dict = dict_create ();
468 type = data_parser_get_type (parser);
471 char name[VAR_NAME_LEN + 1];
472 struct fmt_spec input, output;
476 while (type == DP_FIXED && lex_match (lexer, '/'))
478 if (!lex_force_int (lexer))
480 if (lex_integer (lexer) < record)
482 msg (SE, _("The record number specified, %ld, is at or "
483 "before the previous record, %d. Data "
484 "fields must be listed in order of "
485 "increasing record number."),
486 lex_integer (lexer), record);
489 if (lex_integer (lexer) > data_parser_get_records (parser))
491 msg (SE, _("The record number specified, %ld, exceeds "
492 "the number of records per case specified "
494 lex_integer (lexer), data_parser_get_records (parser));
497 record = lex_integer (lexer);
501 if (!lex_force_id (lexer))
503 strcpy (name, lex_tokid (lexer));
506 if (type == DP_DELIMITED)
508 if (!parse_format_specifier (lexer, &input)
509 || !fmt_check_input (&input))
514 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
516 if (!parse_format_specifier_name (lexer, &input.type))
518 input.w = lc - fc + 1;
520 if (!fmt_check_input (&input))
523 output = fmt_for_output_from_input (&input);
525 v = dict_create_var (dict, name, fmt_var_width (&input));
528 msg (SE, _("%s is a duplicate variable name."), name);
531 var_set_both_formats (v, &output);
533 if (type == DP_DELIMITED)
534 data_parser_add_delimited_field (parser, &input,
535 var_get_case_index (v),
538 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
541 while (lex_token (lexer) != '.');
543 reader = dfm_open_reader (fh, lexer);
547 data_parser_make_active_file (parser, ds, reader, dict);
552 data_parser_destroy (parser);
555 return CMD_CASCADING_FAILURE;