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