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