#include "spreadsheet-reader.h"
-#include "c-xvasprintf.h"
-
#if !GNM_SUPPORT
struct casereader *
-gnumeric_open_reader (struct spreadsheet_read_info *gri, struct spreadsheet_read_options *opts, struct dictionary **dict)
+gnumeric_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"), "Gnumeric");
static struct ccase *gnm_file_casereader_read (struct casereader *, void *);
+
static const struct casereader_class gnm_file_casereader_class =
{
gnm_file_casereader_read,
struct sheet_detail
{
- xmlChar *name;
+ /* The name of the sheet (utf8 encoding) */
+ char *name;
int start_col;
int stop_col;
struct gnumeric_reader
{
struct spreadsheet spreadsheet;
+ int ref_cnt;
/* The libxml reader for this instance */
xmlTextReaderPtr xtr;
};
+void
+gnumeric_destroy (struct spreadsheet *s)
+{
+ struct gnumeric_reader *r = (struct gnumeric_reader *) s;
+
+ if (0 == --r->ref_cnt)
+ {
+ int i;
+
+ for (i = 0; i < s->n_sheets; ++i)
+ {
+ xmlFree (r->sheets[i].name);
+ }
+
+ free (r->sheets);
+
+ free (r);
+ }
+}
+
+
const char *
gnumeric_get_sheet_name (struct spreadsheet *s, int n)
{
struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
assert (n < s->n_sheets);
- return gr->sheets[n].name;
+ return gr->sheets[n].name;
}
static void process_node (struct gnumeric_reader *r);
+
char *
gnumeric_get_sheet_range (struct spreadsheet *s, int n)
{
static void
gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
{
- int i;
struct gnumeric_reader *r = r_;
if ( r == NULL)
return ;
caseproto_unref (r->proto);
-#if 0
- for (i = 0; i < r->spreadsheet.n_sheets; ++i)
- {
- xmlFree (r->sheets[i].name);
- }
-
- free (r->sheets);
-
-
- free (r);
-#endif
+ gnumeric_destroy (&r->spreadsheet);
}
+
static void
process_node (struct gnumeric_reader *r)
{
if (name == NULL)
name = xmlStrdup (_xml ("--"));
-
r->node_type = xmlTextReaderNodeType (r->xtr);
- switch ( r->state)
+ switch (r->state)
{
case STATE_PRE_INIT:
r->current_sheet = -1;
}
else if (XML_READER_TYPE_TEXT == r->node_type)
{
- r->sheets [r->spreadsheet.n_sheets - 1].name = xmlTextReaderValue (r->xtr);
+ r->sheets [r->spreadsheet.n_sheets - 1].name = CHAR_CAST (char *, xmlTextReaderValue (r->xtr));
}
break;
};
-void
-gnumeric_destroy (struct spreadsheet *s)
+static void
+gnumeric_error_handler (void *ctx, const char *mesg,
+ UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
{
- struct gnumeric_reader *r = (struct gnumeric *) s;
- gnm_file_casereader_destroy (NULL, s);
+ struct gnumeric_reader *r = ctx;
+
+ msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
+ "Gnumeric",
+ r->spreadsheet.file_name,
+ xmlTextReaderLocatorLineNumber (loc),
+ mesg);
}
-
static struct gnumeric_reader *
-gnumeric_reopen (struct gnumeric_reader *r, const char *filename)
+gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_errors)
{
int ret;
if (NULL == gz)
return NULL;
+
xtr = xmlReaderForIO ((xmlInputReadCallback) gzread,
- (xmlInputCloseCallback) gzclose, gz,
- NULL, NULL, 0);
+ (xmlInputCloseCallback) gzclose, gz,
+ NULL, NULL,
+ show_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
if (xtr == NULL)
{
r->spreadsheet.n_sheets = -1;
r->spreadsheet.file_name = filename;
}
+
+ if (show_errors)
+ xmlTextReaderSetErrorHandler (xtr, gnumeric_error_handler, r);
r->target_sheet = NULL;
r->target_sheet_index = -1;
r->row = r->col = -1;
r->state = STATE_PRE_INIT;
r->xtr = xtr;
+ r->ref_cnt++;
/* Advance to the start of the workbook.
This gives us some confidence that we are actually dealing with a gnumeric
process_node (r);
}
+
+ if ( ret != 1)
+ {
+ /* Does not seem to be a gnumeric file */
+ xmlFreeTextReader (r->xtr);
+ free (r);
+ return NULL;
+ }
+
r->spreadsheet.type = SPREADSHEET_GNUMERIC;
+ if (show_errors)
+ {
+ const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
+ xmlCharEncoding xce = xmlParseCharEncoding (CHAR_CAST (const char *, enc));
+
+ if ( XML_CHAR_ENCODING_UTF8 != xce)
+ {
+ /* I have been told that ALL gnumeric files are UTF8 encoded. If that is correct, this
+ can never happen. */
+ msg (MW, _("The gnumeric file `%s' is encoded as %s instead of the usual UTF-8 encoding. "
+ "Any non-ascii characters will be incorrectly imported."),
+ r->spreadsheet.file_name,
+ enc);
+ }
+ }
+
return r;
}
struct spreadsheet *
-gnumeric_probe (const char *filename)
+gnumeric_probe (const char *filename, bool report_errors)
{
- struct gnumeric_reader *r = gnumeric_reopen (NULL, filename);
+ struct gnumeric_reader *r = gnumeric_reopen (NULL, filename, report_errors);
return &r->spreadsheet;
}
struct casereader *
gnumeric_make_reader (struct spreadsheet *spreadsheet,
- const struct spreadsheet_read_info *gri,
- struct spreadsheet_read_options *opts)
+ const struct spreadsheet_read_options *opts)
{
+ int x = 0;
struct gnumeric_reader *r = NULL;
unsigned long int vstart = 0;
int ret;
r = (struct gnumeric_reader *) (spreadsheet);
if (r->row != -1)
- r = gnumeric_reopen (r, NULL);
+ r = gnumeric_reopen (r, NULL, true);
+
+
if ( opts->cell_range )
{
n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
}
- if ( gri->read_names )
+ if ( opts->read_names )
{
r->start_row++;
n_cases --;
if ( r->row < r->start_row)
{
- if ( gri->read_names )
+ if ( opts->read_names )
{
var_spec [idx].name = xstrdup (text);
}
var_spec [idx].first_value = xmlStrdup (value);
if (-1 == var_spec [idx].width )
- var_spec [idx].width = (gri->asw == -1) ?
- ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : gri->asw;
+ var_spec [idx].width = (opts->asw == -1) ?
+ ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : opts->asw;
}
free (value);
r->first_case = case_create (r->proto);
case_set_missing (r->first_case);
- int x = 0;
+
for ( i = 0 ; i < n_var_specs ; ++i )
{
+ const struct variable *var;
+
if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
continue;
- const struct variable *var = dict_get_var (r->dict, x++);
+ var = dict_get_var (r->dict, x++);
convert_xml_string_to_value (r->first_case, var,
var_spec[i].first_value);
free (var_spec);
-
- if (opts->cell_range == NULL)
- {
- opts->cell_range = c_xasprintf ("%c%d:%c%ld",
- r->start_col + 'A',
- r->start_row,
- r->stop_col + 'A' + caseproto_get_n_widths (r->proto),
- r->start_row + n_cases);
- }
-
+
return casereader_create_sequential
(NULL,
r->proto,