Finish converting struct variable to an opaque type. In this
[pspp] / src / language / lexer / variable-parser.c
index 44be48b9dd6459297ab1b904e20ea8f4f125ba89..d9eae5247c3dc833a5f105861e6179354567797d 100644 (file)
@@ -30,6 +30,7 @@
 #include <data/procedure.h>
 #include <data/variable.h>
 #include <libpspp/alloc.h>
+#include <libpspp/assertion.h>
 #include <libpspp/bit-vector.h>
 #include <libpspp/hash.h>
 #include <libpspp/message.h>
@@ -148,7 +149,7 @@ parse_var_idx_class (struct lexer *lexer, const struct var_set *vs,
   if (!parse_vs_variable_idx (lexer, vs, idx))
     return false;
 
-  *class = dict_class_from_id (var_set_get_var (vs, *idx)->name);
+  *class = dict_class_from_id (var_get_name (var_set_get_var (vs, *idx)));
   return true;
 }
 
@@ -163,24 +164,32 @@ add_variable (struct variable ***v, size_t *nv, size_t *mv,
               const struct var_set *vs, size_t idx)
 {
   struct variable *add = var_set_get_var (vs, idx);
+  const char *add_name = var_get_name (add);
 
-  if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC
+  if ((pv_opts & PV_NUMERIC) && !var_is_numeric (add)
     msg (SW, _("%s is not a numeric variable.  It will not be "
-               "included in the variable list."), add->name);
-  else if ((pv_opts & PV_STRING) && add->type != ALPHA
+               "included in the variable list."), add_name);
+  else if ((pv_opts & PV_STRING) && !var_is_alpha (add)
     msg (SE, _("%s is not a string variable.  It will not be "
-               "included in the variable list."), add->name);
+               "included in the variable list."), add_name);
   else if ((pv_opts & PV_NO_SCRATCH)
-           && dict_class_from_id (add->name) == DC_SCRATCH)
+           && dict_class_from_id (add_name) == DC_SCRATCH)
     msg (SE, _("Scratch variables (such as %s) are not allowed "
-               "here."), add->name);
-  else if ((pv_opts & PV_SAME_TYPE) && *nv && add->type != (*v)[0]->type) 
+               "here."), add_name);
+  else if ((pv_opts & (PV_SAME_TYPE | PV_SAME_WIDTH)) && *nv
+           && var_get_type (add) != var_get_type ((*v)[0])) 
     msg (SE, _("%s and %s are not the same type.  All variables in "
                "this variable list must be of the same type.  %s "
-               "will be omitted from list."),
-         (*v)[0]->name, add->name, add->name);
+               "will be omitted from the list."),
+         var_get_name ((*v)[0]), add_name, add_name);
+  else if ((pv_opts & PV_SAME_WIDTH) && *nv
+           && var_get_width (add) != var_get_width ((*v)[0]))
+    msg (SE, _("%s and %s are string variables with different widths.  "
+               "All variables in this variable list must have the "
+               "same width.  %s will be omttied from the list."),
+         var_get_name ((*v)[0]), add_name, add_name);
   else if ((pv_opts & PV_NO_DUPLICATE) && included[idx]) 
-    msg (SE, _("Variable %s appears twice in variable list."), add->name);
+    msg (SE, _("Variable %s appears twice in variable list."), add_name);
   else if ((pv_opts & PV_DUPLICATE) || !included[idx])
     {
       if (*nv >= *mv)
@@ -208,7 +217,7 @@ add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
   size_t i;
   
   for (i = first_idx; i <= last_idx; i++)
-    if (dict_class_from_id (var_set_get_var (vs, i)->name) == class)
+    if (dict_class_from_id (var_get_name (var_set_get_var (vs, i))) == class)
       add_variable (v, nv, mv, included, pv_opts, vs, i);
 }
 
@@ -227,11 +236,12 @@ parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
   assert (v != NULL);
   assert (nv != NULL);
 
-  /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
-     specified. */
-  assert ((((pv_opts & PV_NUMERIC) != 0)
-           + ((pv_opts & PV_STRING) != 0)
-           + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
+  /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE,
+     PV_SAME_WIDTH may be specified. */
+  assert (((pv_opts & PV_NUMERIC) != 0)
+          + ((pv_opts & PV_STRING) != 0)
+          + ((pv_opts & PV_SAME_TYPE) != 0)
+          + ((pv_opts & PV_SAME_WIDTH) != 0) <= 1);
 
   /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
   assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
@@ -250,8 +260,13 @@ parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
       size_t i;
       
       included = xcalloc (var_set_get_cnt (vs), sizeof *included);
-      for (i = 0; i < *nv; i++)
-        included[(*v)[i]->index] = 1;
+      for (i = 0; i < *nv; i++) 
+        {
+          size_t index;
+          if (!var_set_lookup_var_idx (vs, var_get_name ((*v)[i]), &index))
+            NOT_REACHED ();
+          included[index] = 1; 
+        }
     }
   else
     included = NULL;
@@ -285,10 +300,11 @@ parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
 
               if (last_idx < first_idx)
                 {
+                  const char *first_name = var_get_name (first_var);
+                  const char *last_name = var_get_name (last_var);
                   msg (SE, _("%s TO %s is not valid syntax since %s "
                              "precedes %s in the dictionary."),
-                       first_var->name, last_var->name,
-                       first_var->name, last_var->name);
+                       first_name, last_name, first_name, last_name);
                   goto fail;
                 }
 
@@ -299,8 +315,9 @@ parse_var_set_vars (struct lexer *lexer, const struct var_set *vs,
                              "the same variable dictionaries, of either "
                              "ordinary, scratch, or system variables.  "
                              "%s is a %s variable, whereas %s is %s."),
-                       first_var->name, dict_class_to_name (class),
-                       last_var->name, dict_class_to_name (last_class));
+                       var_get_name (first_var), dict_class_to_name (class),
+                       var_get_name (last_var),
+                       dict_class_to_name (last_class));
                   goto fail;
                 }
 
@@ -552,7 +569,7 @@ parse_mixed_vars (struct lexer *lexer, const struct dictionary *dict,
            goto fail;
          *names = xnrealloc (*names, *nnames + nv, sizeof **names);
          for (i = 0; i < nv; i++)
-           (*names)[*nnames + i] = xstrdup (v[i]->name);
+           (*names)[*nnames + i] = xstrdup (var_get_name (v[i]));
          free (v);
          *nnames += nv;
        }
@@ -683,7 +700,7 @@ dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
   struct variable *v = dict_lookup_var (d, name);
   if (v != NULL) 
     {
-      *idx = v->index;
+      *idx = var_get_dict_index (v);
       return true;
     }
   else
@@ -744,11 +761,12 @@ array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
                               size_t *idx) 
 {
   struct array_var_set *avs = vs->aux;
-  struct variable v, *vp, *const *vpp;
+  struct variable *v, *const *vpp;
 
-  strcpy (v.name, name);
-  vp = &v;
-  vpp = hsh_find (avs->name_tab, &vp);
+  v = var_create (name, 0);
+  vpp = hsh_find (avs->name_tab, &v);
+  var_destroy (v);
+  
   if (vpp != NULL) 
     {
       *idx = vpp - avs->var;
@@ -787,8 +805,8 @@ var_set_create_from_array (struct variable *const *var, size_t var_cnt)
   avs->var = var;
   avs->var_cnt = var_cnt;
   avs->name_tab = hsh_create (2 * var_cnt,
-                              compare_var_ptr_names, hash_var_ptr_name, NULL,
-                              NULL);
+                              compare_var_ptrs_by_name, hash_var_ptr_by_name,
+                              NULL, NULL);
   for (i = 0; i < var_cnt; i++)
     if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL) 
       {