Fix bug #21280. Thanks to John Darrington for review.
[pspp-builds.git] / src / data / dictionary.c
index f3c76c6e6d25bdf1c67b2796c2934295ee5be881..5d6633bd7ad61e98f4288a9f88e797486c2efd49 100644 (file)
@@ -116,7 +116,13 @@ dict_create (void)
 }
 
 /* Creates and returns a (deep) copy of an existing
-   dictionary. */
+   dictionary.
+
+   The new dictionary's case indexes are copied from the old
+   dictionary.  If the new dictionary won't be used to access
+   cases produced with the old dictionary, then the new
+   dictionary's case indexes should be compacted with
+   dict_compact_values to save space. */
 struct dictionary *
 dict_clone (const struct dictionary *s)
 {
@@ -135,6 +141,8 @@ dict_clone (const struct dictionary *s)
 
       for (i = 0; i < var_get_short_name_cnt (sv); i++)
         var_set_short_name (dv, i, var_get_short_name (sv, i));
+
+      var_set_vardict (dv, var_get_vardict (sv));
     }
 
   d->next_value_idx = s->next_value_idx;
@@ -176,20 +184,7 @@ dict_clear (struct dictionary *d)
 
   while (d->var_cnt > 0 )
     {
-      struct variable *v = d->var[d->var_cnt - 1];
-      int dict_index = var_get_dict_index (v);
-      int case_index = var_get_case_index (v);
-      int val_cnt = var_get_value_cnt (v);
-
-      var_clear_vardict (v);
-      var_destroy (v);
-
-      d->var_cnt--;
-
-      if (d->callbacks &&  d->callbacks->var_deleted )
-       d->callbacks->var_deleted (d,
-                                  dict_index, case_index, val_cnt,
-                                  d->cb_data);
+      dict_delete_var (d, d->var[d->var_cnt - 1]);
     }
 
   free (d->var);
@@ -386,7 +381,7 @@ dict_lookup_var (const struct dictionary *d, const char *name)
   struct variable *target ;
   struct variable *result ;
 
-  if ( ! var_is_valid_name (name, false))
+  if ( ! var_is_plausible_name (name, false))
     return NULL;
 
   target = var_create (name, 0);
@@ -516,6 +511,7 @@ dict_delete_var (struct dictionary *d, struct variable *v)
   var_clear_vardict (v);
   var_destroy (v);
 
+
   if (d->callbacks &&  d->callbacks->var_deleted )
     d->callbacks->var_deleted (d, dict_index, case_index, val_cnt, d->cb_data);
 }
@@ -959,8 +955,16 @@ dict_set_split_vars (struct dictionary *d,
   assert (cnt == 0 || split != NULL);
 
   d->split_cnt = cnt;
-  d->split = cnt > 0 ? xnrealloc (d->split, cnt, sizeof *d->split) : NULL;
-  memcpy (d->split, split, cnt * sizeof *d->split);
+  if ( cnt > 0 )
+   {
+    d->split = xnrealloc (d->split, cnt, sizeof *d->split) ;
+    memcpy (d->split, split, cnt * sizeof *d->split);
+   }
+  else
+   {
+    free (d->split);
+    d->split = NULL;
+   }
 
   if ( d->callbacks &&  d->callbacks->split_changed )
     d->callbacks->split_changed (d, d->cb_data);