X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fods-reader.c;h=7d91135b99e82cf2d249a6dae2b95039a1e65a6a;hb=c8e1adf66ea2110fc8a3f489c864e5ea93105031;hp=8504c14cca545033de0d074a29f45659c47258c0;hpb=19bf192a093527bf10b1de95fbe8798ba156f960;p=pspp diff --git a/src/data/ods-reader.c b/src/data/ods-reader.c index 8504c14cca..7d91135b99 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 @@ -16,56 +16,38 @@ #include -#include "libpspp/message.h" -#include "libpspp/misc.h" -#include "libpspp/assertion.h" - -#include "data/data-in.h" - -#include "gl/c-strtod.h" -#include "gl/minmax.h" - -#include "gettext.h" -#define _(msgid) gettext (msgid) -#define N_(msgid) (msgid) - #include "ods-reader.h" #include "spreadsheet-reader.h" -#if !ODF_READ_SUPPORT - -struct casereader * -ods_open_reader (const struct spreadsheet_read_options *opts, - struct dictionary **dict) -{ - msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument"); - - return NULL; -} - -#else - -#include "libpspp/zip-reader.h" - - #include #include #include #include #include -#include "data/format.h" #include "data/case.h" #include "data/casereader-provider.h" +#include "data/data-in.h" #include "data/dictionary.h" +#include "data/format.h" #include "data/identifier.h" #include "data/value.h" #include "data/variable.h" +#include "libpspp/assertion.h" #include "libpspp/i18n.h" +#include "libpspp/message.h" +#include "libpspp/misc.h" #include "libpspp/str.h" +#include "libpspp/zip-reader.h" +#include "gl/c-strtod.h" +#include "gl/minmax.h" #include "gl/xalloc.h" +#include "gettext.h" +#define _(msgid) gettext (msgid) +#define N_(msgid) (msgid) + static void ods_file_casereader_destroy (struct casereader *, void *); static struct ccase *ods_file_casereader_read (struct casereader *, void *); @@ -103,6 +85,7 @@ enum reader_state struct state_data { xmlTextReaderPtr xtr; + struct zip_member *zm; int node_type; enum reader_state state; int row; @@ -121,16 +104,19 @@ state_data_destroy (struct state_data *sd) xmlFreeTextReader (sd->xtr); sd->xtr = NULL; + + zip_member_finish (sd->zm); + sd->zm = NULL; } struct ods_reader { struct spreadsheet spreadsheet; struct zip_reader *zreader; - int ref_cnt; + int target_sheet_index; xmlChar *target_sheet_name; - + /* State data for the meta data */ struct state_data msd; @@ -157,11 +143,11 @@ struct ods_reader }; void -ods_destroy (struct spreadsheet *s) +ods_unref (struct spreadsheet *s) { struct ods_reader *r = (struct ods_reader *) s; - if (--r->ref_cnt == 0) + if (--s->ref_cnt == 0) { int i; @@ -170,10 +156,12 @@ ods_destroy (struct spreadsheet *s) { xmlFree (r->sheets[i].name); } - + + dict_unref (r->dict); + zip_reader_destroy (r->zreader); free (r->sheets); - + free (s->file_name); free (r); } } @@ -188,7 +176,7 @@ reading_target_sheet (const struct ods_reader *r, const struct state_data *msd) if ( 0 == xmlStrcmp (r->target_sheet_name, msd->current_sheet_name)) return true; } - + if (r->target_sheet_index == msd->current_sheet + 1) return true; @@ -207,7 +195,7 @@ ods_get_sheet_name (struct spreadsheet *s, int n) assert (n < s->n_sheets); - while ( + while ( (r->n_allocated_sheets <= n) || or->state != STATE_SPREADSHEET ) @@ -227,12 +215,12 @@ ods_get_sheet_range (struct spreadsheet *s, int n) { struct ods_reader *r = (struct ods_reader *) s; 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 ) { @@ -276,7 +264,7 @@ ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_) r->target_sheet_name = NULL; - ods_destroy (&r->spreadsheet); + ods_unref (&r->spreadsheet); } @@ -306,7 +294,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); @@ -337,24 +325,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; @@ -362,13 +350,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; @@ -408,7 +396,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,7 +405,7 @@ process_node (struct ods_reader *or, struct state_data *r) or->sheets[r->current_sheet].stop_row = r->row - 1; - if ( or->sheets[r->current_sheet].stop_col < r->col - 1) + if ( or->sheets[r->current_sheet].stop_col < r->col - 1) or->sheets[r->current_sheet].stop_col = r->col - 1; if (XML_READER_TYPE_END_ELEMENT == r->node_type) @@ -431,8 +419,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 @@ -514,7 +502,7 @@ convert_xml_to_value (struct ccase *c, const struct variable *var, 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"), + msg (MW, _("Cannot convert the value in the spreadsheet cell %s to format (%s): %s"), cell, fmt_to_string (fmt, buf), m); free (cell); } @@ -523,6 +511,12 @@ convert_xml_to_value (struct ccase *c, const struct variable *var, } } +static int +xml_reader_for_zip_member (void *zm_, char *buffer, int len) +{ + struct zip_member *zm = zm_; + return zip_member_read (zm, buffer, len); +} /* Try to find out how many sheets there are in the "workbook" */ static int @@ -535,9 +529,7 @@ get_sheet_count (struct zip_reader *zreader) if ( meta == NULL) return -1; - mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read, - (xmlInputCloseCallback) NULL, - meta, NULL, NULL, 0); + mxtr = xmlReaderForIO (xml_reader_for_zip_member, NULL, meta, NULL, NULL, 0); while (1 == xmlTextReaderRead (mxtr)) { @@ -550,25 +542,28 @@ get_sheet_count (struct zip_reader *zreader) { int s = _xmlchar_to_int (attr); xmlFreeTextReader (mxtr); + zip_member_finish (meta); xmlFree (name); - xmlFree (attr); + xmlFree (attr); return s; } - xmlFree (attr); + xmlFree (attr); } - xmlFree (name); + xmlFree (name); } xmlFreeTextReader (mxtr); + zip_member_finish (meta); return -1; } static void ods_error_handler (void *ctx, const char *mesg, - UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc) + xmlParserSeverities sev UNUSED, + 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, @@ -577,8 +572,9 @@ ods_error_handler (void *ctx, const char *mesg, } -static xmlTextReaderPtr -init_reader (struct ods_reader *r, bool report_errors) +static bool +init_reader (struct ods_reader *r, bool report_errors, + struct state_data *state) { struct zip_member *content = zip_member_open (r->zreader, "content.xml"); xmlTextReaderPtr xtr; @@ -586,21 +582,22 @@ init_reader (struct ods_reader *r, bool report_errors) if ( content == NULL) return NULL; - xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read, - (xmlInputCloseCallback) NULL, - content, NULL, NULL, + xtr = xmlReaderForIO (xml_reader_for_zip_member, NULL, content, NULL, NULL, report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) ); if ( xtr == NULL) - return false; + return false; + *state = (struct state_data) { .xtr = xtr, + .zm = content, + .state = STATE_INIT }; r->spreadsheet.type = SPREADSHEET_ODS; - if (report_errors) + if (report_errors) xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r); - return xtr; + return true; } @@ -610,7 +607,6 @@ ods_probe (const char *filename, bool report_errors) { int sheet_count; struct ods_reader *r = xzalloc (sizeof *r); - xmlTextReaderPtr xtr; struct zip_reader *zr; ds_init_empty (&r->zip_errs); @@ -632,25 +628,16 @@ ods_probe (const char *filename, bool report_errors) sheet_count = get_sheet_count (zr); r->zreader = zr; - r->ref_cnt = 1; - - xtr = init_reader (r, report_errors); - if (xtr == NULL) - { - goto error; - } - r->msd.xtr = xtr; - r->msd.row = 0; - r->msd.col = 0; - r->msd.current_sheet = 0; - r->msd.state = STATE_INIT; + r->spreadsheet.ref_cnt = 1; + if (!init_reader (r, report_errors, &r->msd)) + goto error; r->spreadsheet.n_sheets = sheet_count; r->n_allocated_sheets = 0; r->sheets = NULL; - r->spreadsheet.file_name = filename; + r->spreadsheet.file_name = strdup (filename); return &r->spreadsheet; error: @@ -661,7 +648,7 @@ ods_probe (const char *filename, bool report_errors) } struct casereader * -ods_make_reader (struct spreadsheet *spreadsheet, +ods_make_reader (struct spreadsheet *spreadsheet, const struct spreadsheet_read_options *opts) { intf ret = 0; @@ -671,7 +658,6 @@ ods_make_reader (struct spreadsheet *spreadsheet, int i; struct var_spec *var_spec = NULL; int n_var_specs = 0; - xmlTextReaderPtr xtr; struct ods_reader *r = (struct ods_reader *) spreadsheet; xmlChar *val_string = NULL; @@ -679,18 +665,11 @@ 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) + if (!init_reader (r, true, &r->rsd)) goto error; - r->rsd.xtr = xtr; - r->rsd.row = 0; - r->rsd.col = 0; - r->rsd.current_sheet = 0; - r->rsd.state = STATE_INIT; - r->used_first_case = false; r->first_case = NULL; @@ -717,7 +696,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))) @@ -737,43 +716,43 @@ ods_make_reader (struct spreadsheet *spreadsheet, { while (1 == xmlTextReaderRead (r->rsd.xtr)) { - int idx; - process_node (r, &r->rsd); /* If the row is finished then stop for now */ if (r->rsd.state == STATE_TABLE && r->rsd.row > r->start_row) break; - idx = r->rsd.col - r->start_col -1 ; + int idx = r->rsd.col - r->start_col - 1; - if ( idx < 0) + if (idx < 0) continue; 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) { xmlChar *value = xmlTextReaderValue (r->rsd.xtr); - - if ( idx >= n_var_specs) + if (idx >= n_var_specs) { var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1)); /* 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; } - var_spec[idx].firstval.text = 0; - var_spec[idx].firstval.value = 0; - var_spec[idx].firstval.type = 0; - - var_spec [idx].name = strdup (CHAR_CAST (const char *, value)); + for (int i = 0; i < r->rsd.col_span; ++i) + { + var_spec[idx - i].firstval.text = 0; + var_spec[idx - i].firstval.value = 0; + var_spec[idx - i].firstval.type = 0; + var_spec[idx - i].name = + strdup (CHAR_CAST (const char *, value)); + } xmlFree (value); } @@ -815,7 +794,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; @@ -908,7 +887,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); @@ -919,8 +898,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; @@ -946,7 +923,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) ) { @@ -954,7 +931,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) ) @@ -964,7 +941,7 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) c = case_create (r->proto); case_set_missing (c); - + while (1 == xmlTextReaderRead (r->rsd.xtr)) { process_node (r, &r->rsd); @@ -979,15 +956,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) { @@ -1013,6 +991,8 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) break; } + xmlFree (type); + xmlFree (val_string); + return c; } -#endif