Rewrite Import Dialog.
[pspp] / src / data / ods-reader.c
index 0e55eb273e0ec60e3e2069c925c3a41112e78bec..c5ada6e09101bc3977ee2ecb67af59b0621aed6f 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "data/data-in.h"
 
+#include "gl/c-strtod.h"
 #include "gl/minmax.h"
 
 #include "gettext.h"
@@ -66,9 +67,9 @@ ods_open_reader (const struct spreadsheet_read_options *opts,
 #include "gl/xalloc.h"
 
 static void ods_file_casereader_destroy (struct casereader *, void *);
-
 static struct ccase *ods_file_casereader_read (struct casereader *, void *);
 
+
 static const struct casereader_class ods_file_casereader_class =
   {
     ods_file_casereader_read,
@@ -99,21 +100,42 @@ enum reader_state
     STATE_CELL_CONTENT     /* Found a the text within a cell */
   };
 
-struct ods_reader
+struct state_data
 {
-  struct spreadsheet spreadsheet;
-  struct zip_reader *zreader;
   xmlTextReaderPtr xtr;
-
+  int node_type;
   enum reader_state state;
-  bool sheet_found;
   int row;
   int col;
-  int node_type;
-  int sheet_index;
+  int current_sheet;
+  xmlChar *current_sheet_name;
+
+  int col_span;
+};
+
+static void
+state_data_destroy (struct state_data *sd)
+{
+  xmlFree (sd->current_sheet_name);
+  sd->current_sheet_name = NULL;
+
+  xmlFreeTextReader (sd->xtr);
+  sd->xtr = NULL;
+}
+
+struct ods_reader
+{
+  struct spreadsheet spreadsheet;
+  struct zip_reader *zreader;
 
-  const xmlChar *target_sheet;
   int target_sheet_index;
+  xmlChar *target_sheet_name;
+  
+  /* State data for the meta data */
+  struct state_data msd;
+
+  /* State data for the reader */
+  struct state_data rsd;
 
   int start_row;
   int start_col;
@@ -130,57 +152,102 @@ struct ods_reader
   bool read_names;
 
   struct string ods_errs;
+
+  struct string zip_errs;
 };
 
+void
+ods_unref (struct spreadsheet *s)
+{
+  struct ods_reader *r = (struct ods_reader *) s;
+
+  if (--s->ref_cnt == 0)
+    {
+      int i;
+
+      state_data_destroy (&r->msd);
+      for (i = 0; i < r->n_allocated_sheets; ++i)
+       {
+         xmlFree (r->sheets[i].name);
+       }
+       
+      zip_reader_destroy (r->zreader);
+      free (r->sheets);
+      free (s->file_name);
+      free (r);
+    }
+}
+
+
 
-static void process_node (struct ods_reader *r);
+static bool
+reading_target_sheet (const struct ods_reader *r, const struct state_data *msd)
+{
+  if (r->target_sheet_name != NULL)
+    {
+      if ( 0 == xmlStrcmp (r->target_sheet_name, msd->current_sheet_name))
+       return true;
+    }
+  
+  if (r->target_sheet_index == msd->current_sheet + 1)
+    return true;
+
+  return false;
+}
+
+
+static void process_node (struct ods_reader *or, struct state_data *r);
 
 
 const char *
 ods_get_sheet_name (struct spreadsheet *s, int n)
 {
-  int ret;
-  struct ods_reader *or = (struct ods_reader *) s;
-  
+  struct ods_reader *r = (struct ods_reader *) s;
+  struct state_data *or = &r->msd;
+
   assert (n < s->n_sheets);
 
   while ( 
-        (or->n_allocated_sheets <= n)
-        && 
-        (1 == (ret = xmlTextReaderRead (or->xtr)))
+         (r->n_allocated_sheets <= n)
+         || or->state != STATE_SPREADSHEET
          )
     {
-      process_node (or);
+      int ret = xmlTextReaderRead (or->xtr);
+      if ( ret != 1)
+       break;
+
+      process_node (r, or);
     }
 
-  return or->sheets[n].name;
+  return r->sheets[n].name;
 }
 
 char *
 ods_get_sheet_range (struct spreadsheet *s, int n)
 {
-  int ret = -1;
-  struct ods_reader *or = (struct ods_reader *) s;
+  struct ods_reader *r = (struct ods_reader *) s;
+  struct state_data *or = &r->msd;
   
   assert (n < s->n_sheets);
 
   while ( 
-        (
-         (or->n_allocated_sheets <= n)
-         || (or->sheets[n].stop_row == -1) )
-        && 
-        (1 == (ret = xmlTextReaderRead (or->xtr)))
+         (r->n_allocated_sheets <= n)
+         || (r->sheets[n].stop_row == -1) 
+         || or->state != STATE_SPREADSHEET
          )
     {
-      process_node (or);
-    }
+      int ret = xmlTextReaderRead (or->xtr);
+      if ( ret != 1)
+       break;
 
+      process_node (r, or);
+    }
 
-  return create_cell_ref (
-                         or->sheets[n].start_col,
-                         or->sheets[n].start_row,
-                         or->sheets[n].stop_col,
-                         or->sheets[n].stop_row);
+  return create_cell_range (
+                         r->sheets[n].start_col,
+                         r->sheets[n].start_row,
+                         r->sheets[n].stop_col,
+                         r->sheets[n].stop_row);
 }
 
 
@@ -191,25 +258,33 @@ ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
   if ( r == NULL)
     return ;
 
-  if (r->xtr)
-    xmlFreeTextReader (r->xtr);
-  r->xtr = NULL;
+  state_data_destroy (&r->rsd);
 
   if ( ! ds_is_empty (&r->ods_errs))
     msg (ME, "%s", ds_cstr (&r->ods_errs));
 
   ds_destroy (&r->ods_errs);
 
-  if ( ! r->used_first_case )
+  if ( r->first_case && ! r->used_first_case )
     case_unref (r->first_case);
 
+
   caseproto_unref (r->proto);
+  r->proto = NULL;
 
-  //  free (r);
+  xmlFree (r->target_sheet_name);
+  r->target_sheet_name = NULL;
+
+
+  ods_unref (&r->spreadsheet);
 }
 
+
+
+
+
 static void
-process_node (struct ods_reader *r)
+process_node (struct ods_reader *or, struct state_data *r)
 {
   xmlChar *name = xmlTextReaderName (r->xtr);
   if (name == NULL)
@@ -224,11 +299,9 @@ process_node (struct ods_reader *r)
       if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
          XML_READER_TYPE_ELEMENT  == r->node_type)
        {
-         printf ("%s:%d Start of Workbook %d: Rows %d\n", __FILE__, __LINE__,
-                 r->sheet_index,  r->row);
-
          r->state = STATE_SPREADSHEET;
-         r->sheet_index = -1
+         r->current_sheet = -1;
+         r->current_sheet_name = NULL;
        }
       break;
     case STATE_SPREADSHEET:
@@ -236,49 +309,31 @@ process_node (struct ods_reader *r)
          && 
          (XML_READER_TYPE_ELEMENT == r->node_type))
        {
-         xmlChar *value = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
-
+         xmlFree (r->current_sheet_name);
+         r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
 
-         ++r->sheet_index;
+         ++r->current_sheet;
 
-         printf ("%s:%d Start of SHEET %d: Allocated Sheets %d\n", __FILE__, __LINE__,
-                 r->sheet_index,  r->n_allocated_sheets);
-
-         if (r->sheet_index >= r->n_allocated_sheets)
+         if (r->current_sheet >= or->n_allocated_sheets)
            {
-             r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
-             r->sheets[r->n_allocated_sheets - 1].start_col = -1;
-             r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
-             r->sheets[r->n_allocated_sheets - 1].start_row = -1;
-             r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
-             r->sheets[r->n_allocated_sheets - 1].name = value;
+             assert (r->current_sheet == or->n_allocated_sheets);
+             or->sheets = xrealloc (or->sheets, sizeof (*or->sheets) * ++or->n_allocated_sheets);
+             or->sheets[or->n_allocated_sheets - 1].start_col = -1;
+             or->sheets[or->n_allocated_sheets - 1].stop_col = -1;
+             or->sheets[or->n_allocated_sheets - 1].start_row = -1;
+             or->sheets[or->n_allocated_sheets - 1].stop_row = -1;
+             or->sheets[or->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
            }
 
-
-
          r->col = 0;
          r->row = 0;
 
-
-         if ( r->target_sheet != NULL)
-           {
-             if ( 0 == xmlStrcmp (value, r->target_sheet))
-               {
-                 r->sheet_found = true;
-               }
-           }
-         else if (r->target_sheet_index == r->sheet_index)
-           {
-             r->sheet_found = true;
-           }
          r->state = STATE_TABLE;
        }
       else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
               XML_READER_TYPE_ELEMENT  == r->node_type)
        {
          r->state = STATE_INIT;
-         printf ("%s:%d End of Workbook %d: Rows %d Cols %d\n", __FILE__, __LINE__,
-                 r->sheet_index,  r->row, r->col);
        }
       break;
     case STATE_TABLE:
@@ -291,19 +346,18 @@ process_node (struct ods_reader *r)
          
          int row_span = value ? _xmlchar_to_int (value) : 1;
 
-         printf ("%s:%d  Start of Row %d Span %d\n", __FILE__, __LINE__,  r->row, row_span);
          r->row += row_span;
          r->col = 0;
          
          if (! xmlTextReaderIsEmptyElement (r->xtr))
            r->state = STATE_ROW;
+
+         xmlFree (value);
        }
       else if (0 == xmlStrcasecmp (name, _xml("table:table")) && 
               (XML_READER_TYPE_END_ELEMENT  == r->node_type))
        {
-         printf ("%s:%d End of SHEET %d %d,%d\n", __FILE__, __LINE__,  r->sheet_index,  r->row, r->col);
          r->state = STATE_SPREADSHEET;
-
        }
       break;
     case STATE_ROW:
@@ -315,22 +369,18 @@ process_node (struct ods_reader *r)
            xmlTextReaderGetAttribute (r->xtr,
                                       _xml ("table:number-columns-repeated"));
          
-         int col_span = value ? _xmlchar_to_int (value) : 1;
-
-         r->col += col_span;
+         r->col_span = value ? _xmlchar_to_int (value) : 1;
+         r->col += r->col_span;
 
-         printf ("%s:%d  (span %d) Start of Cell %d, %d\n", __FILE__, __LINE__,  
-                 col_span,
-                 r->row, r->col);
          if (! xmlTextReaderIsEmptyElement (r->xtr))
            r->state = STATE_CELL;
+
+         xmlFree (value);
        }
       else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
                &&
                (XML_READER_TYPE_END_ELEMENT  == r->node_type))
        {
-         /* Set the span back to the default */
-         printf ("%s:%d  End of Cell:  %d, %d\n", __FILE__, __LINE__, r->row, r->col);
          r->state = STATE_TABLE;
        }
       break;
@@ -339,7 +389,6 @@ process_node (struct ods_reader *r)
            &&
           ( XML_READER_TYPE_ELEMENT  == r->node_type))
        {
-         printf ("%s:%d  Start of Cell Contents %d\n", __FILE__, __LINE__,    r->row);
          if (! xmlTextReaderIsEmptyElement (r->xtr))
            r->state = STATE_CELL_CONTENT;
        }
@@ -349,29 +398,27 @@ process_node (struct ods_reader *r)
          (XML_READER_TYPE_END_ELEMENT  == r->node_type)
          )
        {
-         printf ("%s:%d End of Cell contents: Rows %d\n", __FILE__, __LINE__, r->row);
          r->state = STATE_ROW;
        }
       break;
     case STATE_CELL_CONTENT:
-      assert (r->sheet_index >= 0);
-      assert (r->sheet_index < r->n_allocated_sheets);
+      assert (r->current_sheet >= 0);
+      assert (r->current_sheet < or->n_allocated_sheets);
 
-      if (r->sheets[r->sheet_index].start_row == -1)
-       r->sheets[r->sheet_index].start_row = r->row - 1;
+      if (or->sheets[r->current_sheet].start_row == -1)
+       or->sheets[r->current_sheet].start_row = r->row - 1;
 
       if ( 
-         (r->sheets[r->sheet_index].start_col == -1)
+         (or->sheets[r->current_sheet].start_col == -1)
          ||
-         (r->sheets[r->sheet_index].start_col >= r->col - 1)
+         (or->sheets[r->current_sheet].start_col >= r->col - 1)
           )
-       r->sheets[r->sheet_index].start_col = r->col - 1;
+       or->sheets[r->current_sheet].start_col = r->col - 1;
 
-      r->sheets[r->sheet_index].stop_row = r->row - 1;
-      printf ("Stop Row for  %d is %d\n", r->sheet_index, r->sheets[r->sheet_index].stop_row);
+      or->sheets[r->current_sheet].stop_row = r->row - 1;
 
-      if ( r->sheets[r->sheet_index].stop_col <  r->col - 1)
-       r->sheets[r->sheet_index].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)
        r->state = STATE_CELL;
@@ -430,7 +477,7 @@ xmv_to_width (const struct xml_value *xmv, int fallback)
  */
 static void
 convert_xml_to_value (struct ccase *c, const struct variable *var,
-                     const struct xml_value *xmv)
+                     const struct xml_value *xmv, int col, int row)
 {
   union value *v = case_data_rw (c, var);
 
@@ -442,20 +489,37 @@ convert_xml_to_value (struct ccase *c, const struct variable *var,
     value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
   else
     {
-      const char *text ;
       const struct fmt_spec *fmt = var_get_write_format (var);
       enum fmt_category fc  = fmt_get_category (fmt->type);
 
       assert ( fc != FMT_CAT_STRING);
 
-      text =
-        xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
+      if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
+       {
+         v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
+       }
+      else
+       {
+         const char *text = xmv->value ?
+           CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
+
+         char *m = 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"));
+         if (m)
+           {
+             char buf [FMT_STRING_LEN_MAX + 1];
+             char *cell = create_cell_ref (col, row);
+
+             msg (MW, _("Cannot convert the value in the spreadsheet cell %s to format (%s): %s"), 
+                  cell, fmt_to_string (fmt, buf), m);
+             free (cell);
+           }
+         free (m);
+       }
     }
 }
 
@@ -472,7 +536,7 @@ get_sheet_count (struct zip_reader *zreader)
     return -1;
 
   mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
-                        (xmlInputCloseCallback) zip_member_finish,
+                        (xmlInputCloseCallback) NULL,
                         meta,   NULL, NULL, 0);
 
   while (1 == xmlTextReaderRead (mxtr))
@@ -481,14 +545,21 @@ get_sheet_count (struct zip_reader *zreader)
       if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
        {
          xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
-           
+
          if ( attr != NULL)
            {
              int s = _xmlchar_to_int (attr);
+             xmlFreeTextReader (mxtr);
+             xmlFree (name);
+             xmlFree (attr);      
              return s;
            }
+         xmlFree (attr);      
        }
+      xmlFree (name);      
     }
+
+  xmlFreeTextReader (mxtr);
   return -1;
 }
 
@@ -506,72 +577,85 @@ ods_error_handler (void *ctx, const char *mesg,
 }
 
 
-static bool
+static xmlTextReaderPtr
 init_reader (struct ods_reader *r, bool report_errors)
 {
   struct zip_member *content = zip_member_open (r->zreader, "content.xml");
   xmlTextReaderPtr xtr;
 
   if ( content == NULL)
-    return false;
-
-  zip_member_ref (content);
-
+    return NULL;
 
   xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
-                       (xmlInputCloseCallback) zip_member_finish,
+                       (xmlInputCloseCallback) NULL,
                        content,   NULL, NULL,
                        report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
 
   if ( xtr == NULL)
     return false;
 
-  r->xtr = xtr;
+
   r->spreadsheet.type = SPREADSHEET_ODS;
 
   if (report_errors) 
     xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
 
-  return true;
+  return xtr;
 }
 
 
+
 struct spreadsheet *
 ods_probe (const char *filename, bool report_errors)
 {
-  struct ods_reader *r;
-  struct string errs = DS_EMPTY_INITIALIZER;
   int sheet_count;
-  struct zip_reader *zr = zip_reader_create (filename, &errs);
+  struct ods_reader *r = xzalloc (sizeof *r);
+  xmlTextReaderPtr xtr;  
+  struct zip_reader *zr;
+
+  ds_init_empty (&r->zip_errs);
+
+  zr = zip_reader_create (filename, &r->zip_errs);
 
   if (zr == NULL)
-    return NULL;
+    {
+      if (report_errors)
+       {
+         msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
+              filename, ds_cstr (&r->zip_errs));
+       }
+      ds_destroy (&r->zip_errs);
+      free (r);
+      return NULL;
+    }
 
   sheet_count = get_sheet_count (zr);
 
-  r = xzalloc (sizeof *r);
   r->zreader = zr;
+  r->spreadsheet.ref_cnt = 1;
 
-  if (! init_reader (r, false))
+  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;
+
 
   r->spreadsheet.n_sheets = sheet_count;
   r->n_allocated_sheets = 0;
-  r->sheet_index = -1;
   r->sheets = NULL;
 
-  ds_destroy (&errs);
-
-  printf ("%s:%d\n", __FILE__, __LINE__);
-
-  r->spreadsheet.file_name = filename;
+  r->spreadsheet.file_name = strdup (filename);
   return &r->spreadsheet;
 
  error:
+  ds_destroy (&r->zip_errs);
   zip_reader_destroy (r->zreader);
-  ds_destroy (&errs);
   free (r);
   return NULL;
 }
@@ -587,6 +671,7 @@ 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;
@@ -594,16 +679,22 @@ ods_make_reader (struct spreadsheet *spreadsheet,
   assert (r);
   r->read_names = opts->read_names;
   ds_init_empty (&r->ods_errs);
+  ++r->spreadsheet.ref_cnt;
 
-
-  printf ("%s:%d\n", __FILE__, __LINE__);
-
-  if ( !init_reader (r, true))
+  xtr = init_reader (r, true);
+  if ( xtr == NULL)
     goto error;
 
-  printf ("%s:%d\n", __FILE__, __LINE__);
+  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;
 
-  if ( opts->cell_range )
+  if (opts->cell_range)
     {
       if ( ! convert_cell_ref (opts->cell_range,
                               &r->start_col, &r->start_row,
@@ -622,41 +713,19 @@ ods_make_reader (struct spreadsheet *spreadsheet,
       r->stop_row = -1;
     }
 
-  r->state = STATE_INIT;
-  r->target_sheet = BAD_CAST opts->sheet_name;
+  r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
   r->target_sheet_index = opts->sheet_index;
-  r->row = r->col = 0;
-  r->sheet_index = 0;
-
-  printf ("%s:%d\n", __FILE__, __LINE__);
-
-
-  /* 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);
-    }
-
-  printf ("%s:%d Number of var specs %d\n", __FILE__, __LINE__, n_var_specs);
-  printf ("%s:%d Target Sjheet %d\n", __FILE__, __LINE__, r->target_sheet_index);
-
 
   /* Advance to the start of the cells for the target sheet */
-  while ( r->sheet_index < r->target_sheet_index - 1 ||
-         r->state != STATE_TABLE
-         )
+  while ( ! reading_target_sheet (r, &r->rsd)  
+         || r->rsd.state != STATE_ROW || r->rsd.row <= r->start_row )
     {
-      if (1 != (ret = xmlTextReaderRead (r->xtr)))
+      if (1 != (ret = xmlTextReaderRead (r->rsd.xtr)))
           break;
 
-      process_node (r);
+      process_node (r, &r->rsd);
     }
 
-  printf ("%s:%d\n", __FILE__, __LINE__);
-
   if (ret < 1)
     {
       msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
@@ -664,27 +733,31 @@ ods_make_reader (struct spreadsheet *spreadsheet,
       goto error;
     }
 
-  printf ("%s:%d\n", __FILE__, __LINE__);
-
   if ( opts->read_names)
     {
-      while (1 == (ret = xmlTextReaderRead (r->xtr)))
+      while (1 == xmlTextReaderRead (r->rsd.xtr))
        {
          int idx;
 
-         process_node (r);
+         process_node (r, &r->rsd);
 
          /* If the row is finished then stop for now */
-         if (r->state == STATE_TABLE && r->row > r->start_row)
+         if (r->rsd.state == STATE_TABLE && r->rsd.row > r->start_row)
            break;
-         
-         idx = r->col - r->start_col - 1;
 
-         if (r->state == STATE_CELL_CONTENT 
+         idx = r->rsd.col - r->start_col -1 ;
+
+         if ( idx < 0)
+           continue;
+
+         if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
+           continue;
+
+         if (r->rsd.state == STATE_CELL_CONTENT 
              &&
-             XML_READER_TYPE_TEXT  == r->node_type)
+             XML_READER_TYPE_TEXT  == r->rsd.node_type)
            {
-             xmlChar *value = xmlTextReaderValue (r->xtr);
+             xmlChar *value = xmlTextReaderValue (r->rsd.xtr);
 
              if ( idx >= n_var_specs)
                {
@@ -693,7 +766,7 @@ ods_make_reader (struct spreadsheet *spreadsheet,
                  /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
                  memset (var_spec + n_var_specs,
                          0, 
-                         (n_var_specs - idx + 1) * sizeof (*var_spec));
+                         (idx - n_var_specs + 1) * sizeof (*var_spec));
                  n_var_specs = idx + 1;
                }
              var_spec[idx].firstval.text = 0;
@@ -701,36 +774,56 @@ ods_make_reader (struct spreadsheet *spreadsheet,
              var_spec[idx].firstval.type = 0;
 
              var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
+
              xmlFree (value);
            }
        }
     }
 
-
   /* Read in the first row of data */
-  while (1 == xmlTextReaderRead (r->xtr))
+  while (1 == xmlTextReaderRead (r->rsd.xtr))
     {
       int idx;
-      process_node (r);
+      process_node (r, &r->rsd);
+
+      if ( ! reading_target_sheet (r, &r->rsd) )
+       break;
 
       /* If the row is finished then stop for now */
-      if (r->state == STATE_TABLE && r->row > r->start_row + 1)
+      if (r->rsd.state == STATE_TABLE &&
+         r->rsd.row > r->start_row + (opts->read_names ? 1 : 0))
        break;
 
-      idx = r->col - r->start_col - 1;
+      idx = r->rsd.col - r->start_col - 1;
+      if (idx < 0)
+       continue;
+
+      if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
+       continue;
 
-      if ( r->state == STATE_CELL &&
-          XML_READER_TYPE_ELEMENT  == r->node_type)
+      if ( r->rsd.state == STATE_CELL &&
+          XML_READER_TYPE_ELEMENT  == r->rsd.node_type)
        {
-         type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
-         val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
+         type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
+         val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
        }
 
-      if ( r->state == STATE_CELL_CONTENT &&
-          XML_READER_TYPE_TEXT  == r->node_type)
+      if ( r->rsd.state == STATE_CELL_CONTENT &&
+          XML_READER_TYPE_TEXT  == r->rsd.node_type)
        {
+         if (idx >= n_var_specs)
+           {
+             var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
+             memset (var_spec + n_var_specs,
+                     0, 
+                     (idx - n_var_specs + 1) * sizeof (*var_spec));
+
+             var_spec [idx].name = NULL;
+             n_var_specs = idx + 1;
+           }
+
          var_spec [idx].firstval.type = type;
-         var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
+         var_spec [idx].firstval.text = xmlTextReaderValue (r->rsd.xtr);
          var_spec [idx].firstval.value = val_string;
 
          val_string = NULL;
@@ -738,9 +831,10 @@ ods_make_reader (struct spreadsheet *spreadsheet,
        }
     }
 
+
   /* Create the dictionary and populate it */
   r->spreadsheet.dict = r->dict = dict_create (
-    CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
+    CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->rsd.xtr)));
 
   for (i = 0; i < n_var_specs ; ++i )
     {
@@ -765,9 +859,6 @@ ods_make_reader (struct spreadsheet *spreadsheet,
       var_set_both_formats (var, &fmt);
     }
 
-  /* Create the first case, and cache it */
-  r->used_first_case = false;
-
   if ( n_var_specs ==  0 )
     {
       msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
@@ -775,24 +866,30 @@ ods_make_reader (struct spreadsheet *spreadsheet,
       goto error;
     }
 
+  /* Create the first case, and cache it */
   r->proto = caseproto_ref (dict_get_proto (r->dict));
   r->first_case = case_create (r->proto);
   case_set_missing (r->first_case);
 
-  printf ("%s:%d Hello %d\n", __FILE__, __LINE__, n_var_specs);
-
-  for ( i = 0 ; i < n_var_specs ; ++i )
+  for (i = 0 ; i < n_var_specs; ++i)
     {
       const struct variable *var = dict_get_var (r->dict, i);
 
-      convert_xml_to_value (r->first_case, var,  &var_spec[i].firstval);
+      convert_xml_to_value (r->first_case, var,  &var_spec[i].firstval,
+                           r->rsd.col - n_var_specs + i,
+                           r->rsd.row - 1);
     }
 
-  printf ("%s:%d Hello %d\n", __FILE__, __LINE__, n_var_specs);
+  /* Read in the first row of data */
+  while (1 == xmlTextReaderRead (r->rsd.xtr))
+    {
+      process_node (r, &r->rsd);
+
+      if (r->rsd.state == STATE_ROW)
+       break;
+    }
 
-  //  zip_reader_destroy (zreader);
 
-#if 0
   for ( i = 0 ; i < n_var_specs ; ++i )
     {
       free (var_spec[i].firstval.type);
@@ -802,9 +899,8 @@ ods_make_reader (struct spreadsheet *spreadsheet,
     }
 
   free (var_spec);
-#endif
 
-  printf ("%s:%d Creating reader\n", __FILE__, __LINE__);
+
   return casereader_create_sequential
     (NULL,
      r->proto,
@@ -813,8 +909,6 @@ ods_make_reader (struct spreadsheet *spreadsheet,
 
  error:
   
-  // zip_reader_destroy (zreader);
-
   for ( i = 0 ; i < n_var_specs ; ++i )
     {
       free (var_spec[i].firstval.type);
@@ -829,7 +923,6 @@ ods_make_reader (struct spreadsheet *spreadsheet,
   r->spreadsheet.dict = NULL;
   ods_file_casereader_destroy (NULL, r);
 
-
   return NULL;
 }
 
@@ -840,92 +933,90 @@ static struct ccase *
 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
 {
   struct ccase *c = NULL;
-  xmlChar *val_string = NULL;
   struct ods_reader *r = r_;
-  int current_row = r->row;
 
-  printf ("%s:%d Reading CASE\n", __FILE__, __LINE__);
+  xmlChar *val_string = NULL;
+  xmlChar *type = NULL;
 
-  if ( !r->used_first_case )
+  if (!r->used_first_case)
     {
       r->used_first_case = true;
       return r->first_case;
     }
 
-  printf ("%s:%d Reading CASE\n", __FILE__, __LINE__);
 
-  if ( r->state > STATE_INIT)
+  /* Advance to the start of a row. (If there is one) */
+  while (r->rsd.state != STATE_ROW 
+        && 1 == xmlTextReaderRead (r->rsd.xtr)
+        )
     {
-      c = case_create (r->proto);
-      case_set_missing (c);
+      process_node (r, &r->rsd);
     }
 
-  if (r->state == STATE_SPREADSHEET)
+
+  if ( ! reading_target_sheet (r, &r->rsd)  
+       ||  r->rsd.state < STATE_TABLE
+       ||  (r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
+       )
     {
-      printf ("%s:%d CASES ARE ENDED\n", __FILE__, __LINE__);
       return NULL;
     }
 
-  while (1 == xmlTextReaderRead (r->xtr))
+  c = case_create (r->proto);
+  case_set_missing (c);
+  
+  while (1 == xmlTextReaderRead (r->rsd.xtr))
     {
-      process_node (r);
-      if ( r->row > current_row)
-       {
-         break;
-       }
-      if ( r->col < r->start_col || (r->stop_col != -1 && r->col > r->stop_col))
-       {
-         continue;
-       }
-      if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
-       {
-         continue;
-       }
-      if ( r->stop_row != -1 && r->row > r->stop_row)
-       {
-         continue;
-       }
-      if ( r->state == STATE_CELL &&
-          r->node_type == XML_READER_TYPE_ELEMENT )
+      process_node (r, &r->rsd);
+
+      if ( r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
+       break;
+
+      if (r->rsd.state == STATE_CELL &&
+          r->rsd.node_type == XML_READER_TYPE_ELEMENT)
        {
-         val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
+         type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
+         val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
        }
 
-      if ( r->state == STATE_CELL_CONTENT && r->node_type == XML_READER_TYPE_TEXT )
+      if (r->rsd.state == STATE_CELL_CONTENT && 
+          r->rsd.node_type == XML_READER_TYPE_TEXT)
        {
          int col;
          struct xml_value *xmv = xzalloc (sizeof *xmv);
-         xmv->text = xmlTextReaderValue (r->xtr);
-         xmv->value = val_string;
+         xmv->text = xmlTextReaderValue (r->rsd.xtr);
+         xmv->value = val_string;       
          val_string = NULL;
+         xmv->type = type;
+         type = NULL;
 
-         /*
-         for (col = 0; col < r->span ; ++col)
+         for (col = 0; col < r->rsd.col_span; ++col)
            {
-             const int idx = r->col + col - r->start_col;
-
-             const struct variable *var = dict_get_var (r->dict, idx);
-
-             convert_xml_to_value (c, var, xmv);
+             const struct variable *var;
+             const int idx = r->rsd.col - col - r->start_col - 1;
+             if (idx < 0)
+               continue;
+             if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
+               break;
+             if (idx >= dict_get_var_cnt (r->dict))
+               break;
+
+              var = dict_get_var (r->dict, idx);
+             convert_xml_to_value (c, var, xmv, idx + r->start_col, r->rsd.row - 1);
            }
-         */
-         free (xmv->text);
-         free (xmv->value);
+
+         xmlFree (xmv->text);
+         xmlFree (xmv->value);
+         xmlFree (xmv->type);
          free (xmv);
        }
-
-      if ( r->state < STATE_TABLE)
+      if ( r->rsd.state <= STATE_TABLE)
        break;
     }
 
-  if (NULL == c || (r->stop_row != -1 && r->row > r->stop_row + 1))
-    {
-      case_unref (c);
-      return NULL;
-    }
-  else
-    {
-      return c;
-    }
+  xmlFree (type);
+  xmlFree (val_string);
+  
+  return c;
 }
 #endif