spreadsheet: Avoid sharing a dictionary between spreadsheet and client.
[pspp] / src / data / gnumeric-reader.c
index a0a4766652d2c07f029f0e1cb163bc6f0bf70894..7033448d8b4dc23a7c63b2d967854d551e0f47d9 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2007, 2009, 2010, 2011, 2012, 2013  Free Software Foundation, Inc.
+   Copyright (C) 2007, 2009, 2010, 2011, 2012, 2013, 2016  Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -28,7 +28,7 @@
 
 #include "spreadsheet-reader.h"
 
-#if !GNM_SUPPORT
+#if !GNM_READ_SUPPORT
 
 struct casereader *
 gnumeric_open_reader (const struct spreadsheet_read_options *opts, struct dictionary **dict)
@@ -149,7 +149,6 @@ state_data_destroy (struct state_data *sd)
 struct gnumeric_reader
 {
   struct spreadsheet spreadsheet;
-  int ref_cnt;
 
   struct state_data rsd;
   struct state_data msd;
@@ -174,11 +173,11 @@ struct gnumeric_reader
 
 
 void
-gnumeric_destroy (struct spreadsheet *s)
+gnumeric_unref (struct spreadsheet *s)
 {
   struct gnumeric_reader *r = (struct gnumeric_reader *) s;
 
-  if (0 == --r->ref_cnt)
+  if (0 == --s->ref_cnt)
     {
       int i;
 
@@ -186,10 +185,15 @@ gnumeric_destroy (struct spreadsheet *s)
        {
          xmlFree (r->sheets[i].name);
        }
-    
+
+
       free (r->sheets);
       state_data_destroy (&r->msd);
 
+      dict_destroy (r->dict);
+
+      free (s->file_name);
+
       free (r);
     }
 }
@@ -250,7 +254,7 @@ gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
   if (r->proto) 
     caseproto_unref (r->proto);
 
-  gnumeric_destroy (&r->spreadsheet);
+  gnumeric_unref (&r->spreadsheet);
 }
 
 
@@ -556,7 +560,7 @@ gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_erro
     {
       r = xzalloc (sizeof *r);
       r->spreadsheet.n_sheets = -1;
-      r->spreadsheet.file_name = filename;
+      r->spreadsheet.file_name = strdup (filename);
       sd = &r->msd;
     }
   else
@@ -573,7 +577,8 @@ gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_erro
   sd->row = sd->col = -1;
   sd->state = STATE_PRE_INIT;
   sd->xtr = xtr;
-  r->ref_cnt++;
+  r->spreadsheet.ref_cnt++;
+
 
   /* Advance to the start of the workbook.
      This gives us some confidence that we are actually dealing with a gnumeric
@@ -589,8 +594,7 @@ gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_erro
   if ( ret != 1)
     {
       /* Does not seem to be a gnumeric file */
-      xmlFreeTextReader (sd->xtr);
-      free (r);
+      gnumeric_unref (&r->spreadsheet);
       return NULL;
     }
 
@@ -629,6 +633,7 @@ struct casereader *
 gnumeric_make_reader (struct spreadsheet *spreadsheet,
                      const struct spreadsheet_read_options *opts)
 {
+  int type = 0;
   int x = 0;
   struct gnumeric_reader *r = NULL;
   unsigned long int vstart = 0;
@@ -696,7 +701,7 @@ gnumeric_make_reader (struct spreadsheet *spreadsheet,
       n_cases --;
     }
 
-  int type = 0;
+
   /* Read in the first row of cells,
      including the headers if read_names was set */
   while (
@@ -782,7 +787,7 @@ gnumeric_make_reader (struct spreadsheet *spreadsheet,
              xmlChar *attr =
                xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("ValueType"));
 
-             if ( NULL == attr || 60 !=  _xmlchar_to_int (attr))
+             if ( NULL == attr || VALUE_STRING !=  _xmlchar_to_int (attr))
                var_spec [idx].width = 0;
 
              free (attr);
@@ -870,8 +875,6 @@ gnumeric_make_reader (struct spreadsheet *spreadsheet,
     }
 
   free (var_spec);
-  dict_destroy (spreadsheet->dict);
-  spreadsheet->dict = NULL;
 
   gnm_file_casereader_destroy (NULL, r);
 
@@ -952,4 +955,4 @@ gnm_file_casereader_read (struct casereader *reader UNUSED, void *r_)
 }
 
 
-#endif /* GNM_SUPPORT */
+#endif /* GNM_READ_SUPPORT */