Make the gnumeric reader behave the same as the odsreader
[pspp] / src / data / gnumeric-reader.c
index 514616ace74b59749cef5930344533a7bda51683..9193cfb061c4949be89b8467877174bcbf1a0264 100644 (file)
@@ -63,6 +63,7 @@ static void gnm_file_casereader_destroy (struct casereader *, void *);
 
 static struct ccase *gnm_file_casereader_read (struct casereader *, void *);
 
+
 static const struct casereader_class gnm_file_casereader_class =
   {
     gnm_file_casereader_read,
@@ -103,6 +104,7 @@ struct sheet_detail
 struct gnumeric_reader
 {
   struct spreadsheet spreadsheet;
+  int ref_cnt;
 
   /* The libxml reader for this instance */
   xmlTextReaderPtr xtr;
@@ -133,6 +135,30 @@ struct gnumeric_reader
 };
 
 
+void
+gnumeric_destroy (struct spreadsheet *s)
+{
+  struct gnumeric_reader *r = (struct gnumeric_reader *) s;
+
+
+#if 0
+  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);
+    }
+#endif
+}
+
+
 const char *
 gnumeric_get_sheet_name (struct spreadsheet *s, int n)
 {
@@ -176,9 +202,11 @@ static void
 gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
 {
   struct gnumeric_reader *r = r_;
+
   if ( r == NULL)
        return ;
 
+#if 0
   if ( r->xtr)
     xmlFreeTextReader (r->xtr);
   r->xtr = NULL;
@@ -188,19 +216,11 @@ gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
 
   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);
+  gnumeric_destroy (&r->spreadsheet);
 #endif
 }
 
+
 static void
 process_node (struct gnumeric_reader *r)
 {
@@ -425,13 +445,6 @@ struct var_spec
 };
 
 
-void 
-gnumeric_destroy (struct spreadsheet *s)
-{
-  gnm_file_casereader_destroy (NULL, s);
-}
-
-
 static void
 gnumeric_error_handler (void *ctx, const char *mesg,
                        UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
@@ -494,6 +507,7 @@ gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_erro
   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
@@ -563,6 +577,8 @@ gnumeric_make_reader (struct spreadsheet *spreadsheet,
   if (r->row != -1)
     r = gnumeric_reopen (r, NULL, true);
 
+
+
   if ( opts->cell_range )
     {
       if ( ! convert_cell_ref (opts->cell_range,