X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fods-reader.c;h=8504c14cca545033de0d074a29f45659c47258c0;hb=0755b9a4256554906377f2db62e25ac09255a790;hp=dd9331a777b4a2ba7295972d933232d8dcfe5277;hpb=8870ca57b746c93b91fb740927fcda45643499d2;p=pspp diff --git a/src/data/ods-reader.c b/src/data/ods-reader.c index dd9331a777..8504c14cca 100644 --- a/src/data/ods-reader.c +++ b/src/data/ods-reader.c @@ -113,7 +113,17 @@ 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; @@ -121,7 +131,10 @@ struct ods_reader /* */ 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,43 +152,44 @@ struct ods_reader /* */ bool read_names; struct string ods_errs; + + struct string zip_errs; }; void ods_destroy (struct spreadsheet *s) { - printf ("%s:%d\n", __FILE__, __LINE__); -#if 0 struct ods_reader *r = (struct ods_reader *) s; if (--r->ref_cnt == 0) { int i; + state_data_destroy (&r->msd); for (i = 0; i < r->n_allocated_sheets; ++i) { xmlFree (r->sheets[i].name); } - + zip_reader_destroy (r->zreader); free (r->sheets); + 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; @@ -189,7 +203,7 @@ 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); @@ -212,7 +226,7 @@ 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); @@ -229,7 +243,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, @@ -240,31 +254,29 @@ ods_get_sheet_range (struct spreadsheet *s, int n) static void ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_) { - printf ("%s:%d %p\n", __FILE__, __LINE__, 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 } @@ -465,7 +477,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); @@ -491,12 +503,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); } } } @@ -564,7 +586,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, @@ -583,28 +604,33 @@ init_reader (struct ods_reader *r, bool report_errors) } + 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); - xmlTextReaderPtr xtr; + 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; @@ -613,25 +639,23 @@ ods_probe (const char *filename, bool report_errors) { 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; return &r->spreadsheet; error: + ds_destroy (&r->zip_errs); zip_reader_destroy (r->zreader); - ds_destroy (&errs); free (r); return NULL; } @@ -667,6 +691,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) { @@ -709,7 +735,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; @@ -833,9 +859,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."), @@ -843,6 +866,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); @@ -851,7 +875,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 */ @@ -909,9 +935,11 @@ 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) { - printf ("%s:%d\n", __FILE__, __LINE__); r->used_first_case = true; return r->first_case; } @@ -931,18 +959,12 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_) || (r->stop_row != -1 && r->rsd.row > r->stop_row + 1) ) { - printf ("%s:%d\n", __FILE__, __LINE__); return NULL; } - printf ("%s:%d\n", __FILE__, __LINE__); c = case_create (r->proto); case_set_missing (c); -#if 1 - xmlChar *val_string = NULL; - xmlChar *type = NULL; - while (1 == xmlTextReaderRead (r->rsd.xtr)) { process_node (r, &r->rsd); @@ -979,18 +1001,17 @@ 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); - // xmlFree (xmv->value); - // xmlFree (xmv->type); - // free (xmv); + xmlFree (xmv->text); + xmlFree (xmv->value); + xmlFree (xmv->type); + free (xmv); } if ( r->rsd.state <= STATE_TABLE) break; } -#endif return c; }