X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fods-reader.c;h=736985ea8be29a8c6bc4947de7591e7f889f89b5;hb=6abd8f3ba67ec3751fc47487c1e09cf8e8de144a;hp=5363d40fe4e5df6ae3eafb134a1b9f6c9b5e0483;hpb=1e3875c54e8c220e282ab46f17bebdf4c0dea689;p=pspp diff --git a/src/data/ods-reader.c b/src/data/ods-reader.c index 5363d40fe4..736985ea8b 100644 --- a/src/data/ods-reader.c +++ b/src/data/ods-reader.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2011, 2012, 2013 Free Software Foundation, Inc. + Copyright (C) 2011, 2012, 2013, 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 @@ -34,15 +34,40 @@ #if !ODF_READ_SUPPORT -struct casereader * -ods_open_reader (const struct spreadsheet_read_options *opts, - struct dictionary **dict) +struct spreadsheet * +ods_probe (const char *filename, bool report_errors) { - msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument"); + if (report_errors) + msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument"); + + return NULL; +} +const char * +ods_get_sheet_name (struct spreadsheet *s, int n) +{ return NULL; } +char * +ods_get_sheet_range (struct spreadsheet *s, int n) +{ + return NULL; +} + +struct casereader * +ods_make_reader (struct spreadsheet *spreadsheet, + const struct spreadsheet_read_options *opts) +{ + return NULL; +} + + +void +ods_unref (struct spreadsheet *r) +{ +} + #else #include "libpspp/zip-reader.h" @@ -113,15 +138,28 @@ struct state_data int col_span; }; -struct ods_reader /* */ +static void +state_data_destroy (struct state_data *sd) +{ + xmlFree (sd->current_sheet_name); + sd->current_sheet_name = NULL; + + xmlFreeTextReader (sd->xtr); + sd->xtr = NULL; +} + +struct ods_reader { struct spreadsheet spreadsheet; struct zip_reader *zreader; - int ref_cnt; + int target_sheet_index; xmlChar *target_sheet_name; - - struct state_data foo; + + /* State data for the meta data */ + struct state_data msd; + + /* State data for the reader */ struct state_data rsd; int start_row; @@ -139,42 +177,46 @@ struct ods_reader /* */ bool read_names; struct string ods_errs; + + struct string zip_errs; }; void -ods_destroy (struct spreadsheet *s) +ods_unref (struct spreadsheet *s) { -#if 0 struct ods_reader *r = (struct ods_reader *) s; - if (--r->ref_cnt == 0) + if (--s->ref_cnt == 0) { int i; + state_data_destroy (&r->msd); for (i = 0; i < r->n_allocated_sheets; ++i) { xmlFree (r->sheets[i].name); } + dict_destroy (r->dict); + zip_reader_destroy (r->zreader); free (r->sheets); + free (s->file_name); free (r); } -#endif } static bool -reading_target_sheet (const struct ods_reader *r, const struct state_data *foo) +reading_target_sheet (const struct ods_reader *r, const struct state_data *msd) { if (r->target_sheet_name != NULL) { - if ( 0 == xmlStrcmp (r->target_sheet_name, foo->current_sheet_name)) + if ( 0 == xmlStrcmp (r->target_sheet_name, msd->current_sheet_name)) return true; } - - if (r->target_sheet_index == foo->current_sheet + 1) + + if (r->target_sheet_index == msd->current_sheet + 1) return true; return false; @@ -188,11 +230,11 @@ const char * ods_get_sheet_name (struct spreadsheet *s, int n) { struct ods_reader *r = (struct ods_reader *) s; - struct state_data *or = &r->foo; + struct state_data *or = &r->msd; assert (n < s->n_sheets); - while ( + while ( (r->n_allocated_sheets <= n) || or->state != STATE_SPREADSHEET ) @@ -211,13 +253,13 @@ char * ods_get_sheet_range (struct spreadsheet *s, int n) { struct ods_reader *r = (struct ods_reader *) s; - struct state_data *or = &r->foo; - + struct state_data *or = &r->msd; + assert (n < s->n_sheets); - while ( + while ( (r->n_allocated_sheets <= n) - || (r->sheets[n].stop_row == -1) + || (r->sheets[n].stop_row == -1) || or->state != STATE_SPREADSHEET ) { @@ -228,7 +270,7 @@ ods_get_sheet_range (struct spreadsheet *s, int n) process_node (r, or); } - return create_cell_ref ( + return create_cell_range ( r->sheets[n].start_col, r->sheets[n].start_row, r->sheets[n].stop_col, @@ -239,30 +281,29 @@ ods_get_sheet_range (struct spreadsheet *s, int n) static void ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_) { -#if 0 struct ods_reader *r = r_; if ( r == NULL) return ; - if (r->xtr) - xmlFreeTextReader (r->xtr); - r->xtr = NULL; + state_data_destroy (&r->rsd); if ( ! ds_is_empty (&r->ods_errs)) msg (ME, "%s", ds_cstr (&r->ods_errs)); ds_destroy (&r->ods_errs); - if ( ! r->used_first_case ) + if ( r->first_case && ! r->used_first_case ) case_unref (r->first_case); + caseproto_unref (r->proto); + r->proto = NULL; - xmlFree (r->current_sheet_name); xmlFree (r->target_sheet_name); + r->target_sheet_name = NULL; - ods_destroy (&r->spreadsheet); -#endif + + ods_unref (&r->spreadsheet); } @@ -292,7 +333,7 @@ process_node (struct ods_reader *or, struct state_data *r) break; case STATE_SPREADSHEET: if (0 == xmlStrcasecmp (name, _xml("table:table")) - && + && (XML_READER_TYPE_ELEMENT == r->node_type)) { xmlFree (r->current_sheet_name); @@ -323,24 +364,24 @@ process_node (struct ods_reader *or, struct state_data *r) } break; case STATE_TABLE: - if (0 == xmlStrcasecmp (name, _xml("table:table-row")) && + if (0 == xmlStrcasecmp (name, _xml("table:table-row")) && (XML_READER_TYPE_ELEMENT == r->node_type)) { xmlChar *value = xmlTextReaderGetAttribute (r->xtr, _xml ("table:number-rows-repeated")); - + int row_span = value ? _xmlchar_to_int (value) : 1; r->row += row_span; r->col = 0; - + if (! xmlTextReaderIsEmptyElement (r->xtr)) r->state = STATE_ROW; xmlFree (value); } - else if (0 == xmlStrcasecmp (name, _xml("table:table")) && + else if (0 == xmlStrcasecmp (name, _xml("table:table")) && (XML_READER_TYPE_END_ELEMENT == r->node_type)) { r->state = STATE_SPREADSHEET; @@ -348,13 +389,13 @@ process_node (struct ods_reader *or, struct state_data *r) break; case STATE_ROW: if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell"))) - && + && (XML_READER_TYPE_ELEMENT == r->node_type)) { xmlChar *value = xmlTextReaderGetAttribute (r->xtr, _xml ("table:number-columns-repeated")); - + r->col_span = value ? _xmlchar_to_int (value) : 1; r->col += r->col_span; @@ -394,7 +435,7 @@ process_node (struct ods_reader *or, struct state_data *r) if (or->sheets[r->current_sheet].start_row == -1) or->sheets[r->current_sheet].start_row = r->row - 1; - if ( + if ( (or->sheets[r->current_sheet].start_col == -1) || (or->sheets[r->current_sheet].start_col >= r->col - 1) @@ -417,8 +458,8 @@ process_node (struct ods_reader *or, struct state_data *r) xmlFree (name); } -/* - A struct containing the parameters of a cell's value +/* + A struct containing the parameters of a cell's value parsed from the xml */ struct xml_value @@ -463,7 +504,7 @@ xmv_to_width (const struct xml_value *xmv, int fallback) */ static void convert_xml_to_value (struct ccase *c, const struct variable *var, - const struct xml_value *xmv) + const struct xml_value *xmv, int col, int row) { union value *v = case_data_rw (c, var); @@ -489,12 +530,22 @@ convert_xml_to_value (struct ccase *c, const struct variable *var, const char *text = xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text); - - data_in (ss_cstr (text), "UTF-8", + char *m = data_in (ss_cstr (text), "UTF-8", fmt->type, v, var_get_width (var), "UTF-8"); + + if (m) + { + char buf [FMT_STRING_LEN_MAX + 1]; + char *cell = create_cell_ref (col, row); + + msg (MW, _("Cannot convert the value in the spreadsheet cell %s to format (%s): %s"), + cell, fmt_to_string (fmt, buf), m); + free (cell); + } + free (m); } } } @@ -527,12 +578,12 @@ get_sheet_count (struct zip_reader *zreader) int s = _xmlchar_to_int (attr); xmlFreeTextReader (mxtr); xmlFree (name); - xmlFree (attr); + xmlFree (attr); return s; } - xmlFree (attr); + xmlFree (attr); } - xmlFree (name); + xmlFree (name); } xmlFreeTextReader (mxtr); @@ -544,7 +595,7 @@ ods_error_handler (void *ctx, const char *mesg, UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc) { struct ods_reader *r = ctx; - + msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"), "ODF", r->spreadsheet.file_name, @@ -562,7 +613,6 @@ init_reader (struct ods_reader *r, bool report_errors) if ( content == NULL) return NULL; - zip_member_ref (content); xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read, (xmlInputCloseCallback) NULL, content, NULL, NULL, @@ -574,68 +624,71 @@ init_reader (struct ods_reader *r, bool report_errors) r->spreadsheet.type = SPREADSHEET_ODS; - if (report_errors) + if (report_errors) xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r); return xtr; } + struct spreadsheet * ods_probe (const char *filename, bool report_errors) { - struct ods_reader *r; - struct string errs = DS_EMPTY_INITIALIZER; int sheet_count; - struct zip_reader *zr = zip_reader_create (filename, &errs); + struct ods_reader *r = xzalloc (sizeof *r); xmlTextReaderPtr xtr; + struct zip_reader *zr; + + ds_init_empty (&r->zip_errs); + + zr = zip_reader_create (filename, &r->zip_errs); if (zr == NULL) { if (report_errors) { msg (ME, _("Cannot open %s as a OpenDocument file: %s"), - filename, ds_cstr (&errs)); + filename, ds_cstr (&r->zip_errs)); } + ds_destroy (&r->zip_errs); + free (r); return NULL; } sheet_count = get_sheet_count (zr); - r = xzalloc (sizeof *r); r->zreader = zr; - r->ref_cnt = 1; + r->spreadsheet.ref_cnt = 1; xtr = init_reader (r, report_errors); if (xtr == NULL) { goto error; } - r->foo.xtr = xtr; - r->foo.row = 0; - r->foo.col = 0; - r->foo.current_sheet = 0; - r->foo.state = STATE_INIT; + r->msd.xtr = xtr; + r->msd.row = 0; + r->msd.col = 0; + r->msd.current_sheet = 0; + r->msd.state = STATE_INIT; r->spreadsheet.n_sheets = sheet_count; r->n_allocated_sheets = 0; r->sheets = NULL; - ds_destroy (&errs); - - r->spreadsheet.file_name = filename; + r->spreadsheet.file_name = strdup (filename); return &r->spreadsheet; error: + ds_destroy (&r->zip_errs); zip_reader_destroy (r->zreader); - ds_destroy (&errs); free (r); return NULL; } struct casereader * -ods_make_reader (struct spreadsheet *spreadsheet, +ods_make_reader (struct spreadsheet *spreadsheet, const struct spreadsheet_read_options *opts) { intf ret = 0; @@ -653,7 +706,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, assert (r); r->read_names = opts->read_names; ds_init_empty (&r->ods_errs); - ++r->ref_cnt; + ++r->spreadsheet.ref_cnt; xtr = init_reader (r, true); if ( xtr == NULL) @@ -665,6 +718,8 @@ ods_make_reader (struct spreadsheet *spreadsheet, r->rsd.current_sheet = 0; r->rsd.state = STATE_INIT; + r->used_first_case = false; + r->first_case = NULL; if (opts->cell_range) { @@ -689,7 +744,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, r->target_sheet_index = opts->sheet_index; /* Advance to the start of the cells for the target sheet */ - while ( ! reading_target_sheet (r, &r->rsd) + while ( ! reading_target_sheet (r, &r->rsd) || r->rsd.state != STATE_ROW || r->rsd.row <= r->start_row ) { if (1 != (ret = xmlTextReaderRead (r->rsd.xtr))) @@ -707,7 +762,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, if ( opts->read_names) { - while (1 == (ret = xmlTextReaderRead (r->rsd.xtr))) + while (1 == xmlTextReaderRead (r->rsd.xtr)) { int idx; @@ -725,7 +780,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, if (r->stop_col != -1 && idx > r->stop_col - r->start_col) continue; - if (r->rsd.state == STATE_CELL_CONTENT + if (r->rsd.state == STATE_CELL_CONTENT && XML_READER_TYPE_TEXT == r->rsd.node_type) { @@ -737,7 +792,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */ memset (var_spec + n_var_specs, - 0, + 0, (idx - n_var_specs + 1) * sizeof (*var_spec)); n_var_specs = idx + 1; } @@ -787,7 +842,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, { var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1)); memset (var_spec + n_var_specs, - 0, + 0, (idx - n_var_specs + 1) * sizeof (*var_spec)); var_spec [idx].name = NULL; @@ -831,9 +886,6 @@ ods_make_reader (struct spreadsheet *spreadsheet, var_set_both_formats (var, &fmt); } - /* Create the first case, and cache it */ - r->used_first_case = false; - if ( n_var_specs == 0 ) { msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."), @@ -841,6 +893,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, goto error; } + /* Create the first case, and cache it */ r->proto = caseproto_ref (dict_get_proto (r->dict)); r->first_case = case_create (r->proto); case_set_missing (r->first_case); @@ -849,7 +902,9 @@ ods_make_reader (struct spreadsheet *spreadsheet, { const struct variable *var = dict_get_var (r->dict, i); - convert_xml_to_value (r->first_case, var, &var_spec[i].firstval); + convert_xml_to_value (r->first_case, var, &var_spec[i].firstval, + r->rsd.col - n_var_specs + i, + r->rsd.row - 1); } /* Read in the first row of data */ @@ -880,7 +935,7 @@ ods_make_reader (struct spreadsheet *spreadsheet, &ods_file_casereader_class, r); error: - + for ( i = 0 ; i < n_var_specs ; ++i ) { free (var_spec[i].firstval.type); @@ -891,8 +946,6 @@ ods_make_reader (struct spreadsheet *spreadsheet, free (var_spec); - dict_destroy (r->spreadsheet.dict); - r->spreadsheet.dict = NULL; ods_file_casereader_destroy (NULL, r); return NULL; @@ -907,6 +960,9 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) struct ccase *c = NULL; struct ods_reader *r = r_; + xmlChar *val_string = NULL; + xmlChar *type = NULL; + if (!r->used_first_case) { r->used_first_case = true; @@ -915,7 +971,7 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) /* Advance to the start of a row. (If there is one) */ - while (r->rsd.state != STATE_ROW + while (r->rsd.state != STATE_ROW && 1 == xmlTextReaderRead (r->rsd.xtr) ) { @@ -923,7 +979,7 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) } - if ( ! reading_target_sheet (r, &r->rsd) + if ( ! reading_target_sheet (r, &r->rsd) || r->rsd.state < STATE_TABLE || (r->stop_row != -1 && r->rsd.row > r->stop_row + 1) ) @@ -933,9 +989,6 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) c = case_create (r->proto); case_set_missing (c); - - xmlChar *val_string = NULL; - xmlChar *type = NULL; while (1 == xmlTextReaderRead (r->rsd.xtr)) { @@ -951,15 +1004,16 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value")); } - if (r->rsd.state == STATE_CELL_CONTENT && + if (r->rsd.state == STATE_CELL_CONTENT && r->rsd.node_type == XML_READER_TYPE_TEXT) { int col; struct xml_value *xmv = xzalloc (sizeof *xmv); xmv->text = xmlTextReaderValue (r->rsd.xtr); - xmv->value = val_string; - xmv->type = type; + xmv->value = val_string; val_string = NULL; + xmv->type = type; + type = NULL; for (col = 0; col < r->rsd.col_span; ++col) { @@ -973,7 +1027,7 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) break; var = dict_get_var (r->dict, idx); - convert_xml_to_value (c, var, xmv); + convert_xml_to_value (c, var, xmv, idx + r->start_col, r->rsd.row - 1); } xmlFree (xmv->text); @@ -985,6 +1039,9 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) break; } + xmlFree (type); + xmlFree (val_string); + return c; } #endif