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