zip-reader: Read the whole central directory at .zip open time.
[pspp] / src / data / ods-reader.c
index 736985ea8be29a8c6bc4947de7591e7f889f89b5..ae9ac8e0414cee65d4229feb6788b9cc2ca4e372 100644 (file)
 
 #include <config.h>
 
-#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 spreadsheet *
-ods_probe (const char *filename, bool report_errors)
-{
-  if (report_errors)
-    msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument");
-
-  return NULL;
-}
-
-const char *
-ods_get_sheet_name (struct spreadsheet *s, int n)
-{
-  return NULL;
-}
-
-char *
-ods_get_sheet_range (struct spreadsheet *s, int n)
-{
-  return NULL;
-}
-
-struct casereader *
-ods_make_reader (struct spreadsheet *spreadsheet,
-                const struct spreadsheet_read_options *opts)
-{
-  return NULL;
-}
-
-
-void
-ods_unref (struct spreadsheet *r)
-{
-}
-
-#else
-
-#include "libpspp/zip-reader.h"
-
-
 #include <assert.h>
 #include <stdbool.h>
 #include <errno.h>
 #include <libxml/xmlreader.h>
 #include <zlib.h>
 
-#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 *);
 
@@ -128,6 +85,7 @@ enum reader_state
 struct state_data
 {
   xmlTextReaderPtr xtr;
+  struct zip_member *zm;
   int node_type;
   enum reader_state state;
   int row;
@@ -146,6 +104,9 @@ state_data_destroy (struct state_data *sd)
 
   xmlFreeTextReader (sd->xtr);
   sd->xtr = NULL;
+
+  zip_member_finish (sd->zm);
+  sd->zm = NULL;
 }
 
 struct ods_reader
@@ -196,7 +157,7 @@ ods_unref (struct spreadsheet *s)
          xmlFree (r->sheets[i].name);
        }
 
-      dict_destroy (r->dict);
+      dict_unref (r->dict);
 
       zip_reader_destroy (r->zreader);
       free (r->sheets);
@@ -444,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)
@@ -550,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
@@ -562,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))
     {
@@ -577,6 +542,7 @@ get_sheet_count (struct zip_reader *zreader)
            {
              int s = _xmlchar_to_int (attr);
              xmlFreeTextReader (mxtr);
+              zip_member_finish (meta);
              xmlFree (name);
              xmlFree (attr);
              return s;
@@ -587,6 +553,7 @@ get_sheet_count (struct zip_reader *zreader)
     }
 
   xmlFreeTextReader (mxtr);
+  zip_member_finish (meta);
   return -1;
 }
 
@@ -604,8 +571,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;
@@ -613,21 +581,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)
     xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
 
-  return xtr;
+  return true;
 }
 
 
@@ -637,7 +606,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);
@@ -661,17 +629,8 @@ ods_probe (const char *filename, bool report_errors)
   r->zreader = zr;
   r->spreadsheet.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;
-
+  if (!init_reader (r, report_errors, &r->msd))
+    goto error;
 
   r->spreadsheet.n_sheets = sheet_count;
   r->n_allocated_sheets = 0;
@@ -698,7 +657,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;
@@ -708,16 +666,9 @@ ods_make_reader (struct spreadsheet *spreadsheet,
   ds_init_empty (&r->ods_errs);
   ++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;
 
@@ -764,17 +715,15 @@ 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)
@@ -785,8 +734,7 @@ ods_make_reader (struct spreadsheet *spreadsheet,
              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));
 
@@ -796,11 +744,14 @@ ods_make_reader (struct spreadsheet *spreadsheet,
                          (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);
            }
@@ -935,7 +886,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);
@@ -1044,4 +995,3 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
 
   return c;
 }
-#endif