Spreadsheet readers: Seperate the mandatory from the optional arguments.
[pspp] / src / data / ods-reader.c
index ea8b9ad56b62fa279116a21703cfaf529ec747ec..097c245d179403380e755ceae9e6d66962921886 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 2011, 2012 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
@@ -33,7 +33,8 @@
 #if !ODF_READ_SUPPORT
 
 struct casereader *
-ods_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
+ods_open_reader (const struct spreadsheet_read_info *gri, struct spreadsheet_read_options *opts, 
+                struct dictionary **dict)
 {
   msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument");
 
@@ -324,17 +325,18 @@ convert_xml_to_value (struct ccase *c, const struct variable *var,
       text =
         xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
 
-      data_in (ss_cstr (text), "UTF-8",
-              fmt->type,
-              v,
-              var_get_width (var),
-              "UTF-8");
+      free (data_in (ss_cstr (text), "UTF-8",
+                     fmt->type,
+                     v,
+                     var_get_width (var),
+                     "UTF-8"));
     }
 }
 
 
 struct casereader *
-ods_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
+ods_open_reader (const struct spreadsheet_read_info *gri, struct spreadsheet_read_options *opts,
+                struct dictionary **dict)
 {
   int ret = 0;
   xmlChar *type = NULL;
@@ -382,14 +384,14 @@ ods_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
       goto error;
     }
 
-  if ( gri->cell_range )
+  if ( opts->cell_range )
     {
-      if ( ! convert_cell_ref (gri->cell_range,
+      if ( ! convert_cell_ref (opts->cell_range,
                               &r->start_col, &r->start_row,
                               &r->stop_col, &r->stop_row))
        {
          msg (SE, _("Invalid cell range `%s'"),
-              gri->cell_range);
+              opts->cell_range);
          goto error;
        }
     }
@@ -402,8 +404,8 @@ ods_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
     }
 
   r->state = STATE_INIT;
-  r->target_sheet = BAD_CAST gri->sheet_name;
-  r->target_sheet_index = gri->sheet_index;
+  r->target_sheet = BAD_CAST opts->sheet_name;
+  r->target_sheet_index = opts->sheet_index;
   r->row = r->col = -1;
   r->sheet_index = 0;
 
@@ -411,8 +413,10 @@ ods_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
   /* If CELLRANGE was given, then we know how many variables should be read */
   if ( r->stop_col != -1 )
     {
+      assert (var_spec == NULL);
       n_var_specs =  r->stop_col - r->start_col + 1;
       var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
+      memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
     }
 
 
@@ -460,7 +464,7 @@ ods_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
                  var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
 
                  /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
-                 memset (var_spec + n_var_specs * sizeof (*var_spec),
+                 memset (var_spec + n_var_specs,
                          0, 
                          (n_var_specs - idx + 1) * sizeof (*var_spec));
                  n_var_specs = idx + 1;
@@ -590,6 +594,10 @@ ods_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
 
   free (var_spec);
 
+  dict_destroy (r->dict);
+  ods_file_casereader_destroy (NULL, r);
+
+
   return NULL;
 }