Renamed some variables and removed some
[pspp] / src / data / ods-reader.c
index 7079027f3d4d926354935f81c7a545e529245f8d..640e08d8f871f2755052133caf916d20185498b2 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2011, 2012 Free Software Foundation, Inc.
+   Copyright (C) 2011, 2012, 2013 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
@@ -86,9 +86,6 @@ struct sheet_detail
   int stop_col;
   int start_row;
   int stop_row;
-
-  int maxcol;
-  int maxrow;
 };
 
 
@@ -105,7 +102,7 @@ enum reader_state
 struct ods_reader
 {
   struct spreadsheet spreadsheet;
-
+  struct zip_reader *zreader;
   xmlTextReaderPtr xtr;
 
   enum reader_state state;
@@ -113,17 +110,23 @@ struct ods_reader
   int row;
   int col;
   int node_type;
-  int sheet_index;
-  int max_col;
-  int min_col;
+  int current_sheet;
 
   const xmlChar *target_sheet;
   int target_sheet_index;
 
+
+  int wanted_row_start;
+  int wanted_col_start;
+
+#if 0
   int start_row;
   int start_col;
   int stop_row;
   int stop_col;
+#endif
+
+  int col_span;
 
   struct sheet_detail *sheets;
   int n_allocated_sheets;
@@ -164,13 +167,15 @@ ods_get_sheet_name (struct spreadsheet *s, int n)
 char *
 ods_get_sheet_range (struct spreadsheet *s, int n)
 {
-  int ret;
+  int ret = -1;
   struct ods_reader *or = (struct ods_reader *) s;
   
   assert (n < s->n_sheets);
 
   while ( 
-        (or->n_allocated_sheets <= n || or->sheets[n].stop_col == -1)
+        (
+         (or->n_allocated_sheets <= n)
+         || (or->sheets[n].stop_row == -1) )
         && 
         (1 == (ret = xmlTextReaderRead (or->xtr)))
          )
@@ -178,6 +183,7 @@ ods_get_sheet_range (struct spreadsheet *s, int n)
       process_node (or);
     }
 
+
   return create_cell_ref (
                          or->sheets[n].start_col,
                          or->sheets[n].start_row,
@@ -195,6 +201,7 @@ ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
 
   if (r->xtr)
     xmlFreeTextReader (r->xtr);
+  r->xtr = NULL;
 
   if ( ! ds_is_empty (&r->ods_errs))
     msg (ME, "%s", ds_cstr (&r->ods_errs));
@@ -206,7 +213,7 @@ ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
 
   caseproto_unref (r->proto);
 
-  free (r);
+  //  free (r);
 }
 
 static void
@@ -225,10 +232,8 @@ 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->current_sheet = -1;
        }
       break;
     case STATE_SPREADSHEET:
@@ -237,20 +242,21 @@ process_node (struct ods_reader *r)
          (XML_READER_TYPE_ELEMENT == r->node_type))
        {
          xmlChar *value = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
-         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;
-         r->col = -1;
-         r->row = -1;
-         r->max_col = -1;
-         r->min_col = INT_MAX;
-         ++r->sheet_index;
-
-         printf ("%s:%d Start of SHEET %d: Rows %d\n", __FILE__, __LINE__,
-                 r->sheet_index,  r->row);
+
+         ++r->current_sheet;
+
+         if (r->current_sheet >= r->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;
+           }
+
+         r->col = 0;
+         r->row = 0;
 
          if ( r->target_sheet != NULL)
            {
@@ -259,7 +265,7 @@ process_node (struct ods_reader *r)
                  r->sheet_found = true;
                }
            }
-         else if (r->target_sheet_index == r->sheet_index)
+         else if (r->target_sheet_index == r->current_sheet)
            {
              r->sheet_found = true;
            }
@@ -270,7 +276,7 @@ process_node (struct ods_reader *r)
        {
          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);
+                 r->current_sheet,  r->row, r->col);
        }
       break;
     case STATE_TABLE:
@@ -283,9 +289,8 @@ 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 = -1;
+         r->col = 0;
          
          if (! xmlTextReaderIsEmptyElement (r->xtr))
            r->state = STATE_ROW;
@@ -293,12 +298,7 @@ process_node (struct ods_reader *r)
       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->sheets[r->sheet_index].stop_row = r->row;
-         r->sheets[r->sheet_index].stop_col = r->max_col - 1;
-         r->sheets[r->sheet_index].start_col = r->min_col;
          r->state = STATE_SPREADSHEET;
-
        }
       break;
     case STATE_ROW:
@@ -310,15 +310,9 @@ process_node (struct ods_reader *r)
            xmlTextReaderGetAttribute (r->xtr,
                                       _xml ("table:number-columns-repeated"));
          
-         int col_span = value ? _xmlchar_to_int (value) : 0;
-
-         r->col += col_span;
-         r->col ++;
+         r->col_span = value ? _xmlchar_to_int (value) : 1;
+         r->col += r->col_span;
 
-         if (r->min_col > r->col)
-           r->min_col = r->col;
-
-         printf ("%s:%d  Start of Cell %d, %d\n", __FILE__, __LINE__,    r->row, r->col);
          if (! xmlTextReaderIsEmptyElement (r->xtr))
            r->state = STATE_CELL;
        }
@@ -326,10 +320,6 @@ process_node (struct ods_reader *r)
                &&
                (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);
-         if ( r->max_col < r->col)
-           r->max_col = r->col;
          r->state = STATE_TABLE;
        }
       break;
@@ -338,7 +328,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;
        }
@@ -348,16 +337,30 @@ 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:
-      if (r->sheets[r->sheet_index].start_row == -1)
-       r->sheets[r->sheet_index].start_row = r->row;
-      //      printf ("%s:%d Cell contents: Rows %d\n", __FILE__, __LINE__, r->row);
-      /* if (XML_READER_TYPE_TEXT != r->node_type) */
-      r->state = STATE_CELL;
+      assert (r->current_sheet >= 0);
+      assert (r->current_sheet < r->n_allocated_sheets);
+
+      if (r->sheets[r->current_sheet].start_row == -1)
+       r->sheets[r->current_sheet].start_row = r->row - 1;
+
+      if ( 
+         (r->sheets[r->current_sheet].start_col == -1)
+         ||
+         (r->sheets[r->current_sheet].start_col >= r->col - 1)
+          )
+       r->sheets[r->current_sheet].start_col = r->col - 1;
+
+      r->sheets[r->current_sheet].stop_row = r->row - 1;
+
+      if ( r->sheets[r->current_sheet].stop_col <  r->col - 1)
+       r->sheets[r->current_sheet].stop_col = r->col - 1;
+
+      if (XML_READER_TYPE_END_ELEMENT  == r->node_type)
+       r->state = STATE_CELL;
       break;
     default:
       NOT_REACHED ();
@@ -489,32 +492,17 @@ ods_error_handler (void *ctx, const char *mesg,
 }
 
 
-struct spreadsheet *
-ods_probe (const char *filename, bool report_errors)
+static bool
+init_reader (struct ods_reader *r, bool report_errors)
 {
-  struct ods_reader *r;
-  struct string errs;
-  xmlTextReaderPtr xtr ;
-  int sheet_count;
-  struct zip_member *content = NULL;
-
-  struct zip_reader *zreader = NULL;
-
-  ds_init_empty (&errs);
-
-  zreader = zip_reader_create (filename, &errs);
-
-  if (zreader == NULL)
-    return NULL;
-
-  content = zip_member_open (zreader, "content.xml");
+  struct zip_member *content = zip_member_open (r->zreader, "content.xml");
+  xmlTextReaderPtr xtr;
 
   if ( content == NULL)
-    goto error;
+    return false;
 
   zip_member_ref (content);
 
-  sheet_count = get_sheet_count (zreader);
 
   xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
                        (xmlInputCloseCallback) zip_member_finish,
@@ -522,30 +510,52 @@ ods_probe (const char *filename, bool report_errors)
                        report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
 
   if ( xtr == NULL)
-    goto error;
+    return false;
 
-  r = xzalloc (sizeof *r);
   r->xtr = xtr;
   r->spreadsheet.type = SPREADSHEET_ODS;
-  r->spreadsheet.n_sheets = sheet_count;
-  r->n_allocated_sheets = 0;
-  r->sheet_index = -1;
-  r->sheets = NULL;
 
   if (report_errors) 
     xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
 
+  return true;
+}
 
-  ds_destroy (&errs);
 
-  printf ("%s:%d\n", __FILE__, __LINE__);
+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);
+
+  if (zr == NULL)
+    return NULL;
+
+  sheet_count = get_sheet_count (zr);
+
+  r = xzalloc (sizeof *r);
+  r->zreader = zr;
+
+  if (! init_reader (r, report_errors))
+    {
+      goto error;
+    }
+
+  r->spreadsheet.n_sheets = sheet_count;
+  r->n_allocated_sheets = 0;
+  r->sheets = NULL;
+
+  ds_destroy (&errs);
 
   r->spreadsheet.file_name = filename;
   return &r->spreadsheet;
 
  error:
-  zip_reader_destroy (zreader);
+  zip_reader_destroy (r->zreader);
   ds_destroy (&errs);
+  free (r);
   return NULL;
 }
 
@@ -568,6 +578,11 @@ ods_make_reader (struct spreadsheet *spreadsheet,
   r->read_names = opts->read_names;
   ds_init_empty (&r->ods_errs);
 
+
+  if ( !init_reader (r, true))
+    goto error;
+
+#if 0
   if ( opts->cell_range )
     {
       if ( ! convert_cell_ref (opts->cell_range,
@@ -586,14 +601,14 @@ ods_make_reader (struct spreadsheet *spreadsheet,
       r->stop_col = -1;
       r->stop_row = -1;
     }
+#endif
 
   r->state = STATE_INIT;
   r->target_sheet = BAD_CAST opts->sheet_name;
   r->target_sheet_index = opts->sheet_index;
-  r->row = r->col = -1;
-  r->sheet_index = 0;
-
+  r->row = r->col = 0;
 
+#if 0
   /* If CELLRANGE was given, then we know how many variables should be read */
   if ( r->stop_col != -1 )
     {
@@ -602,10 +617,12 @@ ods_make_reader (struct spreadsheet *spreadsheet,
       var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
       memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
     }
-
+#endif
 
   /* Advance to the start of the cells for the target sheet */
-  while ( (r->row < r->start_row ))
+  while ( r->current_sheet < r->target_sheet_index - 1 ||
+         r->state != STATE_TABLE
+         )
     {
       if (1 != (ret = xmlTextReaderRead (r->xtr)))
           break;
@@ -613,6 +630,7 @@ ods_make_reader (struct spreadsheet *spreadsheet,
       process_node (r);
     }
 
+
   if (ret < 1)
     {
       msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
@@ -625,26 +643,23 @@ ods_make_reader (struct spreadsheet *spreadsheet,
       while (1 == (ret = xmlTextReaderRead (r->xtr)))
        {
          int idx;
+
          process_node (r);
-         if ( r->row > r->start_row)
-           break;
 
-         if (r->col == -1 && r->row == r->start_row)
+         /* If the row is finished then stop for now */
+         if (r->state == STATE_TABLE && r->row > r->wanted_row_start)
            break;
 
-         if ( r->col < r->start_col)
-           continue;
-
-         idx = r->col - r->start_col;
+         idx = r->col - r->wanted_col_start - 1;
 
          if (r->state == STATE_CELL_CONTENT 
              &&
              XML_READER_TYPE_TEXT  == r->node_type)
            {
              xmlChar *value = xmlTextReaderValue (r->xtr);
+
              if ( idx >= n_var_specs)
                {
-
                  var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
 
                  /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
@@ -658,8 +673,7 @@ ods_make_reader (struct spreadsheet *spreadsheet,
              var_spec[idx].firstval.type = 0;
 
              var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
-             free (value);
-             value = NULL;
+             xmlFree (value);
            }
        }
     }
@@ -669,16 +683,12 @@ ods_make_reader (struct spreadsheet *spreadsheet,
     {
       int idx;
       process_node (r);
-      if ( r->row >= r->start_row + 1 + opts->read_names)
-       break;
-
-      if ( r->col < r->start_col)
-       continue;
 
-      if ( r->col - r->start_col + 1 > n_var_specs)
-       continue;
+      /* If the row is finished then stop for now */
+      if (r->state == STATE_TABLE && r->row > r->wanted_row_start + (opts->read_names ? 1 : 0))
+       break;
 
-      idx = r->col - r->start_col;
+      idx = r->col - r->wanted_col_start - 1;
 
       if ( r->state == STATE_CELL &&
           XML_READER_TYPE_ELEMENT  == r->node_type)
@@ -690,9 +700,17 @@ ods_make_reader (struct spreadsheet *spreadsheet,
       if ( r->state == STATE_CELL_CONTENT &&
           XML_READER_TYPE_TEXT  == r->node_type)
        {
+         if ( idx >= n_var_specs) 
+           {
+             var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
+             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.value = val_string;
+
          val_string = NULL;
          type = NULL;
        }
@@ -702,7 +720,7 @@ ods_make_reader (struct spreadsheet *spreadsheet,
   r->spreadsheet.dict = r->dict = dict_create (
     CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
 
-  for (i = 0 ; i < n_var_specs ; ++i )
+  for (i = 0; i < n_var_specs ; ++i )
     {
       struct fmt_spec fmt;
       struct variable *var = NULL;
@@ -739,15 +757,25 @@ ods_make_reader (struct spreadsheet *spreadsheet,
   r->first_case = case_create (r->proto);
   case_set_missing (r->first_case);
 
-  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);
     }
 
+  /* Read in the first row of data */
+  while (1 == xmlTextReaderRead (r->xtr))
+    {
+      process_node (r);
+
+      if (r->state == STATE_ROW)
+       break;
+    }
+
   //  zip_reader_destroy (zreader);
 
+#if 0
   for ( i = 0 ; i < n_var_specs ; ++i )
     {
       free (var_spec[i].firstval.type);
@@ -757,6 +785,7 @@ ods_make_reader (struct spreadsheet *spreadsheet,
     }
 
   free (var_spec);
+#endif
 
   return casereader_create_sequential
     (NULL,
@@ -797,9 +826,6 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
   struct ods_reader *r = r_;
   int current_row = r->row;
 
-  if ( r->row == -1)
-    return NULL;
-
   if ( !r->used_first_case )
     {
       r->used_first_case = true;
@@ -812,32 +838,29 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
       case_set_missing (c);
     }
 
+  if (r->state == STATE_SPREADSHEET 
+      && 
+      r->current_sheet == r->target_sheet_index - 1
+      )
+    {
+      return NULL;
+    }
+
   while (1 == xmlTextReaderRead (r->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->row > current_row && r->state == STATE_ROW)
+       break;
+
       if ( r->state == STATE_CELL &&
           r->node_type == XML_READER_TYPE_ELEMENT )
        {
          val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
        }
 
-      if ( r->state == STATE_CELL_CONTENT && r->node_type == XML_READER_TYPE_TEXT )
+      if ( r->state == STATE_CELL_CONTENT && 
+          r->node_type == XML_READER_TYPE_TEXT )
        {
          int col;
          struct xml_value *xmv = xzalloc (sizeof *xmv);
@@ -845,16 +868,13 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
          xmv->value = val_string;
          val_string = NULL;
 
-         /*
-         for (col = 0; col < r->span ; ++col)
+         for (col = 0; col < r->col_span; ++col)
            {
-             const int idx = r->col + col - r->start_col;
-
+             const int idx = r->col + col - r->wanted_col_start - 1;
              const struct variable *var = dict_get_var (r->dict, idx);
-
              convert_xml_to_value (c, var, xmv);
            }
-         */
+
          free (xmv->text);
          free (xmv->value);
          free (xmv);
@@ -864,7 +884,7 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
        break;
     }
 
-  if (NULL == c || (r->stop_row != -1 && r->row > r->stop_row + 1))
+  if (NULL == c)
     {
       case_unref (c);
       return NULL;