Got some of the tests working
[pspp] / src / data / gnumeric-reader.c
index 201ef599a16f7466fd589caa6340b75b46c97a4a..514616ace74b59749cef5930344533a7bda51683 100644 (file)
 
 #include "spreadsheet-reader.h"
 
-#include "c-xvasprintf.h"
-
 #if !GNM_SUPPORT
 
 struct casereader *
-gnumeric_open_reader (struct spreadsheet_read_info *gri, struct spreadsheet_read_options *opts, struct dictionary **dict)
+gnumeric_open_reader (const struct spreadsheet_read_options *opts, struct dictionary **dict)
 {
   msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "Gnumeric");
 
@@ -89,7 +87,8 @@ enum reader_state
 
 struct sheet_detail
 {
-  xmlChar *name;
+  /* The name of the sheet (utf8 encoding) */
+  char *name;
 
   int start_col;
   int stop_col;
@@ -140,13 +139,14 @@ gnumeric_get_sheet_name (struct spreadsheet *s, int n)
   struct gnumeric_reader *gr = (struct gnumeric_reader *) s;
   assert (n < s->n_sheets);
 
-  return gr->sheets[n].name; // Kludge: Assumes the encoding is utf8
+  return gr->sheets[n].name; 
 }
 
 
 static void process_node (struct gnumeric_reader *r);
 
 
+
 char *
 gnumeric_get_sheet_range (struct spreadsheet *s, int n)
 {
@@ -208,10 +208,9 @@ process_node (struct gnumeric_reader *r)
   if (name == NULL)
     name = xmlStrdup (_xml ("--"));
 
-
   r->node_type = xmlTextReaderNodeType (r->xtr);
 
-  switch ( r->state)
+  switch (r->state)
     {
     case STATE_PRE_INIT:
       r->current_sheet = -1;
@@ -244,7 +243,7 @@ process_node (struct gnumeric_reader *r)
        }
       else if (XML_READER_TYPE_TEXT == r->node_type)
        {
-         r->sheets [r->spreadsheet.n_sheets - 1].name = xmlTextReaderValue (r->xtr);
+         r->sheets [r->spreadsheet.n_sheets - 1].name = CHAR_CAST (char *, xmlTextReaderValue (r->xtr));
        }
       break;
 
@@ -439,7 +438,8 @@ gnumeric_error_handler (void *ctx, const char *mesg,
 {
   struct gnumeric_reader *r = ctx;
        
-  msg (MW, _("There was a problem whilst reading the Gnumeric file `%s' (near line %d): `%s'"),
+  msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
+       "Gnumeric",
        r->spreadsheet.file_name,
        xmlTextReaderLocatorLineNumber (loc),
        mesg);
@@ -505,6 +505,7 @@ gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_erro
       process_node (r);
     }
 
+
   if ( ret != 1)
     {
       /* Does not seem to be a gnumeric file */
@@ -515,6 +516,22 @@ gnumeric_reopen (struct gnumeric_reader *r, const char *filename, bool show_erro
 
   r->spreadsheet.type = SPREADSHEET_GNUMERIC;
 
+  if (show_errors)
+    {
+      const xmlChar *enc = xmlTextReaderConstEncoding (r->xtr);
+      xmlCharEncoding xce = xmlParseCharEncoding (CHAR_CAST (const char *, enc));
+
+      if ( XML_CHAR_ENCODING_UTF8 != xce)
+       {
+         /* I have been told that ALL gnumeric files are UTF8 encoded.  If that is correct, this 
+            can never happen. */
+         msg (MW, _("The gnumeric file `%s' is encoded as %s instead of the usual UTF-8 encoding. "
+                    "Any non-ascii characters will be incorrectly imported."),
+              r->spreadsheet.file_name,
+              enc);
+       }
+    }
+
   return r;
 }
 
@@ -530,9 +547,9 @@ gnumeric_probe (const char *filename, bool report_errors)
 
 struct casereader *
 gnumeric_make_reader (struct spreadsheet *spreadsheet,
-                     const struct spreadsheet_read_info *gri, 
-                     struct spreadsheet_read_options *opts)
+                     const struct spreadsheet_read_options *opts)
 {
+  int x = 0;
   struct gnumeric_reader *r = NULL;
   unsigned long int vstart = 0;
   int ret;
@@ -592,7 +609,7 @@ gnumeric_make_reader (struct spreadsheet *spreadsheet,
       n_cases = MIN (n_cases, r->stop_row - r->start_row + 1);
     }
 
-  if ( gri->read_names )
+  if ( opts->read_names )
     {
       r->start_row++;
       n_cases --;
@@ -636,7 +653,7 @@ gnumeric_make_reader (struct spreadsheet *spreadsheet,
 
          if ( r->row < r->start_row)
            {
-             if ( gri->read_names )
+             if ( opts->read_names )
                {
                  var_spec [idx].name = xstrdup (text);
                }
@@ -646,8 +663,8 @@ gnumeric_make_reader (struct spreadsheet *spreadsheet,
              var_spec [idx].first_value = xmlStrdup (value);
 
              if (-1 ==  var_spec [idx].width )
-               var_spec [idx].width = (gri->asw == -1) ?
-                 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : gri->asw;
+               var_spec [idx].width = (opts->asw == -1) ?
+                 ROUND_UP (strlen(text), SPREADSHEET_DEFAULT_WIDTH) : opts->asw;
            }
 
          free (value);
@@ -707,13 +724,15 @@ gnumeric_make_reader (struct spreadsheet *spreadsheet,
   r->first_case = case_create (r->proto);
   case_set_missing (r->first_case);
 
-  int x = 0;
+
   for ( i = 0 ; i < n_var_specs ; ++i )
     {
+      const struct variable *var;
+
       if ( (var_spec[i].name == NULL) && (var_spec[i].first_value == NULL))
        continue;
 
-      const struct variable *var = dict_get_var (r->dict, x++);
+      var = dict_get_var (r->dict, x++);
 
       convert_xml_string_to_value (r->first_case, var,
                                   var_spec[i].first_value);
@@ -727,16 +746,7 @@ gnumeric_make_reader (struct spreadsheet *spreadsheet,
 
   free (var_spec);
   
-  
-  if (opts->cell_range == NULL)
-    {
-      opts->cell_range = c_xasprintf ("%c%d:%c%ld", 
-                                      r->start_col + 'A',
-                                      r->start_row,
-                                      r->stop_col + 'A' + caseproto_get_n_widths (r->proto),
-                                      r->start_row + n_cases);
-    }
-  
+
   return casereader_create_sequential
     (NULL,
      r->proto,