X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fdata-io%2Fget-data.c;h=8a58be434c71a6b61de8497bb255458fd3d3daf5;hb=8d6bfdd2a100bf8166b3b0b3f006d46f3e7a59e9;hp=3458b39e413fa0c205746977223d5023e86b3b45;hpb=7ec18587c5a81ed4cac8d458412c5c08ba68a6b1;p=pspp diff --git a/src/language/data-io/get-data.c b/src/language/data-io/get-data.c index 3458b39e41..8a58be434c 100644 --- a/src/language/data-io/get-data.c +++ b/src/language/data-io/get-data.c @@ -1,6 +1,6 @@ /* PSPP - a program for statistical analysis. Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, - 2013, 2015 Free Software Foundation, Inc. + 2013, 2015, 2016 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -46,19 +46,6 @@ #define _(msgid) gettext (msgid) #define N_(msgid) (msgid) - -#ifdef ODF_READ_SUPPORT -static const bool odf_read_support = true; -#else -static const bool odf_read_support = false; -#endif - -#ifdef GNM_READ_SUPPORT -static const bool gnm_read_support = true; -#else -static const bool gnm_read_support = false; -#endif - static bool parse_spreadsheet (struct lexer *lexer, char **filename, struct spreadsheet_read_options *opts); @@ -72,21 +59,27 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds) { char *tok = NULL; struct spreadsheet_read_options opts; - + opts.sheet_name = NULL; opts.sheet_index = -1; opts.cell_range = NULL; opts.read_names = false; opts.asw = -1; - lex_force_match (lexer, T_SLASH); + if (! lex_force_match (lexer, T_SLASH)) + goto error; if (!lex_force_match_id (lexer, "TYPE")) goto error; - lex_force_match (lexer, T_EQUALS); + if (!lex_force_match (lexer, T_EQUALS)) + goto error; + + const char *s = lex_tokcstr (lexer); + + if (s) + tok = strdup (s); - tok = strdup (lex_tokcstr (lexer)); if (lex_match_id (lexer, "TXT")) { free (tok); @@ -97,45 +90,38 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds) free (tok); return parse_get_psql (lexer, ds); } - else if (lex_match_id (lexer, "GNM") || + else if (lex_match_id (lexer, "GNM") || lex_match_id (lexer, "ODS")) { char *filename = NULL; - struct casereader *reader = NULL; - struct dictionary *dict = NULL; - if (!parse_spreadsheet (lexer, &filename, &opts)) goto error; - if ( gnm_read_support && 0 == strncasecmp (tok, "GNM", 3)) - { - struct spreadsheet *spreadsheet = gnumeric_probe (filename, true); - if (spreadsheet == NULL) - goto error; - reader = gnumeric_make_reader (spreadsheet, &opts); - dict = spreadsheet->dict; - gnumeric_unref (spreadsheet); - } - else if ( odf_read_support && 0 == strncasecmp (tok, "ODS", 3)) - { - struct spreadsheet *spreadsheet = ods_probe (filename, true); - if (spreadsheet == NULL) - goto error; - reader = ods_make_reader (spreadsheet, &opts); - dict = spreadsheet->dict; - ods_unref (spreadsheet); - } + struct spreadsheet *spreadsheet = NULL; + if ( 0 == strncasecmp (tok, "GNM", 3)) + spreadsheet = gnumeric_probe (filename, true); + else if ( 0 == strncasecmp (tok, "ODS", 3)) + spreadsheet = ods_probe (filename, true); + if (spreadsheet == NULL) + { + msg (SE, _("error reading file `%s'"), filename); + free (filename); + goto error; + } free (filename); + struct casereader *reader = spreadsheet_make_reader (spreadsheet, &opts); if (reader) { - dataset_set_dict (ds, dict); + dataset_set_dict (ds, dict_clone (spreadsheet->dict)); dataset_set_source (ds, reader); free (tok); destroy_spreadsheet_read_info (&opts); + spreadsheet_unref (spreadsheet); return CMD_SUCCESS; } + spreadsheet_unref (spreadsheet); } else msg (SE, _("Unsupported TYPE %s."), tok); @@ -157,12 +143,14 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds) psql.bsize = -1; ds_init_empty (&psql.sql); - lex_force_match (lexer, T_SLASH); + if (! lex_force_match (lexer, T_SLASH)) + goto error; if (!lex_force_match_id (lexer, "CONNECT")) goto error; - lex_force_match (lexer, T_EQUALS); + if (! lex_force_match (lexer, T_EQUALS)) + goto error; if (!lex_force_string (lexer)) goto error; @@ -176,14 +164,20 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds) if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH")) { lex_match (lexer, T_EQUALS); - psql.str_width = lex_integer (lexer); - lex_get (lexer); + if (lex_force_int (lexer)) + { + psql.str_width = lex_integer (lexer); + lex_get (lexer); + } } else if ( lex_match_id (lexer, "BSIZE")) { lex_match (lexer, T_EQUALS); - psql.bsize = lex_integer (lexer); - lex_get (lexer); + if (lex_force_int (lexer)) + { + psql.bsize = lex_integer (lexer); + lex_get (lexer); + } } else if ( lex_match_id (lexer, "UNENCRYPTED")) { @@ -224,7 +218,7 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds) } static bool -parse_spreadsheet (struct lexer *lexer, char **filename, +parse_spreadsheet (struct lexer *lexer, char **filename, struct spreadsheet_read_options *opts) { opts->sheet_index = 1; @@ -233,12 +227,14 @@ parse_spreadsheet (struct lexer *lexer, char **filename, opts->read_names = true; opts->asw = -1; - lex_force_match (lexer, T_SLASH); + if (! lex_force_match (lexer, T_SLASH)) + goto error; if (!lex_force_match_id (lexer, "FILE")) goto error; - lex_force_match (lexer, T_EQUALS); + if (! lex_force_match (lexer, T_EQUALS)) + goto error; if (!lex_force_string (lexer)) goto error; @@ -252,8 +248,11 @@ parse_spreadsheet (struct lexer *lexer, char **filename, if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH")) { lex_match (lexer, T_EQUALS); - opts->asw = lex_integer (lexer); - lex_get (lexer); + if (lex_force_int (lexer)) + { + opts->asw = lex_integer (lexer); + lex_get (lexer); + } } else if (lex_match_id (lexer, "SHEET")) { @@ -270,6 +269,9 @@ parse_spreadsheet (struct lexer *lexer, char **filename, } else if (lex_match_id (lexer, "INDEX")) { + if (!lex_force_int (lexer)) + goto error; + opts->sheet_index = lex_integer (lexer); if (opts->sheet_index <= 0) { @@ -376,11 +378,13 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) enum data_parser_type type; bool has_type; - lex_force_match (lexer, T_SLASH); + if (! lex_force_match (lexer, T_SLASH)) + goto error; if (!lex_force_match_id (lexer, "FILE")) goto error; - lex_force_match (lexer, T_EQUALS); + if (! lex_force_match (lexer, T_EQUALS)) + goto error; fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL); if (fh == NULL) goto error; @@ -483,33 +487,22 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) lex_match (lexer, T_EQUALS); if (lex_match (lexer, T_ALL)) { - data_parser_set_case_limit (parser, -1); - data_parser_set_case_percent (parser, 100); + /* Nothing to do. */ } else if (lex_match_id (lexer, "FIRST")) { if (!lex_force_int (lexer)) goto error; - if (lex_integer (lexer) < 1) - { - msg (SE, _("Value of %s must be 1 or greater."), "FIRST"); - goto error; - } - data_parser_set_case_limit (parser, lex_integer (lexer)); lex_get (lexer); } else if (lex_match_id (lexer, "PERCENT")) { if (!lex_force_int (lexer)) goto error; - if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100) - { - msg (SE, _("Value of %s must be between 1 and 100."), "PERCENT"); - goto error; - } - data_parser_set_case_percent (parser, lex_integer (lexer)); lex_get (lexer); } + msg (SW, _("Ignoring obsolete IMPORTCASES subcommand. (N OF CASES " + "or SAMPLE may be used to substitute.)")); } else if (lex_match_id_n (lexer, "DELIMITERS", 4)) { @@ -606,18 +599,26 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) lex_get (lexer); } + const char * tstr = lex_tokcstr (lexer); + if (tstr == NULL) + { + lex_error (lexer, NULL); + goto error; + } + name = xstrdup (tstr); if (!lex_force_id (lexer) - || !dict_id_is_valid (dict, lex_tokcstr (lexer), true)) - goto error; - name = xstrdup (lex_tokcstr (lexer)); + || !dict_id_is_valid (dict, name, true)) + { + goto error; + } lex_get (lexer); - if (type == DP_DELIMITED) { if (!parse_format_specifier (lexer, &input) - || !fmt_check_input (&input)) - goto error; - + || !fmt_check_input (&input)) + { + goto error; + } output = fmt_for_output_from_input (&input); } else @@ -637,14 +638,12 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) msg (SE, _("Unknown format type `%s'."), fmt_type_name); goto error; } - /* Compose input format. */ input.type = fmt_type; input.w = lc - fc + 1; input.d = 0; if (!fmt_check_input (&input)) goto error; - /* Compose output format. */ if (w != 0) { @@ -657,7 +656,6 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) else output = fmt_for_output_from_input (&input); } - v = dict_create_var (dict, name, fmt_var_width (&input)); if (v == NULL) { @@ -665,7 +663,6 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) goto error; } var_set_both_formats (v, &output); - if (type == DP_DELIMITED) data_parser_add_delimited_field (parser, &input, var_get_case_index (v), @@ -682,14 +679,14 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) if (reader == NULL) goto error; - data_parser_make_active_file (parser, ds, reader, dict); + data_parser_make_active_file (parser, ds, reader, dict, NULL, NULL); fh_unref (fh); free (encoding); return CMD_SUCCESS; error: data_parser_destroy (parser); - dict_destroy (dict); + dict_unref (dict); fh_unref (fh); free (name); free (encoding); @@ -697,7 +694,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) } -static void +static void destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts) { free (opts->cell_range);