1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007 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>
23 #include <data/dictionary.h>
24 #include <data/format.h>
25 #include <data/procedure.h>
26 #include <language/command.h>
27 #include <language/data-io/data-parser.h>
28 #include <language/data-io/data-reader.h>
29 #include <language/data-io/file-handle.h>
30 #include <language/data-io/placement-parser.h>
31 #include <language/lexer/format-parser.h>
32 #include <language/lexer/lexer.h>
33 #include <libpspp/message.h>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) (msgid)
39 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
40 static int parse_get_txt (struct lexer *lexer, struct dataset *);
43 cmd_get_data (struct lexer *lexer, struct dataset *ds)
45 lex_force_match (lexer, '/');
47 if (!lex_force_match_id (lexer, "TYPE"))
50 lex_force_match (lexer, '=');
52 if (lex_match_id (lexer, "GNM"))
53 return parse_get_gnm (lexer, ds);
54 else if (lex_match_id (lexer, "TXT"))
55 return parse_get_txt (lexer, ds);
57 msg (SE, _("Unsupported TYPE %s"), lex_tokid (lexer));
63 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
65 struct gnumeric_read_info gri = {NULL, NULL, NULL, 1, true, -1};
67 lex_force_match (lexer, '/');
69 if (!lex_force_match_id (lexer, "FILE"))
72 lex_force_match (lexer, '=');
74 if (!lex_force_string (lexer))
77 gri.file_name = strdup (ds_cstr (lex_tokstr (lexer)));
81 while (lex_match (lexer, '/') )
83 if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
85 lex_match (lexer, '=');
86 gri.asw = lex_integer (lexer);
88 else if (lex_match_id (lexer, "SHEET"))
90 lex_match (lexer, '=');
91 if (lex_match_id (lexer, "NAME"))
93 if ( ! lex_force_string (lexer) )
96 gri.sheet_name = strdup (ds_cstr (lex_tokstr (lexer)));
99 else if (lex_match_id (lexer, "INDEX"))
101 gri.sheet_index = lex_integer (lexer);
106 else if (lex_match_id (lexer, "CELLRANGE"))
108 lex_match (lexer, '=');
110 if (lex_match_id (lexer, "FULL"))
112 gri.cell_range = NULL;
113 lex_put_back (lexer, T_ID);
115 else if (lex_match_id (lexer, "RANGE"))
117 if ( ! lex_force_string (lexer) )
120 gri.cell_range = strdup (ds_cstr (lex_tokstr (lexer)));
125 else if (lex_match_id (lexer, "READNAMES"))
127 lex_match (lexer, '=');
129 if ( lex_match_id (lexer, "ON"))
131 gri.read_names = true;
133 else if (lex_match_id (lexer, "OFF"))
135 gri.read_names = false;
139 lex_put_back (lexer, T_ID);
143 printf ("Unknown data file type \"\%s\"\n", lex_tokid (lexer));
150 struct dictionary *dict = NULL;
151 struct casereader *reader = gnumeric_open_reader (&gri, &dict);
154 proc_set_active_file (ds, reader, dict);
157 free (gri.file_name);
158 free (gri.sheet_name);
159 free (gri.cell_range);
164 free (gri.file_name);
165 free (gri.sheet_name);
166 free (gri.cell_range);
171 set_type (struct data_parser *parser, const char *subcommand,
172 enum data_parser_type type, bool *has_type)
176 data_parser_set_type (parser, type);
179 else if (type != data_parser_get_type (parser))
181 msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
182 "was stated or implied earlier in this command."),
184 type == DP_FIXED ? "FIXED" : "DELIMITED",
185 type == DP_FIXED ? "DELIMITED" : "FIXED");
192 parse_get_txt (struct lexer *lexer, struct dataset *ds)
194 struct data_parser *parser = NULL;
195 struct dictionary *dict = NULL;
196 struct file_handle *fh = NULL;
197 struct dfm_reader *reader = NULL;
200 enum data_parser_type type;
203 lex_force_match (lexer, '/');
205 if (!lex_force_match_id (lexer, "FILE"))
207 lex_force_match (lexer, '=');
208 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
212 parser = data_parser_create ();
214 data_parser_set_type (parser, DP_DELIMITED);
215 data_parser_set_span (parser, false);
216 data_parser_set_quotes (parser, ss_empty ());
217 data_parser_set_empty_line_has_field (parser, true);
221 if (!lex_force_match (lexer, '/'))
224 if (lex_match_id (lexer, "ARRANGEMENT"))
228 lex_match (lexer, '=');
229 if (lex_match_id (lexer, "FIXED"))
230 ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
231 else if (lex_match_id (lexer, "DELIMITED"))
232 ok = set_type (parser, "ARRANGEMENT=DELIMITED",
233 DP_DELIMITED, &has_type);
236 lex_error (lexer, _("expecting FIXED or DELIMITED"));
242 else if (lex_match_id (lexer, "FIRSTCASE"))
244 lex_match (lexer, '=');
245 if (!lex_force_int (lexer))
247 if (lex_integer (lexer) < 1)
249 msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
252 data_parser_set_skip (parser, lex_integer (lexer) - 1);
255 else if (lex_match_id_n (lexer, "DELCASE", 4))
257 if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
259 lex_match (lexer, '=');
260 if (lex_match_id (lexer, "LINE"))
261 data_parser_set_span (parser, false);
262 else if (lex_match_id (lexer, "VARIABLES"))
264 data_parser_set_span (parser, true);
266 /* VARIABLES takes an integer argument, but for no
267 good reason. We just ignore it. */
268 if (!lex_force_int (lexer))
274 lex_error (lexer, _("expecting LINE or VARIABLES"));
278 else if (lex_match_id (lexer, "FIXCASE"))
280 if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
282 lex_match (lexer, '=');
283 if (!lex_force_int (lexer))
285 if (lex_integer (lexer) < 1)
287 msg (SE, _("Value of FIXCASE must be at least 1."));
290 data_parser_set_records (parser, lex_integer (lexer));
293 else if (lex_match_id (lexer, "IMPORTCASES"))
295 lex_match (lexer, '=');
296 if (lex_match (lexer, T_ALL))
298 data_parser_set_case_limit (parser, -1);
299 data_parser_set_case_percent (parser, 100);
301 else if (lex_match_id (lexer, "FIRST"))
303 if (!lex_force_int (lexer))
305 if (lex_integer (lexer) < 1)
307 msg (SE, _("Value of FIRST must be at least 1."));
310 data_parser_set_case_limit (parser, lex_integer (lexer));
313 else if (lex_match_id (lexer, "PERCENT"))
315 if (!lex_force_int (lexer))
317 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
319 msg (SE, _("Value of PERCENT must be between 1 and 100."));
322 data_parser_set_case_percent (parser, lex_integer (lexer));
326 else if (lex_match_id_n (lexer, "DELIMITERS", 4))
328 struct string hard_seps = DS_EMPTY_INITIALIZER;
329 const char *soft_seps = "";
333 if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
335 lex_match (lexer, '=');
337 if (!lex_force_string (lexer))
340 s = ds_ss (lex_tokstr (lexer));
341 if (ss_match_string (&s, ss_cstr ("\\t")))
342 ds_put_cstr (&hard_seps, "\t");
343 if (ss_match_string (&s, ss_cstr ("\\\\")))
344 ds_put_cstr (&hard_seps, "\\");
345 while ((c = ss_get_char (&s)) != EOF)
349 ds_put_char (&hard_seps, c);
350 data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
351 data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
352 ds_destroy (&hard_seps);
356 else if (lex_match_id (lexer, "QUALIFIER"))
358 if (!set_type (parser, "QUALIFIER", DP_DELIMITED, &has_type))
360 lex_match (lexer, '=');
362 if (!lex_force_string (lexer))
365 data_parser_set_quotes (parser, ds_ss (lex_tokstr (lexer)));
368 else if (lex_match_id (lexer, "VARIABLES"))
372 lex_error (lexer, _("expecting VARIABLES"));
376 lex_match (lexer, '=');
378 dict = dict_create ();
380 type = data_parser_get_type (parser);
383 char name[VAR_NAME_LEN + 1];
384 struct fmt_spec input, output;
388 while (type == DP_FIXED && lex_match (lexer, '/'))
390 if (!lex_force_int (lexer))
392 if (lex_integer (lexer) < record)
394 msg (SE, _("The record number specified, %ld, is at or "
395 "before the previous record, %d. Data "
396 "fields must be listed in order of "
397 "increasing record number."),
398 lex_integer (lexer), record);
401 if (lex_integer (lexer) > data_parser_get_records (parser))
403 msg (SE, _("The record number specified, %ld, exceeds "
404 "the number of records per case specified "
406 lex_integer (lexer), data_parser_get_records (parser));
409 record = lex_integer (lexer);
413 if (!lex_force_id (lexer))
415 strcpy (name, lex_tokid (lexer));
418 if (type == DP_DELIMITED)
420 if (!parse_format_specifier (lexer, &input)
421 || !fmt_check_input (&input))
426 if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
428 if (!parse_format_specifier_name (lexer, &input.type))
430 input.w = lc - fc + 1;
432 if (!fmt_check_input (&input))
435 output = fmt_for_output_from_input (&input);
437 v = dict_create_var (dict, name, fmt_var_width (&input));
440 msg (SE, _("%s is a duplicate variable name."), name);
443 var_set_both_formats (v, &output);
445 if (type == DP_DELIMITED)
446 data_parser_add_delimited_field (parser, &input,
447 var_get_case_index (v),
450 data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
453 while (lex_token (lexer) != '.');
455 reader = dfm_open_reader (fh, lexer);
459 data_parser_make_active_file (parser, ds, reader, dict);
464 data_parser_destroy (parser);
467 return CMD_CASCADING_FAILURE;