Fixed bug reporting the significance of paired value t-test.
[pspp-builds.git] / src / data / gnumeric-reader.c
index c36a6832537a086ddd0123ad509210c213ef9b2f..522176640489c87ec57027115582cbb9bc26d617 100644 (file)
@@ -53,8 +53,6 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
 #include <data/case.h>
 #include <data/value.h>
 
-#include <gl/mbswidth.h>
-
 #include "gnumeric-reader.h"
 #include <data/identifier.h>
 #include <assert.h>
@@ -65,7 +63,7 @@ static void gnm_file_casereader_destroy (struct casereader *, void *);
 static bool gnm_file_casereader_read (struct casereader *, void *,
                                      struct ccase *);
 
-static struct casereader_class gnm_file_casereader_class =
+static const struct casereader_class gnm_file_casereader_class =
   {
     gnm_file_casereader_read,
     gnm_file_casereader_destroy,
@@ -310,66 +308,6 @@ process_node (struct gnumeric_reader *r)
 }
 
 
-
-/*
-  Change SUGGESTION until it's a valid name that can be added to DICT.
-*/
-static void
-devise_name (const struct dictionary *dict, struct string *name, int *x)
-{
-  struct string basename;
-  if ( ds_is_empty (name))
-    ds_init_cstr (&basename, "var");
-  else
-    ds_init_string (&basename, name);
-  do
-    {
-      ds_clear (name);
-      ds_put_format (name, "%s%d", ds_cstr (&basename), ++(*x));
-    }
-  while (NULL != dict_lookup_var (dict, ds_cstr (name)) );
-
-  ds_destroy (&basename);
-}
-
-/*
-   Mutate NAME of a variable, which is gauranteed to be valid for the
-   dictionary DICT.
-*/
-static void
-munge_name (const struct dictionary *dict, struct string *name)
-{
-  int x = 0;
-
-  if (! ds_is_empty (name))
-    {
-      /* Change all the invalid characters to valid ones */
-      char *s;
-
-      s = ds_data (name);
-
-      if ( !lex_is_id1 (*s))
-       *s = '@';
-
-      s++;
-
-      while (s < ds_data (name) + ds_length (name))
-       {
-         if ( !lex_is_idn (*s))
-           *s = '_';
-         s++;
-       }
-
-      assert (var_is_valid_name (ds_cstr (name), false));
-    }
-
-  while (ds_is_empty (name) || NULL != dict_lookup_var (dict, ds_cstr (name)) )
-    {
-      devise_name (dict, name, &x);
-    }
-}
-
-
 /*
    Sets the VAR of case C, to the value corresponding to the xml string XV
  */
@@ -412,6 +350,7 @@ struct var_spec
 struct casereader *
 gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
 {
+  unsigned long int vstart = 0;
   int ret;
   casenumber n_cases = CASENUMBER_MAX;
   int i;
@@ -553,7 +492,7 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
              xmlChar *attr =
                xmlTextReaderGetAttribute (r->xtr, _xml ("ValueType"));
 
-             if ( 60 !=  _xmlchar_to_int (attr))
+             if ( NULL == attr || 60 !=  _xmlchar_to_int (attr))
                var_spec [idx].width = 0;
 
              free (attr);
@@ -569,25 +508,23 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
 
   for (i = 0 ; i < n_var_specs ; ++i )
     {
-      struct string name;
+      char name[VAR_NAME_LEN + 1];
 
-      /* Probably no data exists for this variable, so allocate a default width */
+      /* Probably no data exists for this variable, so allocate a
+        default width */
       if ( var_spec[i].width == -1 )
        var_spec[i].width = MAX_SHORT_STRING;
 
       r->value_cnt += value_cnt_from_width (var_spec[i].width);
 
-      if (var_spec[i].name)
-       ds_init_cstr (&name, var_spec[i].name);
-      else
-       ds_init_empty (&name);
-
-      munge_name (r->dict, &name);
-
-
-      dict_create_var (r->dict, ds_cstr (&name), var_spec[i].width);
+      if  ( ! dict_make_unique_var_name (r->dict, var_spec[i].name,
+                                        &vstart, name))
+       {
+         msg (ME, _("Cannot create variable name from %s"), var_spec[i].name);
+         goto error;
+       }
 
-      ds_destroy (&name);
+      dict_create_var (r->dict, name, var_spec[i].width);
     }
 
   /* Create the first case, and cache it */
@@ -635,6 +572,7 @@ gnumeric_open_reader (struct gnumeric_read_info *gri, struct dictionary **dict)
     }
 
   free (var_spec);
+  dict_destroy (*dict);
 
   gnm_file_casereader_destroy (NULL, r);