+ 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;
+ sd = &r->msd;
+ }
+ else
+ {
+ sd = &r->rsd;
+ }
+
+ if (show_errors)
+ xmlTextReaderSetErrorHandler (xtr, gnumeric_error_handler, r);
+
+ r->target_sheet = NULL;
+ r->target_sheet_index = -1;
+
+ sd->row = sd->col = -1;
+ sd->state = STATE_PRE_INIT;
+ sd->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 ( (sd->state != STATE_INIT )
+ && 1 == (ret = xmlTextReaderRead (sd->xtr)))
+ {
+ process_node (r, sd);
+ }
+
+
+ if ( ret != 1)
+ {
+ /* Does not seem to be a gnumeric file */
+ xmlFreeTextReader (sd->xtr);
+ free (r);
+ return NULL;
+ }
+
+ r->spreadsheet.type = SPREADSHEET_GNUMERIC;
+
+ if (show_errors)
+ {
+ const xmlChar *enc = xmlTextReaderConstEncoding (sd->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);
+ }