Fixed crash which occured when deleting dictionaries which had variable labels
[pspp] / src / data / dictionary.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "dictionary.h"
20
21 #include <stdlib.h>
22 #include <ctype.h>
23
24 #include <data/attributes.h>
25 #include <data/case.h>
26 #include <data/category.h>
27 #include <data/identifier.h>
28 #include <data/settings.h>
29 #include <data/value-labels.h>
30 #include <data/vardict.h>
31 #include <data/variable.h>
32 #include <data/vector.h>
33 #include <libpspp/array.h>
34 #include <libpspp/assertion.h>
35 #include <libpspp/compiler.h>
36 #include <libpspp/hash.h>
37 #include <libpspp/message.h>
38 #include <libpspp/misc.h>
39 #include <libpspp/pool.h>
40 #include <libpspp/str.h>
41
42 #include "intprops.h"
43 #include "minmax.h"
44 #include "xalloc.h"
45
46 #include "gettext.h"
47 #define _(msgid) gettext (msgid)
48
49 /* A dictionary. */
50 struct dictionary
51   {
52     struct variable **var;      /* Variables. */
53     size_t var_cnt, var_cap;    /* Number of variables, capacity. */
54     struct hsh_table *name_tab; /* Variable index by name. */
55     int next_value_idx;         /* Index of next `union value' to allocate. */
56     const struct variable **split;    /* SPLIT FILE vars. */
57     size_t split_cnt;           /* SPLIT FILE count. */
58     struct variable *weight;    /* WEIGHT variable. */
59     struct variable *filter;    /* FILTER variable. */
60     casenumber case_limit;      /* Current case limit (N command). */
61     char *label;                /* File label. */
62     struct string documents;    /* Documents, as a string. */
63     struct vector **vector;     /* Vectors of variables. */
64     size_t vector_cnt;          /* Number of vectors. */
65     struct attrset attributes;  /* Custom attributes. */
66     const struct dict_callbacks *callbacks; /* Callbacks on dictionary
67                                                modification */
68     void *cb_data ;                  /* Data passed to callbacks */
69
70     void (*changed) (struct dictionary *, void *); /* Generic change callback */
71     void *changed_data;
72   };
73
74 void
75 dict_set_change_callback (struct dictionary *d,
76                           void (*changed) (struct dictionary *, void*),
77                           void *data)
78 {
79   d->changed = changed;
80   d->changed_data = data;
81 }
82
83
84 /* Print a representation of dictionary D to stdout, for
85    debugging purposes. */
86 void
87 dict_dump (const struct dictionary *d)
88 {
89   int i;
90   for (i = 0 ; i < d->var_cnt ; ++i )
91     {
92       const struct variable *v =
93         d->var[i];
94       printf ("Name: %s;\tdict_idx: %d; case_idx: %d\n",
95               var_get_name (v),
96               var_get_dict_index (v),
97               var_get_case_index (v));
98
99     }
100 }
101
102 /* Associate CALLBACKS with DICT.  Callbacks will be invoked whenever
103    the dictionary or any of the variables it contains are modified.
104    Each callback will get passed CALLBACK_DATA.
105    Any callback may be NULL, in which case it'll be ignored.
106 */
107 void
108 dict_set_callbacks (struct dictionary *dict,
109                     const struct dict_callbacks *callbacks,
110                     void *callback_data)
111 {
112   dict->callbacks = callbacks;
113   dict->cb_data = callback_data;
114 }
115
116 /* Shallow copy the callbacks from SRC to DEST */
117 void
118 dict_copy_callbacks (struct dictionary *dest,
119                      const struct dictionary *src)
120 {
121   dest->callbacks = src->callbacks;
122   dest->cb_data = src->cb_data;
123 }
124
125 /* Creates and returns a new dictionary. */
126 struct dictionary *
127 dict_create (void)
128 {
129   struct dictionary *d = xzalloc (sizeof *d);
130
131   d->name_tab = hsh_create (8, compare_vars_by_name, hash_var_by_name,
132                             NULL, NULL);
133   attrset_init (&d->attributes);
134   return d;
135 }
136
137 /* Creates and returns a (deep) copy of an existing
138    dictionary.
139
140    The new dictionary's case indexes are copied from the old
141    dictionary.  If the new dictionary won't be used to access
142    cases produced with the old dictionary, then the new
143    dictionary's case indexes should be compacted with
144    dict_compact_values to save space. */
145 struct dictionary *
146 dict_clone (const struct dictionary *s)
147 {
148   struct dictionary *d;
149   size_t i;
150
151   assert (s != NULL);
152
153   d = dict_create ();
154
155   for (i = 0; i < s->var_cnt; i++)
156     {
157       const struct vardict_info *svdi;
158       struct vardict_info dvdi;
159       struct variable *sv = s->var[i];
160       struct variable *dv = dict_clone_var_assert (d, sv, var_get_name (sv));
161       size_t i;
162
163       for (i = 0; i < var_get_short_name_cnt (sv); i++)
164         var_set_short_name (dv, i, var_get_short_name (sv, i));
165
166       svdi = var_get_vardict (sv);
167       dvdi = *svdi;
168       dvdi.dict = d;
169       var_set_vardict (dv, &dvdi);
170     }
171
172   d->next_value_idx = s->next_value_idx;
173
174   d->split_cnt = s->split_cnt;
175   if (d->split_cnt > 0)
176     {
177       d->split = xnmalloc (d->split_cnt, sizeof *d->split);
178       for (i = 0; i < d->split_cnt; i++)
179         d->split[i] = dict_lookup_var_assert (d, var_get_name (s->split[i]));
180     }
181
182   if (s->weight != NULL)
183     dict_set_weight (d, dict_lookup_var_assert (d, var_get_name (s->weight)));
184
185   if (s->filter != NULL)
186     dict_set_filter (d, dict_lookup_var_assert (d, var_get_name (s->filter)));
187
188   d->case_limit = s->case_limit;
189   dict_set_label (d, dict_get_label (s));
190   dict_set_documents (d, dict_get_documents (s));
191
192   d->vector_cnt = s->vector_cnt;
193   d->vector = xnmalloc (d->vector_cnt, sizeof *d->vector);
194   for (i = 0; i < s->vector_cnt; i++)
195     d->vector[i] = vector_clone (s->vector[i], s, d);
196
197   dict_set_attributes (d, dict_get_attributes (s));
198
199   return d;
200 }
201
202 /* Clears the contents from a dictionary without destroying the
203    dictionary itself. */
204 void
205 dict_clear (struct dictionary *d)
206 {
207   /* FIXME?  Should we really clear case_limit, label, documents?
208      Others are necessarily cleared by deleting all the variables.*/
209   assert (d != NULL);
210
211   while (d->var_cnt > 0 )
212     {
213       dict_delete_var (d, d->var[d->var_cnt - 1]);
214     }
215
216   free (d->var);
217   d->var = NULL;
218   d->var_cnt = d->var_cap = 0;
219   hsh_clear (d->name_tab);
220   d->next_value_idx = 0;
221   dict_set_split_vars (d, NULL, 0);
222   dict_set_weight (d, NULL);
223   dict_set_filter (d, NULL);
224   d->case_limit = 0;
225   free (d->label);
226   d->label = NULL;
227   ds_destroy (&d->documents);
228   dict_clear_vectors (d);
229   attrset_clear (&d->attributes);
230 }
231
232 /* Destroys the aux data for every variable in D, by calling
233    var_clear_aux() for each variable. */
234 void
235 dict_clear_aux (struct dictionary *d)
236 {
237   int i;
238
239   assert (d != NULL);
240
241   for (i = 0; i < d->var_cnt; i++)
242     var_clear_aux (d->var[i]);
243 }
244
245 /* Clears a dictionary and destroys it. */
246 void
247 dict_destroy (struct dictionary *d)
248 {
249   if (d != NULL)
250     {
251       /* In general, we don't want callbacks occuring, if the dictionary
252          is being destroyed */
253       d->callbacks  = NULL ;
254
255       dict_clear (d);
256       hsh_destroy (d->name_tab);
257       attrset_destroy (&d->attributes);
258       free (d);
259     }
260 }
261
262 /* Returns the number of variables in D. */
263 size_t
264 dict_get_var_cnt (const struct dictionary *d)
265 {
266   assert (d != NULL);
267
268   return d->var_cnt;
269 }
270
271 /* Returns the variable in D with dictionary index IDX, which
272    must be between 0 and the count returned by
273    dict_get_var_cnt(), exclusive. */
274 struct variable *
275 dict_get_var (const struct dictionary *d, size_t idx)
276 {
277   assert (d != NULL);
278   assert (idx < d->var_cnt);
279
280   return d->var[idx];
281 }
282
283 /* Sets *VARS to an array of pointers to variables in D and *CNT
284    to the number of variables in *D.  All variables are returned
285    except for those, if any, in the classes indicated by EXCLUDE.
286    (There is no point in putting DC_SYSTEM in EXCLUDE as
287    dictionaries never include system variables.) */
288 void
289 dict_get_vars (const struct dictionary *d, const struct variable ***vars,
290                size_t *cnt, enum dict_class exclude)
291 {
292   dict_get_vars_mutable (d, (struct variable ***) vars, cnt, exclude);
293 }
294
295 /* Sets *VARS to an array of pointers to variables in D and *CNT
296    to the number of variables in *D.  All variables are returned
297    except for those, if any, in the classes indicated by EXCLUDE.
298    (There is no point in putting DC_SYSTEM in EXCLUDE as
299    dictionaries never include system variables.) */
300 void
301 dict_get_vars_mutable (const struct dictionary *d, struct variable ***vars,
302                        size_t *cnt, enum dict_class exclude)
303 {
304   size_t count;
305   size_t i;
306
307   assert (d != NULL);
308   assert (vars != NULL);
309   assert (cnt != NULL);
310   assert (exclude == (exclude & DC_ALL));
311
312   count = 0;
313   for (i = 0; i < d->var_cnt; i++)
314     {
315       enum dict_class class = var_get_dict_class (d->var[i]);
316       if (!(class & exclude))
317         count++;
318     }
319
320   *vars = xnmalloc (count, sizeof **vars);
321   *cnt = 0;
322   for (i = 0; i < d->var_cnt; i++)
323     {
324       enum dict_class class = var_get_dict_class (d->var[i]);
325       if (!(class & exclude))
326         (*vars)[(*cnt)++] = d->var[i];
327     }
328   assert (*cnt == count);
329 }
330
331 static struct variable *
332 add_var (struct dictionary *d, struct variable *v)
333 {
334   /* Add dictionary info to variable. */
335   struct vardict_info vdi;
336   vdi.case_index = d->next_value_idx;
337   vdi.dict_index = d->var_cnt;
338   vdi.dict = d;
339   var_set_vardict (v, &vdi);
340
341   /* Update dictionary. */
342   if (d->var_cnt >= d->var_cap)
343     {
344       d->var_cap = 8 + 2 * d->var_cap;
345       d->var = xnrealloc (d->var, d->var_cap, sizeof *d->var);
346     }
347   d->var[d->var_cnt++] = v;
348   hsh_force_insert (d->name_tab, v);
349
350   if ( d->changed ) d->changed (d, d->changed_data);
351   if ( d->callbacks &&  d->callbacks->var_added )
352     d->callbacks->var_added (d, var_get_dict_index (v), d->cb_data);
353
354   d->next_value_idx += var_get_value_cnt (v);
355
356   return v;
357 }
358
359 /* Creates and returns a new variable in D with the given NAME
360    and WIDTH.  Returns a null pointer if the given NAME would
361    duplicate that of an existing variable in the dictionary. */
362 struct variable *
363 dict_create_var (struct dictionary *d, const char *name, int width)
364 {
365   return (dict_lookup_var (d, name) == NULL
366           ? dict_create_var_assert (d, name, width)
367           : NULL);
368 }
369
370 /* Creates and returns a new variable in D with the given NAME
371    and WIDTH.  Assert-fails if the given NAME would duplicate
372    that of an existing variable in the dictionary. */
373 struct variable *
374 dict_create_var_assert (struct dictionary *d, const char *name, int width)
375 {
376   assert (dict_lookup_var (d, name) == NULL);
377   return add_var (d, var_create (name, width));
378 }
379
380 /* Creates and returns a new variable in D with name NAME, as a
381    copy of existing variable OLD_VAR, which need not be in D or
382    in any dictionary.  Returns a null pointer if the given NAME
383    would duplicate that of an existing variable in the
384    dictionary. */
385 struct variable *
386 dict_clone_var (struct dictionary *d, const struct variable *old_var,
387                 const char *name)
388 {
389   return (dict_lookup_var (d, name) == NULL
390           ? dict_clone_var_assert (d, old_var, name)
391           : NULL);
392 }
393
394 /* Creates and returns a new variable in D with name NAME, as a
395    copy of existing variable OLD_VAR, which need not be in D or
396    in any dictionary.  Assert-fails if the given NAME would
397    duplicate that of an existing variable in the dictionary. */
398 struct variable *
399 dict_clone_var_assert (struct dictionary *d, const struct variable *old_var,
400                        const char *name)
401 {
402   struct variable *new_var = var_clone (old_var);
403   assert (dict_lookup_var (d, name) == NULL);
404   var_set_name (new_var, name);
405   return add_var (d, new_var);
406 }
407
408 /* Returns the variable named NAME in D, or a null pointer if no
409    variable has that name. */
410 struct variable *
411 dict_lookup_var (const struct dictionary *d, const char *name)
412 {
413   struct variable *target ;
414   struct variable *result ;
415
416   if ( ! var_is_plausible_name (name, false))
417     return NULL;
418
419   target = var_create (name, 0);
420   result = hsh_find (d->name_tab, target);
421   var_destroy (target);
422
423   if ( result && var_has_vardict (result)) 
424   {
425       const struct vardict_info *vdi = var_get_vardict (result);
426       assert (vdi->dict == d);
427   }
428
429   return result;
430 }
431
432 /* Returns the variable named NAME in D.  Assert-fails if no
433    variable has that name. */
434 struct variable *
435 dict_lookup_var_assert (const struct dictionary *d, const char *name)
436 {
437   struct variable *v = dict_lookup_var (d, name);
438   assert (v != NULL);
439   return v;
440 }
441
442 /* Returns true if variable V is in dictionary D,
443    false otherwise. */
444 bool
445 dict_contains_var (const struct dictionary *d, const struct variable *v)
446 {
447   if (var_has_vardict (v))
448     {
449       const struct vardict_info *vdi = var_get_vardict (v);
450       return (vdi->dict_index >= 0
451               && vdi->dict_index < d->var_cnt
452               && d->var[vdi->dict_index] == v);
453     }
454   else
455     return false;
456 }
457
458 /* Compares two double pointers to variables, which should point
459    to elements of a struct dictionary's `var' member array. */
460 static int
461 compare_var_ptrs (const void *a_, const void *b_, const void *aux UNUSED)
462 {
463   struct variable *const *a = a_;
464   struct variable *const *b = b_;
465
466   return *a < *b ? -1 : *a > *b;
467 }
468
469 /* Sets the dict_index in V's vardict to DICT_INDEX. */
470 static void
471 set_var_dict_index (struct variable *v, int dict_index)
472 {
473   struct vardict_info vdi = *var_get_vardict (v);
474   struct dictionary *d = vdi.dict;
475   vdi.dict_index = dict_index;
476   var_set_vardict (v, &vdi);
477
478   if ( d->changed ) d->changed (d, d->changed_data);
479   if ( d->callbacks &&  d->callbacks->var_changed )
480     d->callbacks->var_changed (d, dict_index, d->cb_data);
481 }
482
483 /* Sets the case_index in V's vardict to CASE_INDEX. */
484 static void
485 set_var_case_index (struct variable *v, int case_index)
486 {
487   struct vardict_info vdi = *var_get_vardict (v);
488   vdi.case_index = case_index;
489   var_set_vardict (v, &vdi);
490 }
491
492 /* Re-sets the dict_index in the dictionary variables with
493    indexes from FROM to TO (exclusive). */
494 static void
495 reindex_vars (struct dictionary *d, size_t from, size_t to)
496 {
497   size_t i;
498
499   for (i = from; i < to; i++)
500     set_var_dict_index (d->var[i], i);
501 }
502
503 /* Deletes variable V from dictionary D and frees V.
504
505    This is a very bad idea if there might be any pointers to V
506    from outside D.  In general, no variable in the active file's
507    dictionary should be deleted when any transformations are
508    active on the dictionary's dataset, because those
509    transformations might reference the deleted variable.  The
510    safest time to delete a variable is just after a procedure has
511    been executed, as done by DELETE VARIABLES.
512
513    Pointers to V within D are not a problem, because
514    dict_delete_var() knows to remove V from split variables,
515    weights, filters, etc. */
516 void
517 dict_delete_var (struct dictionary *d, struct variable *v)
518 {
519   int dict_index = var_get_dict_index (v);
520   const int case_index = var_get_case_index (v);
521   const int val_cnt = var_get_value_cnt (v);
522
523   assert (dict_contains_var (d, v));
524
525   /* Delete aux data. */
526   var_clear_aux (v);
527
528   dict_unset_split_var (d, v);
529
530   if (d->weight == v)
531     dict_set_weight (d, NULL);
532
533   if (d->filter == v)
534     dict_set_filter (d, NULL);
535
536   dict_clear_vectors (d);
537
538   /* Remove V from var array. */
539   remove_element (d->var, d->var_cnt, sizeof *d->var, dict_index);
540   d->var_cnt--;
541
542   /* Update dict_index for each affected variable. */
543   reindex_vars (d, dict_index, d->var_cnt);
544
545   /* Update name hash. */
546   hsh_force_delete (d->name_tab, v);
547
548
549   /* Free memory. */
550   var_clear_vardict (v);
551   var_destroy (v);
552
553   if ( d->changed ) d->changed (d, d->changed_data);
554   if (d->callbacks &&  d->callbacks->var_deleted )
555     d->callbacks->var_deleted (d, dict_index, case_index, val_cnt, d->cb_data);
556 }
557
558 /* Deletes the COUNT variables listed in VARS from D.  This is
559    unsafe; see the comment on dict_delete_var() for details. */
560 void
561 dict_delete_vars (struct dictionary *d,
562                   struct variable *const *vars, size_t count)
563 {
564   /* FIXME: this can be done in O(count) time, but this algorithm
565      is O(count**2). */
566   assert (d != NULL);
567   assert (count == 0 || vars != NULL);
568
569   while (count-- > 0)
570     dict_delete_var (d, *vars++);
571 }
572
573 /* Deletes the COUNT variables in D starting at index IDX.  This
574    is unsafe; see the comment on dict_delete_var() for
575    details. */
576 void
577 dict_delete_consecutive_vars (struct dictionary *d, size_t idx, size_t count)
578 {
579   /* FIXME: this can be done in O(count) time, but this algorithm
580      is O(count**2). */
581   assert (idx + count <= d->var_cnt);
582
583   while (count-- > 0)
584     dict_delete_var (d, d->var[idx]);
585 }
586
587 /* Deletes scratch variables from dictionary D. */
588 void
589 dict_delete_scratch_vars (struct dictionary *d)
590 {
591   int i;
592
593   /* FIXME: this can be done in O(count) time, but this algorithm
594      is O(count**2). */
595   assert (d != NULL);
596
597   for (i = 0; i < d->var_cnt; )
598     if (var_get_dict_class (d->var[i]) == DC_SCRATCH)
599       dict_delete_var (d, d->var[i]);
600     else
601       i++;
602 }
603
604 /* Moves V to 0-based position IDX in D.  Other variables in D,
605    if any, retain their relative positions.  Runs in time linear
606    in the distance moved. */
607 void
608 dict_reorder_var (struct dictionary *d, struct variable *v, size_t new_index)
609 {
610   size_t old_index = var_get_dict_index (v);
611
612   assert (new_index < d->var_cnt);
613   move_element (d->var, d->var_cnt, sizeof *d->var, old_index, new_index);
614   reindex_vars (d, MIN (old_index, new_index), MAX (old_index, new_index) + 1);
615 }
616
617 /* Reorders the variables in D, placing the COUNT variables
618    listed in ORDER in that order at the beginning of D.  The
619    other variables in D, if any, retain their relative
620    positions. */
621 void
622 dict_reorder_vars (struct dictionary *d,
623                    struct variable *const *order, size_t count)
624 {
625   struct variable **new_var;
626   size_t i;
627
628   assert (d != NULL);
629   assert (count == 0 || order != NULL);
630   assert (count <= d->var_cnt);
631
632   new_var = xnmalloc (d->var_cnt, sizeof *new_var);
633   memcpy (new_var, order, count * sizeof *new_var);
634   for (i = 0; i < count; i++)
635     {
636       size_t index = var_get_dict_index (order[i]);
637       assert (d->var[index] == order[i]);
638       d->var[index] = NULL;
639       set_var_dict_index (order[i], i);
640     }
641   for (i = 0; i < d->var_cnt; i++)
642     if (d->var[i] != NULL)
643       {
644         assert (count < d->var_cnt);
645         new_var[count] = d->var[i];
646         set_var_dict_index (new_var[count], count);
647         count++;
648       }
649   free (d->var);
650   d->var = new_var;
651 }
652
653 /* Changes the name of variable V in dictionary D to NEW_NAME. */
654 static void
655 rename_var (struct dictionary *d, struct variable *v, const char *new_name)
656 {
657   struct vardict_info vdi;
658
659   assert (dict_contains_var (d, v));
660
661   vdi = *var_get_vardict (v);
662   var_clear_vardict (v);
663   var_set_name (v, new_name);
664   var_set_vardict (v, &vdi);
665 }
666
667 /* Changes the name of V in D to name NEW_NAME.  Assert-fails if
668    a variable named NEW_NAME is already in D, except that
669    NEW_NAME may be the same as V's existing name. */
670 void
671 dict_rename_var (struct dictionary *d, struct variable *v,
672                  const char *new_name)
673 {
674   assert (!strcasecmp (var_get_name (v), new_name)
675           || dict_lookup_var (d, new_name) == NULL);
676
677   hsh_force_delete (d->name_tab, v);
678   rename_var (d, v, new_name);
679   hsh_force_insert (d->name_tab, v);
680
681   if (settings_get_algorithm () == ENHANCED)
682     var_clear_short_names (v);
683
684   if ( d->changed ) d->changed (d, d->changed_data);
685   if ( d->callbacks &&  d->callbacks->var_changed )
686     d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
687 }
688
689 /* Renames COUNT variables specified in VARS to the names given
690    in NEW_NAMES within dictionary D.  If the renaming would
691    result in a duplicate variable name, returns false and stores a
692    name that would be duplicated into *ERR_NAME (if ERR_NAME is
693    non-null).  Otherwise, the renaming is successful, and true
694    is returned. */
695 bool
696 dict_rename_vars (struct dictionary *d,
697                   struct variable **vars, char **new_names, size_t count,
698                   char **err_name)
699 {
700   struct pool *pool;
701   char **old_names;
702   size_t i;
703
704   assert (count == 0 || vars != NULL);
705   assert (count == 0 || new_names != NULL);
706
707   /* Save the names of the variables to be renamed. */
708   pool = pool_create ();
709   old_names = pool_nalloc (pool, count, sizeof *old_names);
710   for (i = 0; i < count; i++)
711     old_names[i] = pool_strdup (pool, var_get_name (vars[i]));
712
713   /* Remove the variables to be renamed from the name hash,
714      and rename them. */
715   for (i = 0; i < count; i++)
716     {
717       hsh_force_delete (d->name_tab, vars[i]);
718       rename_var (d, vars[i], new_names[i]);
719     }
720
721   /* Add the renamed variables back into the name hash,
722      checking for conflicts. */
723   for (i = 0; i < count; i++)
724     if (hsh_insert (d->name_tab, vars[i]) != NULL)
725       {
726         /* There is a name conflict.
727            Back out all the name changes that have already
728            taken place, and indicate failure. */
729         size_t fail_idx = i;
730         if (err_name != NULL)
731           *err_name = new_names[i];
732
733         for (i = 0; i < fail_idx; i++)
734           hsh_force_delete (d->name_tab, vars[i]);
735
736         for (i = 0; i < count; i++)
737           {
738             rename_var (d, vars[i], old_names[i]);
739             hsh_force_insert (d->name_tab, vars[i]);
740           }
741
742         pool_destroy (pool);
743         return false;
744       }
745
746   /* Clear short names. */
747   if (settings_get_algorithm () == ENHANCED)
748     for (i = 0; i < count; i++)
749       var_clear_short_names (vars[i]);
750
751   pool_destroy (pool);
752   return true;
753 }
754
755 /* Returns true if a variable named NAME may be inserted in DICT;
756    that is, if there is not already a variable with that name in
757    DICT and if NAME is not a reserved word.  (The caller's checks
758    have already verified that NAME is otherwise acceptable as a
759    variable name.) */
760 static bool
761 var_name_is_insertable (const struct dictionary *dict, const char *name)
762 {
763   return (dict_lookup_var (dict, name) == NULL
764           && lex_id_to_token (ss_cstr (name)) == T_ID);
765 }
766
767 static bool
768 make_hinted_name (const struct dictionary *dict, const char *hint,
769                   char name[VAR_NAME_LEN + 1])
770 {
771   bool dropped = false;
772   char *cp;
773
774   for (cp = name; *hint && cp < name + VAR_NAME_LEN; hint++)
775     {
776       if (cp == name
777           ? lex_is_id1 (*hint) && *hint != '$'
778           : lex_is_idn (*hint))
779         {
780           if (dropped)
781             {
782               *cp++ = '_';
783               dropped = false;
784             }
785           if (cp < name + VAR_NAME_LEN)
786             *cp++ = *hint;
787         }
788       else if (cp > name)
789         dropped = true;
790     }
791   *cp = '\0';
792
793   if (name[0] != '\0')
794     {
795       size_t len = strlen (name);
796       unsigned long int i;
797
798       if (var_name_is_insertable (dict, name))
799         return true;
800
801       for (i = 0; i < ULONG_MAX; i++)
802         {
803           char suffix[INT_BUFSIZE_BOUND (i) + 1];
804           int ofs;
805
806           suffix[0] = '_';
807           if (!str_format_26adic (i + 1, &suffix[1], sizeof suffix - 1))
808             NOT_REACHED ();
809
810           ofs = MIN (VAR_NAME_LEN - strlen (suffix), len);
811           strcpy (&name[ofs], suffix);
812
813           if (var_name_is_insertable (dict, name))
814             return true;
815         }
816     }
817
818   return false;
819 }
820
821 static bool
822 make_numeric_name (const struct dictionary *dict, unsigned long int *num_start,
823                    char name[VAR_NAME_LEN + 1])
824 {
825   unsigned long int number;
826
827   for (number = num_start != NULL ? MAX (*num_start, 1) : 1;
828        number < ULONG_MAX;
829        number++)
830     {
831       sprintf (name, "VAR%03lu", number);
832       if (dict_lookup_var (dict, name) == NULL)
833         {
834           if (num_start != NULL)
835             *num_start = number + 1;
836           return true;
837         }
838     }
839
840   if (num_start != NULL)
841     *num_start = ULONG_MAX;
842   return false;
843 }
844
845
846 /* Attempts to devise a variable name unique within DICT.
847    Returns true if successful, in which case the new variable
848    name is stored into NAME.  Returns false if all names that can
849    be generated have already been taken.  (Returning false is
850    quite unlikely: at least ULONG_MAX unique names can be
851    generated.)
852
853    HINT, if it is non-null, is used as a suggestion that will be
854    modified for suitability as a variable name and for
855    uniqueness.
856
857    If HINT is null or entirely unsuitable, a name in the form
858    "VAR%03d" will be generated, where the smallest unused integer
859    value is used.  If NUM_START is non-null, then its value is
860    used as the minimum numeric value to check, and it is updated
861    to the next value to be checked.
862    */
863 bool
864 dict_make_unique_var_name (const struct dictionary *dict, const char *hint,
865                            unsigned long int *num_start,
866                            char name[VAR_NAME_LEN + 1])
867 {
868   return ((hint != NULL && make_hinted_name (dict, hint, name))
869           || make_numeric_name (dict, num_start, name));
870 }
871
872 /* Returns the weighting variable in dictionary D, or a null
873    pointer if the dictionary is unweighted. */
874 struct variable *
875 dict_get_weight (const struct dictionary *d)
876 {
877   assert (d != NULL);
878   assert (d->weight == NULL || dict_contains_var (d, d->weight));
879
880   return d->weight;
881 }
882
883 /* Returns the value of D's weighting variable in case C, except
884    that a negative weight is returned as 0.  Returns 1 if the
885    dictionary is unweighted.  Will warn about missing, negative,
886    or zero values if *WARN_ON_INVALID is true.  The function will
887    set *WARN_ON_INVALID to false if an invalid weight is
888    found. */
889 double
890 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
891                       bool *warn_on_invalid)
892 {
893   assert (d != NULL);
894   assert (c != NULL);
895
896   if (d->weight == NULL)
897     return 1.0;
898   else
899     {
900       double w = case_num (c, d->weight);
901       if (w < 0.0 || var_is_num_missing (d->weight, w, MV_ANY))
902         w = 0.0;
903       if ( w == 0.0 && warn_on_invalid != NULL && *warn_on_invalid ) {
904           *warn_on_invalid = false;
905           msg (SW, _("At least one case in the data file had a weight value "
906                      "that was user-missing, system-missing, zero, or "
907                      "negative.  These case(s) were ignored."));
908       }
909       return w;
910     }
911 }
912
913 /* Sets the weighting variable of D to V, or turning off
914    weighting if V is a null pointer. */
915 void
916 dict_set_weight (struct dictionary *d, struct variable *v)
917 {
918   assert (d != NULL);
919   assert (v == NULL || dict_contains_var (d, v));
920   assert (v == NULL || var_is_numeric (v));
921
922   d->weight = v;
923
924   if (d->changed) d->changed (d, d->changed_data);
925   if ( d->callbacks &&  d->callbacks->weight_changed )
926     d->callbacks->weight_changed (d,
927                                   v ? var_get_dict_index (v) : -1,
928                                   d->cb_data);
929 }
930
931 /* Returns the filter variable in dictionary D (see cmd_filter())
932    or a null pointer if the dictionary is unfiltered. */
933 struct variable *
934 dict_get_filter (const struct dictionary *d)
935 {
936   assert (d != NULL);
937   assert (d->filter == NULL || dict_contains_var (d, d->filter));
938
939   return d->filter;
940 }
941
942 /* Sets V as the filter variable for dictionary D.  Passing a
943    null pointer for V turn off filtering. */
944 void
945 dict_set_filter (struct dictionary *d, struct variable *v)
946 {
947   assert (d != NULL);
948   assert (v == NULL || dict_contains_var (d, v));
949   assert (v == NULL || var_is_numeric (v));
950
951   d->filter = v;
952
953   if (d->changed) d->changed (d, d->changed_data);
954   if ( d->callbacks && d->callbacks->filter_changed )
955     d->callbacks->filter_changed (d,
956                                   v ? var_get_dict_index (v) : -1,
957                                   d->cb_data);
958 }
959
960 /* Returns the case limit for dictionary D, or zero if the number
961    of cases is unlimited. */
962 casenumber
963 dict_get_case_limit (const struct dictionary *d)
964 {
965   assert (d != NULL);
966
967   return d->case_limit;
968 }
969
970 /* Sets CASE_LIMIT as the case limit for dictionary D.  Use
971    0 for CASE_LIMIT to indicate no limit. */
972 void
973 dict_set_case_limit (struct dictionary *d, casenumber case_limit)
974 {
975   assert (d != NULL);
976
977   d->case_limit = case_limit;
978 }
979
980 /* Returns the case index of the next value to be added to D.
981    This value is the number of `union value's that need to be
982    allocated to store a case for dictionary D. */
983 int
984 dict_get_next_value_idx (const struct dictionary *d)
985 {
986   assert (d != NULL);
987
988   return d->next_value_idx;
989 }
990
991 /* Returns the number of bytes needed to store a case for
992    dictionary D. */
993 size_t
994 dict_get_case_size (const struct dictionary *d)
995 {
996   assert (d != NULL);
997
998   return sizeof (union value) * dict_get_next_value_idx (d);
999 }
1000
1001 /* Reassigns values in dictionary D so that fragmentation is
1002    eliminated. */
1003 void
1004 dict_compact_values (struct dictionary *d)
1005 {
1006   size_t i;
1007
1008   d->next_value_idx = 0;
1009   for (i = 0; i < d->var_cnt; i++)
1010     {
1011       struct variable *v = d->var[i];
1012       set_var_case_index (v, d->next_value_idx);
1013       d->next_value_idx += var_get_value_cnt (v);
1014     }
1015 }
1016
1017 /*
1018    Reassigns case indices for D, increasing each index above START by
1019    the value PADDING.
1020 */
1021 static void
1022 dict_pad_values (struct dictionary *d, int start, int padding)
1023 {
1024   size_t i;
1025
1026   if ( padding <= 0 ) 
1027         return;
1028
1029   for (i = 0; i < d->var_cnt; ++i)
1030     {
1031       struct variable *v = d->var[i];
1032
1033       int index = var_get_case_index (v);
1034
1035       if ( index >= start)
1036         set_var_case_index (v, index + padding);
1037     }
1038
1039   d->next_value_idx += padding;
1040 }
1041
1042
1043 /* Returns the number of values occupied by the variables in
1044    dictionary D.  All variables are considered if EXCLUDE_CLASSES
1045    is 0, or it may contain one or more of (1u << DC_ORDINARY),
1046    (1u << DC_SYSTEM), or (1u << DC_SCRATCH) to exclude the
1047    corresponding type of variable.
1048
1049    The return value may be less than the number of values in one
1050    of dictionary D's cases (as returned by
1051    dict_get_next_value_idx) even if E is 0, because there may be
1052    gaps in D's cases due to deleted variables. */
1053 size_t
1054 dict_count_values (const struct dictionary *d, unsigned int exclude_classes)
1055 {
1056   size_t i;
1057   size_t cnt;
1058
1059   assert ((exclude_classes & ~((1u << DC_ORDINARY)
1060                                | (1u << DC_SYSTEM)
1061                                | (1u << DC_SCRATCH))) == 0);
1062
1063   cnt = 0;
1064   for (i = 0; i < d->var_cnt; i++)
1065     {
1066       enum dict_class class = var_get_dict_class (d->var[i]);
1067       if (!(exclude_classes & (1u << class)))
1068         cnt += var_get_value_cnt (d->var[i]);
1069     }
1070   return cnt;
1071 }
1072 \f
1073 /* Returns the SPLIT FILE vars (see cmd_split_file()).  Call
1074    dict_get_split_cnt() to determine how many SPLIT FILE vars
1075    there are.  Returns a null pointer if and only if there are no
1076    SPLIT FILE vars. */
1077 const struct variable *const *
1078 dict_get_split_vars (const struct dictionary *d)
1079 {
1080   assert (d != NULL);
1081
1082   return d->split;
1083 }
1084
1085 /* Returns the number of SPLIT FILE vars. */
1086 size_t
1087 dict_get_split_cnt (const struct dictionary *d)
1088 {
1089   assert (d != NULL);
1090
1091   return d->split_cnt;
1092 }
1093
1094 /* Removes variable V, which must be in D, from D's set of split
1095    variables. */
1096 void
1097 dict_unset_split_var (struct dictionary *d, struct variable *v)
1098 {
1099   int orig_count;
1100
1101   assert (dict_contains_var (d, v));
1102
1103   orig_count = d->split_cnt;
1104   d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
1105                                &v, compare_var_ptrs, NULL);
1106   if (orig_count != d->split_cnt)
1107     {
1108       if (d->changed) d->changed (d, d->changed_data);
1109       /* We changed the set of split variables so invoke the
1110          callback. */
1111       if (d->callbacks &&  d->callbacks->split_changed)
1112         d->callbacks->split_changed (d, d->cb_data);
1113     }
1114 }
1115
1116 /* Sets CNT split vars SPLIT in dictionary D. */
1117 void
1118 dict_set_split_vars (struct dictionary *d,
1119                      struct variable *const *split, size_t cnt)
1120 {
1121   assert (d != NULL);
1122   assert (cnt == 0 || split != NULL);
1123
1124   d->split_cnt = cnt;
1125   if ( cnt > 0 )
1126    {
1127     d->split = xnrealloc (d->split, cnt, sizeof *d->split) ;
1128     memcpy (d->split, split, cnt * sizeof *d->split);
1129    }
1130   else
1131    {
1132     free (d->split);
1133     d->split = NULL;
1134    }
1135
1136   if (d->changed) d->changed (d, d->changed_data);
1137   if ( d->callbacks &&  d->callbacks->split_changed )
1138     d->callbacks->split_changed (d, d->cb_data);
1139 }
1140
1141 /* Returns the file label for D, or a null pointer if D is
1142    unlabeled (see cmd_file_label()). */
1143 const char *
1144 dict_get_label (const struct dictionary *d)
1145 {
1146   assert (d != NULL);
1147
1148   return d->label;
1149 }
1150
1151 /* Sets D's file label to LABEL, truncating it to a maximum of 60
1152    characters. */
1153 void
1154 dict_set_label (struct dictionary *d, const char *label)
1155 {
1156   assert (d != NULL);
1157
1158   free (d->label);
1159   d->label = label != NULL ? xstrndup (label, 60) : NULL;
1160 }
1161
1162 /* Returns the documents for D, or a null pointer if D has no
1163    documents.  If the return value is nonnull, then the string
1164    will be an exact multiple of DOC_LINE_LENGTH bytes in length,
1165    with each segment corresponding to one line. */
1166 const char *
1167 dict_get_documents (const struct dictionary *d)
1168 {
1169   return ds_is_empty (&d->documents) ? NULL : ds_cstr (&d->documents);
1170 }
1171
1172 /* Sets the documents for D to DOCUMENTS, or removes D's
1173    documents if DOCUMENT is a null pointer.  If DOCUMENTS is
1174    nonnull, then it should be an exact multiple of
1175    DOC_LINE_LENGTH bytes in length, with each segment
1176    corresponding to one line. */
1177 void
1178 dict_set_documents (struct dictionary *d, const char *documents)
1179 {
1180   size_t remainder;
1181
1182   ds_assign_cstr (&d->documents, documents != NULL ? documents : "");
1183
1184   /* In case the caller didn't get it quite right, pad out the
1185      final line with spaces. */
1186   remainder = ds_length (&d->documents) % DOC_LINE_LENGTH;
1187   if (remainder != 0)
1188     ds_put_char_multiple (&d->documents, ' ', DOC_LINE_LENGTH - remainder);
1189 }
1190
1191 /* Drops the documents from dictionary D. */
1192 void
1193 dict_clear_documents (struct dictionary *d)
1194 {
1195   ds_clear (&d->documents);
1196 }
1197
1198 /* Appends LINE to the documents in D.  LINE will be truncated or
1199    padded on the right with spaces to make it exactly
1200    DOC_LINE_LENGTH bytes long. */
1201 void
1202 dict_add_document_line (struct dictionary *d, const char *line)
1203 {
1204   if (strlen (line) > DOC_LINE_LENGTH)
1205     {
1206       /* Note to translators: "bytes" is correct, not characters */
1207       msg (SW, _("Truncating document line to %d bytes."), DOC_LINE_LENGTH);
1208     }
1209   buf_copy_str_rpad (ds_put_uninit (&d->documents, DOC_LINE_LENGTH),
1210                      DOC_LINE_LENGTH, line);
1211 }
1212
1213 /* Returns the number of document lines in dictionary D. */
1214 size_t
1215 dict_get_document_line_cnt (const struct dictionary *d)
1216 {
1217   return ds_length (&d->documents) / DOC_LINE_LENGTH;
1218 }
1219
1220 /* Copies document line number IDX from dictionary D into
1221    LINE, trimming off any trailing white space. */
1222 void
1223 dict_get_document_line (const struct dictionary *d,
1224                         size_t idx, struct string *line)
1225 {
1226   assert (idx < dict_get_document_line_cnt (d));
1227   ds_assign_substring (line, ds_substr (&d->documents, idx * DOC_LINE_LENGTH,
1228                                         DOC_LINE_LENGTH));
1229   ds_rtrim (line, ss_cstr (CC_SPACES));
1230 }
1231
1232 /* Creates in D a vector named NAME that contains the CNT
1233    variables in VAR.  Returns true if successful, or false if a
1234    vector named NAME already exists in D. */
1235 bool
1236 dict_create_vector (struct dictionary *d,
1237                     const char *name,
1238                     struct variable **var, size_t cnt)
1239 {
1240   size_t i;
1241
1242   assert (var != NULL);
1243   assert (cnt > 0);
1244   for (i = 0; i < cnt; i++)
1245     assert (dict_contains_var (d, var[i]));
1246
1247   if (dict_lookup_vector (d, name) == NULL)
1248     {
1249       d->vector = xnrealloc (d->vector, d->vector_cnt + 1, sizeof *d->vector);
1250       d->vector[d->vector_cnt++] = vector_create (name, var, cnt);
1251       return true;
1252     }
1253   else
1254     return false;
1255 }
1256
1257 /* Creates in D a vector named NAME that contains the CNT
1258    variables in VAR.  A vector named NAME must not already exist
1259    in D. */
1260 void
1261 dict_create_vector_assert (struct dictionary *d,
1262                            const char *name,
1263                            struct variable **var, size_t cnt)
1264 {
1265   assert (dict_lookup_vector (d, name) == NULL);
1266   dict_create_vector (d, name, var, cnt);
1267 }
1268
1269 /* Returns the vector in D with index IDX, which must be less
1270    than dict_get_vector_cnt (D). */
1271 const struct vector *
1272 dict_get_vector (const struct dictionary *d, size_t idx)
1273 {
1274   assert (d != NULL);
1275   assert (idx < d->vector_cnt);
1276
1277   return d->vector[idx];
1278 }
1279
1280 /* Returns the number of vectors in D. */
1281 size_t
1282 dict_get_vector_cnt (const struct dictionary *d)
1283 {
1284   assert (d != NULL);
1285
1286   return d->vector_cnt;
1287 }
1288
1289 /* Looks up and returns the vector within D with the given
1290    NAME. */
1291 const struct vector *
1292 dict_lookup_vector (const struct dictionary *d, const char *name)
1293 {
1294   size_t i;
1295   for (i = 0; i < d->vector_cnt; i++)
1296     if (!strcasecmp (vector_get_name (d->vector[i]), name))
1297       return d->vector[i];
1298   return NULL;
1299 }
1300
1301 /* Deletes all vectors from D. */
1302 void
1303 dict_clear_vectors (struct dictionary *d)
1304 {
1305   size_t i;
1306
1307   for (i = 0; i < d->vector_cnt; i++)
1308     vector_destroy (d->vector[i]);
1309   free (d->vector);
1310
1311   d->vector = NULL;
1312   d->vector_cnt = 0;
1313 }
1314
1315 /* Returns D's attribute set.  The caller may examine or modify
1316    the attribute set, but must not destroy it.  Destroying D or
1317    calling dict_set_attributes for D will also destroy D's
1318    attribute set. */
1319 struct attrset *
1320 dict_get_attributes (const struct dictionary *d) 
1321 {
1322   return (struct attrset *) &d->attributes;
1323 }
1324
1325 /* Replaces D's attributes set by a copy of ATTRS. */
1326 void
1327 dict_set_attributes (struct dictionary *d, const struct attrset *attrs)
1328 {
1329   attrset_destroy (&d->attributes);
1330   attrset_clone (&d->attributes, attrs);
1331 }
1332
1333 /* Returns true if D has at least one attribute in its attribute
1334    set, false if D's attribute set is empty. */
1335 bool
1336 dict_has_attributes (const struct dictionary *d) 
1337 {
1338   return attrset_count (&d->attributes) > 0;
1339 }
1340
1341 /* Called from variable.c to notify the dictionary that some property of
1342    the variable has changed */
1343 void
1344 dict_var_changed (const struct variable *v)
1345 {
1346   if ( var_has_vardict (v))
1347     {
1348       const struct vardict_info *vdi = var_get_vardict (v);
1349       struct dictionary *d = vdi->dict;
1350
1351       if ( NULL == d)
1352         return;
1353
1354       if (d->changed ) d->changed (d, d->changed_data);
1355       if ( d->callbacks && d->callbacks->var_changed )
1356         d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
1357     }
1358 }
1359
1360
1361 /* Called from variable.c to notify the dictionary that the variable's width
1362    has changed */
1363 void
1364 dict_var_resized (const struct variable *v, int delta)
1365 {
1366   if ( var_has_vardict (v))
1367     {
1368       const struct vardict_info *vdi = var_get_vardict (v);
1369       struct dictionary *d;
1370
1371       d = vdi->dict;
1372
1373       dict_pad_values (d, var_get_case_index(v) + 1, delta);
1374
1375       if (d->changed) d->changed (d, d->changed_data);
1376       if ( d->callbacks && d->callbacks->var_resized )
1377         d->callbacks->var_resized (d, var_get_dict_index (v), delta, d->cb_data);
1378     }
1379 }
1380
1381 /* Called from variable.c to notify the dictionary that the variable's display width
1382    has changed */
1383 void
1384 dict_var_display_width_changed (const struct variable *v)
1385 {
1386   if ( var_has_vardict (v))
1387     {
1388       const struct vardict_info *vdi = var_get_vardict (v);
1389       struct dictionary *d;
1390
1391       d = vdi->dict;
1392
1393       if (d->changed) d->changed (d, d->changed_data);
1394       if ( d->callbacks && d->callbacks->var_display_width_changed )
1395         d->callbacks->var_display_width_changed (d, var_get_dict_index (v), d->cb_data);
1396     }
1397 }
1398