Plugged some more memory leaks.
[pspp-builds.git] / src / data / dictionary.c
index 7964ad55e94b83c869c2ea531f10efccf8116051..46abaa7316cea0250c9635571f2e429bf0a362e8 100644 (file)
@@ -63,6 +63,23 @@ struct dictionary
     void *cb_data ;                  /* Data passed to callbacks */
   };
 
+/* Print a representation of dictionary D to stdout, for
+   debugging purposes. */
+void
+dict_dump (const struct dictionary *d)
+{
+  int i;
+  for (i = 0 ; i < d->var_cnt ; ++i )
+    {
+      const struct variable *v =
+       d->var[i];
+      printf ("Name: %s;\tdict_idx: %d; case_idx: %d\n",
+             var_get_name (v),
+             var_get_dict_index (v),
+             var_get_case_index (v));
+
+    }
+}
 
 /* Associate CALLBACKS with DICT.  Callbacks will be invoked whenever
    the dictionary or any of the variables it contains are modified.
@@ -159,20 +176,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);
@@ -369,7 +373,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);
@@ -499,6 +503,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);
 }
@@ -896,105 +901,6 @@ dict_count_values (const struct dictionary *d, unsigned int exclude_classes)
   return cnt;
 }
 \f
-/* How to copy a contiguous range of values between cases. */
-struct copy_map
-  {
-    size_t src_idx;             /* Starting value index in source case. */
-    size_t dst_idx;             /* Starting value index in target case. */
-    size_t cnt;                 /* Number of values. */
-  };
-
-/* How to compact a case. */
-struct dict_compactor
-  {
-    struct copy_map *maps;      /* Array of mappings. */
-    size_t map_cnt;             /* Number of mappings. */
-  };
-
-/* Creates and returns a dict_compactor that can be used to
-   compact cases for dictionary D.
-
-   Compacting a case eliminates "holes" between values and after
-   the last value.  (Holes are created by deleting variables.)
-
-   All variables are compacted if EXCLUDE_CLASSES is 0, or it may
-   contain one or more of (1u << DC_ORDINARY), (1u << DC_SYSTEM),
-   or (1u << DC_SCRATCH) to cause the corresponding type of
-   variable to be deleted during compaction. */
-struct dict_compactor *
-dict_make_compactor (const struct dictionary *d, unsigned int exclude_classes)
-{
-  struct dict_compactor *compactor;
-  struct copy_map *map;
-  size_t map_allocated;
-  size_t value_idx;
-  size_t i;
-
-  assert ((exclude_classes & ~((1u << DC_ORDINARY)
-                               | (1u << DC_SYSTEM)
-                               | (1u << DC_SCRATCH))) == 0);
-
-  compactor = xmalloc (sizeof *compactor);
-  compactor->maps = NULL;
-  compactor->map_cnt = 0;
-  map_allocated = 0;
-
-  value_idx = 0;
-  map = NULL;
-  for (i = 0; i < d->var_cnt; i++)
-    {
-      struct variable *v = d->var[i];
-      enum dict_class class = dict_class_from_id (var_get_name (v));
-      if (exclude_classes & (1u << class))
-        continue;
-
-      if (map != NULL && map->src_idx + map->cnt == var_get_case_index (v))
-        map->cnt += var_get_value_cnt (v);
-      else
-        {
-          if (compactor->map_cnt == map_allocated)
-            compactor->maps = x2nrealloc (compactor->maps, &map_allocated,
-                                          sizeof *compactor->maps);
-          map = &compactor->maps[compactor->map_cnt++];
-          map->src_idx = var_get_case_index (v);
-          map->dst_idx = value_idx;
-          map->cnt = var_get_value_cnt (v);
-        }
-      value_idx += var_get_value_cnt (v);
-    }
-
-  return compactor;
-}
-
-/* Compacts SRC by copying it to DST according to the scheme in
-   COMPACTOR.
-
-   Compacting a case eliminates "holes" between values and after
-   the last value.  (Holes are created by deleting variables.) */
-void
-dict_compactor_compact (const struct dict_compactor *compactor,
-                        struct ccase *dst, const struct ccase *src)
-{
-  size_t i;
-
-  for (i = 0; i < compactor->map_cnt; i++)
-    {
-      const struct copy_map *map = &compactor->maps[i];
-      case_copy (dst, map->dst_idx, src, map->src_idx, map->cnt);
-    }
-}
-
-/* Destroys COMPACTOR. */
-void
-dict_compactor_destroy (struct dict_compactor *compactor)
-{
-  if (compactor != NULL)
-    {
-      free (compactor->maps);
-      free (compactor);
-    }
-}
-
 /* Returns the SPLIT FILE vars (see cmd_split_file()).  Call
    dict_get_split_cnt() to determine how many SPLIT FILE vars
    there are.  Returns a null pointer if and only if there are no
@@ -1041,8 +947,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);