+
+static void
+gnumeric_error_handler (void *ctx, const char *mesg,
+ UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
+{
+ 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, bool show_errors)
+{
+ int ret;
+
+ xmlTextReaderPtr xtr;
+ gzFile gz;
+
+ assert (r == NULL || filename == NULL);
+
+ if (r && r->xtr)
+ xmlFreeTextReader (r->xtr);
+
+ if (filename)
+ gz = gzopen (filename, "r");
+ else
+ gz = gzopen ( r->spreadsheet.file_name, "r");
+
+ if (NULL == gz)
+ return NULL;
+
+
+ xtr = xmlReaderForIO ((xmlInputReadCallback) gzread,
+ (xmlInputCloseCallback) gzclose, gz,
+ NULL, NULL,
+ show_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
+
+ if (xtr == NULL)
+ {
+ gzclose (gz);
+ return NULL;
+ }
+
+ if (r == NULL)
+ {
+ r = xzalloc (sizeof *r);
+ 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
+ spreadsheet.
+ */
+ while ( (r->state != STATE_INIT )
+ && 1 == (ret = xmlTextReaderRead (r->xtr)))
+ {
+ 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, bool report_errors)
+{
+ struct gnumeric_reader *r = gnumeric_reopen (NULL, filename, report_errors);
+
+ return &r->spreadsheet;
+}
+
+