X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fdata-io%2Fget-data.c;h=66dd19f2397d7636228f2069eea4406eb07945ae;hb=0fc606c52d7cec253af9b7463b15baabfbc9a33a;hp=e3a93b1738d0324be41f34c49200b7c5b5b3b0ce;hpb=7635ce0697c163bd9c80adb8b382df7a9aa97f42;p=pspp diff --git a/src/language/data-io/get-data.c b/src/language/data-io/get-data.c index e3a93b1738..66dd19f239 100644 --- a/src/language/data-io/get-data.c +++ b/src/language/data-io/get-data.c @@ -46,28 +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; -struct spreadsheet *ods_probe (const char *filename, bool report_errors){} -struct casereader * ods_make_reader (struct spreadsheet *spreadsheet, - const struct spreadsheet_read_options *opts){} -void ods_unref (struct spreadsheet *r){} -#endif - -#ifdef GNM_READ_SUPPORT -static const bool gnm_read_support = true; -#else -static const bool gnm_read_support = false; -struct spreadsheet *gnumeric_probe (const char *filename, bool report_errors){} -struct casereader * gnumeric_make_reader (struct spreadsheet *spreadsheet, - const struct spreadsheet_read_options *opts){} -void gnumeric_unref (struct spreadsheet *r){} - -#endif - static bool parse_spreadsheet (struct lexer *lexer, char **filename, struct spreadsheet_read_options *opts); @@ -81,7 +59,7 @@ 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; @@ -101,7 +79,7 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds) if (s) tok = strdup (s); - + if (lex_match_id (lexer, "TXT")) { free (tok); @@ -112,7 +90,7 @@ 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; @@ -120,14 +98,18 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds) goto error; struct spreadsheet *spreadsheet = NULL; - if ( gnm_read_support && 0 == strncasecmp (tok, "GNM", 3)) + if (0 == strncasecmp (tok, "GNM", 3)) spreadsheet = gnumeric_probe (filename, true); - else if ( odf_read_support && 0 == strncasecmp (tok, "ODS", 3)) + else if (0 == strncasecmp (tok, "ODS", 3)) spreadsheet = ods_probe (filename, true); - free (filename); if (spreadsheet == NULL) - goto error; + { + msg (SE, _("error reading file `%s'"), filename); + free (filename); + goto error; + } + free (filename); struct casereader *reader = spreadsheet_make_reader (spreadsheet, &opts); if (reader) @@ -177,9 +159,9 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds) lex_get (lexer); - while (lex_match (lexer, T_SLASH) ) + while (lex_match (lexer, T_SLASH)) { - if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH")) + if (lex_match_id (lexer, "ASSUMEDSTRWIDTH")) { lex_match (lexer, T_EQUALS); if (lex_force_int (lexer)) @@ -188,7 +170,7 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds) lex_get (lexer); } } - else if ( lex_match_id (lexer, "BSIZE")) + else if (lex_match_id (lexer, "BSIZE")) { lex_match (lexer, T_EQUALS); if (lex_force_int (lexer)) @@ -197,14 +179,14 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds) lex_get (lexer); } } - else if ( lex_match_id (lexer, "UNENCRYPTED")) + else if (lex_match_id (lexer, "UNENCRYPTED")) { psql.allow_clear = true; } else if (lex_match_id (lexer, "SQL")) { lex_match (lexer, T_EQUALS); - if ( ! lex_force_string (lexer) ) + if (! lex_force_string (lexer)) goto error; ds_put_substring (&psql.sql, lex_tokss (lexer)); @@ -215,7 +197,7 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds) struct dictionary *dict = NULL; struct casereader *reader = psql_open_reader (&psql, &dict); - if ( reader ) + if (reader) { dataset_set_dict (ds, dict); dataset_set_source (ds, reader); @@ -236,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; @@ -261,9 +243,9 @@ parse_spreadsheet (struct lexer *lexer, char **filename, lex_get (lexer); - while (lex_match (lexer, T_SLASH) ) + while (lex_match (lexer, T_SLASH)) { - if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH")) + if (lex_match_id (lexer, "ASSUMEDSTRWIDTH")) { lex_match (lexer, T_EQUALS); if (lex_force_int (lexer)) @@ -277,7 +259,7 @@ parse_spreadsheet (struct lexer *lexer, char **filename, lex_match (lexer, T_EQUALS); if (lex_match_id (lexer, "NAME")) { - if ( ! lex_force_string (lexer) ) + if (! lex_force_string (lexer)) goto error; opts->sheet_name = ss_xstrdup (lex_tokss (lexer)); @@ -315,7 +297,7 @@ parse_spreadsheet (struct lexer *lexer, char **filename, } else if (lex_match_id (lexer, "RANGE")) { - if ( ! lex_force_string (lexer) ) + if (! lex_force_string (lexer)) goto error; opts->cell_range = ss_xstrdup (lex_tokss (lexer)); @@ -332,7 +314,7 @@ parse_spreadsheet (struct lexer *lexer, char **filename, { lex_match (lexer, T_EQUALS); - if ( lex_match_id (lexer, "ON")) + if (lex_match_id (lexer, "ON")) { opts->read_names = true; } @@ -443,7 +425,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) DP_DELIMITED, &has_type); else { - lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL); + lex_error_expecting (lexer, "FIXED", "DELIMITED"); goto error; } if (!ok) @@ -481,7 +463,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) } else { - lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL); + lex_error_expecting (lexer, "LINE", "VARIABLES"); goto error; } } @@ -578,7 +560,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) break; else { - lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL); + lex_error_expecting (lexer, "VARIABLES"); goto error; } } @@ -617,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 @@ -648,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) { @@ -668,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) { @@ -676,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), @@ -693,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); @@ -708,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);