Added brief documentation about the FACTOR command's rotation feature
[pspp-builds.git] / src / data / variable.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include "variable.h"
19
20 #include <stdlib.h>
21
22 #include <data/attributes.h>
23 #include <data/category.h>
24 #include <data/data-out.h>
25 #include <data/format.h>
26 #include <data/dictionary.h>
27 #include <data/identifier.h>
28 #include <data/missing-values.h>
29 #include <data/value-labels.h>
30 #include <data/vardict.h>
31
32 #include <libpspp/misc.h>
33 #include <libpspp/assertion.h>
34 #include <libpspp/compiler.h>
35 #include <libpspp/hash.h>
36 #include <libpspp/message.h>
37 #include <libpspp/str.h>
38
39 #include "minmax.h"
40 #include "xalloc.h"
41
42 #include "gettext.h"
43 #define _(msgid) gettext (msgid)
44
45 /* A variable. */
46 struct variable
47   {
48     /* Dictionary information. */
49     char name[VAR_NAME_LEN + 1]; /* Variable name.  Mixed case. */
50     int width;                  /* 0 for numeric, otherwise string width. */
51     struct missing_values miss; /* Missing values. */
52     struct fmt_spec print;      /* Default format for PRINT. */
53     struct fmt_spec write;      /* Default format for WRITE. */
54     struct val_labs *val_labs;  /* Value labels. */
55     char *label;                /* Variable label. */
56
57     /* GUI information. */
58     enum measure measure;       /* Nominal, ordinal, or continuous. */
59     int display_width;          /* Width of data editor column. */
60     enum alignment alignment;   /* Alignment of data in GUI. */
61
62     /* Case information. */
63     bool leave;                 /* Leave value from case to case? */
64
65     /* Data for use by containing dictionary. */
66     struct vardict_info *vardict;
67
68     /* Used only for system and portable file input and output.
69        See short-names.h. */
70     char **short_names;
71     size_t short_name_cnt;
72
73     /* Each command may use these fields as needed. */
74     void *aux;
75     void (*aux_dtor) (struct variable *);
76
77     /* Values of a categorical variable.  Procedures need
78        vectors with binary entries, so any variable of type ALPHA will
79        have its values stored here. */
80     struct cat_vals *obs_vals;
81
82     /* Custom attributes. */
83     struct attrset attributes;
84   };
85 \f
86 /* Creates and returns a new variable with the given NAME and
87    WIDTH and other fields initialized to default values.  The
88    variable is not added to a dictionary; for that, use
89    dict_create_var instead. */
90 struct variable *
91 var_create (const char *name, int width)
92 {
93   struct variable *v;
94   enum val_type type;
95
96   assert (width >= 0 && width <= MAX_STRING);
97
98   v = xmalloc (sizeof *v);
99   v->vardict = NULL;
100   var_set_name (v, name);
101   v->width = width;
102   mv_init (&v->miss, width);
103   v->leave = var_must_leave (v);
104   type = val_type_from_width (width);
105   v->alignment = var_default_alignment (type);
106   v->measure = var_default_measure (type);
107   v->display_width = var_default_display_width (width);
108   v->print = v->write = var_default_formats (width);
109   v->val_labs = NULL;
110   v->label = NULL;
111   v->short_names = NULL;
112   v->short_name_cnt = 0;
113   v->aux = NULL;
114   v->aux_dtor = NULL;
115   v->obs_vals = NULL;
116   attrset_init (&v->attributes);
117
118   return v;
119 }
120
121 /* Creates and returns a clone of OLD_VAR.  Most properties of
122    the new variable are copied from OLD_VAR, except:
123
124     - The variable's short name is not copied, because there is
125       no reason to give a new variable with potentially a new
126       name the same short name.
127
128     - The new variable is not added to OLD_VAR's dictionary by
129       default.  Use dict_clone_var, instead, to do that.
130
131     - Auxiliary data and obs_vals are not copied. */
132 struct variable *
133 var_clone (const struct variable *old_var)
134 {
135   struct variable *new_var = var_create (var_get_name (old_var),
136                                          var_get_width (old_var));
137
138   var_set_missing_values (new_var, var_get_missing_values (old_var));
139   var_set_print_format (new_var, var_get_print_format (old_var));
140   var_set_write_format (new_var, var_get_write_format (old_var));
141   var_set_value_labels (new_var, var_get_value_labels (old_var));
142   var_set_label (new_var, var_get_label (old_var));
143   var_set_measure (new_var, var_get_measure (old_var));
144   var_set_display_width (new_var, var_get_display_width (old_var));
145   var_set_alignment (new_var, var_get_alignment (old_var));
146   var_set_leave (new_var, var_get_leave (old_var));
147   var_set_attributes (new_var, var_get_attributes (old_var));
148
149   return new_var;
150 }
151
152 /* Destroys variable V.
153    V must not belong to a dictionary.  If it does, use
154    dict_delete_var instead. */
155 void
156 var_destroy (struct variable *v)
157 {
158   if (v != NULL)
159     {
160       assert (!var_has_vardict (v));
161       mv_destroy (&v->miss);
162       cat_stored_values_destroy (v->obs_vals);
163       var_clear_short_names (v);
164       var_clear_aux (v);
165       val_labs_destroy (v->val_labs);
166       var_clear_label (v);
167       free (v);
168     }
169 }
170 \f
171 /* Variable names. */
172
173 /* Return variable V's name. */
174 const char *
175 var_get_name (const struct variable *v)
176 {
177   return v->name;
178 }
179
180 /* Sets V's name to NAME.
181    Do not use this function for a variable in a dictionary.  Use
182    dict_rename_var instead. */
183 void
184 var_set_name (struct variable *v, const char *name)
185 {
186   assert (!var_has_vardict (v));
187   assert (var_is_plausible_name (name, false));
188
189   str_copy_trunc (v->name, sizeof v->name, name);
190   dict_var_changed (v);
191 }
192
193 /* Returns true if NAME is an acceptable name for a variable,
194    false otherwise.  If ISSUE_ERROR is true, issues an
195    explanatory error message on failure. */
196 bool
197 var_is_valid_name (const char *name, bool issue_error)
198 {
199   bool plausible;
200   size_t length, i;
201
202   /* Note that strlen returns number of BYTES, not the number of
203      CHARACTERS */
204   length = strlen (name);
205
206   plausible = var_is_plausible_name(name, issue_error);
207
208   if ( ! plausible )
209     return false;
210
211
212   if (!lex_is_id1 (name[0]))
213     {
214       if (issue_error)
215         msg (SE, _("Character `%c' (in %s) may not appear "
216                    "as the first character in a variable name."),
217              name[0], name);
218       return false;
219     }
220
221
222   for (i = 0; i < length; i++)
223     {
224     if (!lex_is_idn (name[i]))
225       {
226         if (issue_error)
227           msg (SE, _("Character `%c' (in %s) may not appear in "
228                      "a variable name."),
229                name[i], name);
230         return false;
231       }
232     }
233
234   return true;
235 }
236
237 /* Returns true if NAME is an plausible name for a variable,
238    false otherwise.  If ISSUE_ERROR is true, issues an
239    explanatory error message on failure.
240    This function makes no use of LC_CTYPE.
241 */
242 bool
243 var_is_plausible_name (const char *name, bool issue_error)
244 {
245   size_t length;
246
247   /* Note that strlen returns number of BYTES, not the number of
248      CHARACTERS */
249   length = strlen (name);
250   if (length < 1)
251     {
252       if (issue_error)
253         msg (SE, _("Variable name cannot be empty string."));
254       return false;
255     }
256   else if (length > VAR_NAME_LEN)
257     {
258       if (issue_error)
259         msg (SE, _("Variable name %s exceeds %d-character limit."),
260              name, (int) VAR_NAME_LEN);
261       return false;
262     }
263
264   if (lex_id_to_token (ss_cstr (name)) != T_ID)
265     {
266       if (issue_error)
267         msg (SE, _("`%s' may not be used as a variable name because it "
268                    "is a reserved word."), name);
269       return false;
270     }
271
272   return true;
273 }
274
275 /* Returns VAR's dictionary class. */
276 enum dict_class
277 var_get_dict_class (const struct variable *var)
278 {
279   return dict_class_from_id (var->name);
280 }
281
282 /* A hsh_compare_func that orders variables A and B by their
283    names. */
284 int
285 compare_vars_by_name (const void *a_, const void *b_, const void *aux UNUSED)
286 {
287   const struct variable *a = a_;
288   const struct variable *b = b_;
289
290   return strcasecmp (a->name, b->name);
291 }
292
293 /* A hsh_hash_func that hashes variable V based on its name. */
294 unsigned
295 hash_var_by_name (const void *v_, const void *aux UNUSED)
296 {
297   const struct variable *v = v_;
298
299   return hash_case_string (v->name, 0);
300 }
301
302 /* A hsh_compare_func that orders pointers to variables A and B
303    by their names. */
304 int
305 compare_var_ptrs_by_name (const void *a_, const void *b_,
306                           const void *aux UNUSED)
307 {
308   struct variable *const *a = a_;
309   struct variable *const *b = b_;
310
311   return strcasecmp (var_get_name (*a), var_get_name (*b));
312 }
313
314 /* A hsh_compare_func that orders pointers to variables A and B
315    by their dictionary indexes. */
316 int
317 compare_var_ptrs_by_dict_index (const void *a_, const void *b_,
318                                 const void *aux UNUSED)
319 {
320   struct variable *const *a = a_;
321   struct variable *const *b = b_;
322   size_t a_index = var_get_dict_index (*a);
323   size_t b_index = var_get_dict_index (*b);
324
325   return a_index < b_index ? -1 : a_index > b_index;
326 }
327
328 /* A hsh_hash_func that hashes pointer to variable V based on its
329    name. */
330 unsigned
331 hash_var_ptr_by_name (const void *v_, const void *aux UNUSED)
332 {
333   struct variable *const *v = v_;
334
335   return hash_case_string (var_get_name (*v), 0);
336 }
337 \f
338 /* Returns the type of variable V. */
339 enum val_type
340 var_get_type (const struct variable *v)
341 {
342   return val_type_from_width (v->width);
343 }
344
345 /* Returns the width of variable V. */
346 int
347 var_get_width (const struct variable *v)
348 {
349   return v->width;
350 }
351
352 /* Changes the width of V to NEW_WIDTH.
353    This function should be used cautiously. */
354 void
355 var_set_width (struct variable *v, int new_width)
356 {
357   const int old_width = v->width;
358
359   if (old_width == new_width)
360     return;
361
362   if (mv_is_resizable (&v->miss, new_width))
363     mv_resize (&v->miss, new_width);
364   else
365     {
366       mv_destroy (&v->miss);
367       mv_init (&v->miss, new_width);
368     }
369
370   if (v->val_labs != NULL)
371     {
372       if (val_labs_can_set_width (v->val_labs, new_width))
373         val_labs_set_width (v->val_labs, new_width);
374       else
375         {
376           val_labs_destroy (v->val_labs);
377           v->val_labs = NULL;
378         }
379     }
380
381   fmt_resize (&v->print, new_width);
382   fmt_resize (&v->write, new_width);
383
384   v->width = new_width;
385   dict_var_resized (v, old_width);
386   dict_var_changed (v);
387 }
388
389 /* Returns true if variable V is numeric, false otherwise. */
390 bool
391 var_is_numeric (const struct variable *v)
392 {
393   return var_get_type (v) == VAL_NUMERIC;
394 }
395
396 /* Returns true if variable V is a string variable, false
397    otherwise. */
398 bool
399 var_is_alpha (const struct variable *v)
400 {
401   return var_get_type (v) == VAL_STRING;
402 }
403 \f
404 /* Returns variable V's missing values. */
405 const struct missing_values *
406 var_get_missing_values (const struct variable *v)
407 {
408   return &v->miss;
409 }
410
411 /* Sets variable V's missing values to MISS, which must be of V's
412    width or at least resizable to V's width.
413    If MISS is null, then V's missing values, if any, are
414    cleared. */
415 void
416 var_set_missing_values (struct variable *v, const struct missing_values *miss)
417 {
418   if (miss != NULL)
419     {
420       assert (mv_is_resizable (miss, v->width));
421       mv_destroy (&v->miss);
422       mv_copy (&v->miss, miss);
423       mv_resize (&v->miss, v->width);
424     }
425   else
426     mv_clear (&v->miss);
427
428   dict_var_changed (v);
429 }
430
431 /* Sets variable V to have no user-missing values. */
432 void
433 var_clear_missing_values (struct variable *v)
434 {
435   var_set_missing_values (v, NULL);
436 }
437
438 /* Returns true if V has any user-missing values,
439    false otherwise. */
440 bool
441 var_has_missing_values (const struct variable *v)
442 {
443   return !mv_is_empty (&v->miss);
444 }
445
446 /* Returns true if VALUE is in the given CLASS of missing values
447    in V, false otherwise. */
448 bool
449 var_is_value_missing (const struct variable *v, const union value *value,
450                       enum mv_class class)
451 {
452   return mv_is_value_missing (&v->miss, value, class);
453 }
454
455 /* Returns true if D is in the given CLASS of missing values in
456    V, false otherwise.
457    V must be a numeric variable. */
458 bool
459 var_is_num_missing (const struct variable *v, double d, enum mv_class class)
460 {
461   return mv_is_num_missing (&v->miss, d, class);
462 }
463
464 /* Returns true if S[] is a missing value for V, false otherwise.
465    S[] must contain exactly as many characters as V's width.
466    V must be a string variable. */
467 bool
468 var_is_str_missing (const struct variable *v, const uint8_t s[],
469                     enum mv_class class)
470 {
471   return mv_is_str_missing (&v->miss, s, class);
472 }
473 \f
474 /* Returns variable V's value labels,
475    possibly a null pointer if it has none. */
476 const struct val_labs *
477 var_get_value_labels (const struct variable *v)
478 {
479   return v->val_labs;
480 }
481
482 /* Returns true if variable V has at least one value label. */
483 bool
484 var_has_value_labels (const struct variable *v)
485 {
486   return val_labs_count (v->val_labs) > 0;
487 }
488
489 /* Sets variable V's value labels to a copy of VLS,
490    which must have a width equal to V's width or one that can be
491    changed to V's width.
492    If VLS is null, then V's value labels, if any, are removed. */
493 void
494 var_set_value_labels (struct variable *v, const struct val_labs *vls)
495 {
496   val_labs_destroy (v->val_labs);
497   v->val_labs = NULL;
498
499   if (vls != NULL)
500     {
501       assert (val_labs_can_set_width (vls, v->width));
502       v->val_labs = val_labs_clone (vls);
503       val_labs_set_width (v->val_labs, v->width);
504       dict_var_changed (v);
505     }
506 }
507
508 /* Makes sure that V has a set of value labels,
509    by assigning one to it if necessary. */
510 static void
511 alloc_value_labels (struct variable *v)
512 {
513   if (v->val_labs == NULL)
514     v->val_labs = val_labs_create (v->width);
515 }
516
517 /* Attempts to add a value label with the given VALUE and LABEL
518    to V.  Returns true if successful, false if VALUE has an
519    existing label or if V is a long string variable. */
520 bool
521 var_add_value_label (struct variable *v,
522                      const union value *value, const char *label)
523 {
524   alloc_value_labels (v);
525   return val_labs_add (v->val_labs, value, label);
526 }
527
528 /* Adds or replaces a value label with the given VALUE and LABEL
529    to V.
530    Has no effect if V is a long string variable. */
531 void
532 var_replace_value_label (struct variable *v,
533                          const union value *value, const char *label)
534 {
535   alloc_value_labels (v);
536   val_labs_replace (v->val_labs, value, label);
537 }
538
539 /* Removes V's value labels, if any. */
540 void
541 var_clear_value_labels (struct variable *v)
542 {
543   var_set_value_labels (v, NULL);
544 }
545
546 /* Returns the label associated with VALUE for variable V,
547    or a null pointer if none. */
548 const char *
549 var_lookup_value_label (const struct variable *v, const union value *value)
550 {
551   return val_labs_find (v->val_labs, value);
552 }
553
554 /* Append STR with a string representing VALUE for variable V.
555    That is, if VALUE has a label, append that label,
556    otherwise format VALUE and append the formatted string.
557    STR must be a pointer to an initialised struct string.
558 */
559 void
560 var_append_value_name (const struct variable *v, const union value *value,
561                        struct string *str)
562 {
563   const char *name = var_lookup_value_label (v, value);
564   if (name == NULL)
565     {
566       char *s = data_out (value, var_get_encoding (v), &v->print);
567       ds_put_cstr (str, s);
568       free (s);
569     }
570   else
571     ds_put_cstr (str, name);
572 }
573 \f
574 /* Print and write formats. */
575
576 /* Returns V's print format specification. */
577 const struct fmt_spec *
578 var_get_print_format (const struct variable *v)
579 {
580   return &v->print;
581 }
582
583 /* Sets V's print format specification to PRINT, which must be a
584    valid format specification for a variable of V's width
585    (ordinarily an output format, but input formats are not
586    rejected). */
587 void
588 var_set_print_format (struct variable *v, const struct fmt_spec *print)
589 {
590   assert (fmt_check_width_compat (print, v->width));
591   v->print = *print;
592   dict_var_changed (v);
593 }
594
595 /* Returns V's write format specification. */
596 const struct fmt_spec *
597 var_get_write_format (const struct variable *v)
598 {
599   return &v->write;
600 }
601
602 /* Sets V's write format specification to WRITE, which must be a
603    valid format specification for a variable of V's width
604    (ordinarily an output format, but input formats are not
605    rejected). */
606 void
607 var_set_write_format (struct variable *v, const struct fmt_spec *write)
608 {
609   assert (fmt_check_width_compat (write, v->width));
610   v->write = *write;
611   dict_var_changed (v);
612 }
613
614 /* Sets V's print and write format specifications to FORMAT,
615    which must be a valid format specification for a variable of
616    V's width (ordinarily an output format, but input formats are
617    not rejected). */
618 void
619 var_set_both_formats (struct variable *v, const struct fmt_spec *format)
620 {
621   var_set_print_format (v, format);
622   var_set_write_format (v, format);
623 }
624
625 /* Returns the default print and write format for a variable of
626    the given TYPE, as set by var_create.  The return value can be
627    used to reset a variable's print and write formats to the
628    default. */
629 struct fmt_spec
630 var_default_formats (int width)
631 {
632   return (width == 0
633           ? fmt_for_output (FMT_F, 8, 2)
634           : fmt_for_output (FMT_A, width, 0));
635 }
636 \f
637 /* Return a string representing this variable, in the form most
638    appropriate from a human factors perspective, that is, its
639    variable label if it has one, otherwise its name. */
640 const char *
641 var_to_string (const struct variable *v)
642 {
643   return v->label != NULL ? v->label : v->name;
644 }
645
646 /* Returns V's variable label, or a null pointer if it has none. */
647 const char *
648 var_get_label (const struct variable *v)
649 {
650   return v->label;
651 }
652
653 /* Sets V's variable label to LABEL, stripping off leading and
654    trailing white space and truncating to 255 characters.
655    If LABEL is a null pointer or if LABEL is an empty string
656    (after stripping white space), then V's variable label (if
657    any) is removed. */
658 void
659 var_set_label (struct variable *v, const char *label)
660 {
661   free (v->label);
662   v->label = NULL;
663
664   if (label != NULL)
665     {
666       struct substring s = ss_cstr (label);
667       ss_trim (&s, ss_cstr (CC_SPACES));
668       ss_truncate (&s, 255);
669       if (!ss_is_empty (s))
670         v->label = ss_xstrdup (s);
671     }
672   dict_var_changed (v);
673 }
674
675 /* Removes any variable label from V. */
676 void
677 var_clear_label (struct variable *v)
678 {
679   var_set_label (v, NULL);
680 }
681
682 /* Returns true if V has a variable V,
683    false otherwise. */
684 bool
685 var_has_label (const struct variable *v)
686 {
687   return v->label != NULL;
688 }
689 \f
690 /* Returns true if M is a valid variable measurement level,
691    false otherwise. */
692 bool
693 measure_is_valid (enum measure m)
694 {
695   return m == MEASURE_NOMINAL || m == MEASURE_ORDINAL || m == MEASURE_SCALE;
696 }
697
698 /* Returns V's measurement level. */
699 enum measure
700 var_get_measure (const struct variable *v)
701 {
702   return v->measure;
703 }
704
705 /* Sets V's measurement level to MEASURE. */
706 void
707 var_set_measure (struct variable *v, enum measure measure)
708 {
709   assert (measure_is_valid (measure));
710   v->measure = measure;
711   dict_var_changed (v);
712 }
713
714 /* Returns the default measurement level for a variable of the
715    given TYPE, as set by var_create.  The return value can be
716    used to reset a variable's measurement level to the
717    default. */
718 enum measure
719 var_default_measure (enum val_type type)
720 {
721   return type == VAL_NUMERIC ? MEASURE_SCALE : MEASURE_NOMINAL;
722 }
723 \f
724 /* Returns V's display width, which applies only to GUIs. */
725 int
726 var_get_display_width (const struct variable *v)
727 {
728   return v->display_width;
729 }
730
731 /* Sets V's display width to DISPLAY_WIDTH. */
732 void
733 var_set_display_width (struct variable *v, int new_width)
734 {
735   int old_width = v->display_width;
736
737   v->display_width = new_width;
738
739   if ( old_width != new_width)
740     dict_var_display_width_changed (v);
741
742   dict_var_changed (v);
743 }
744
745 /* Returns the default display width for a variable of the given
746    WIDTH, as set by var_create.  The return value can be used to
747    reset a variable's display width to the default. */
748 int
749 var_default_display_width (int width)
750 {
751   return width == 0 ? 8 : MIN (width, 32);
752 }
753 \f
754 /* Returns true if A is a valid alignment,
755    false otherwise. */
756 bool
757 alignment_is_valid (enum alignment a)
758 {
759   return a == ALIGN_LEFT || a == ALIGN_RIGHT || a == ALIGN_CENTRE;
760 }
761
762 /* Returns V's display alignment, which applies only to GUIs. */
763 enum alignment
764 var_get_alignment (const struct variable *v)
765 {
766   return v->alignment;
767 }
768
769 /* Sets V's display alignment to ALIGNMENT. */
770 void
771 var_set_alignment (struct variable *v, enum alignment alignment)
772 {
773   assert (alignment_is_valid (alignment));
774   v->alignment = alignment;
775   dict_var_changed (v);
776 }
777
778 /* Returns the default display alignment for a variable of the
779    given TYPE, as set by var_create.  The return value can be
780    used to reset a variable's display alignment to the default. */
781 enum alignment
782 var_default_alignment (enum val_type type)
783 {
784   return type == VAL_NUMERIC ? ALIGN_RIGHT : ALIGN_LEFT;
785 }
786 \f
787 /* Whether variables' values should be preserved from case to
788    case. */
789
790 /* Returns true if variable V's value should be left from case to
791    case, instead of being reset to system-missing or blanks. */
792 bool
793 var_get_leave (const struct variable *v)
794 {
795   return v->leave;
796 }
797
798 /* Sets V's leave setting to LEAVE. */
799 void
800 var_set_leave (struct variable *v, bool leave)
801 {
802   assert (leave || !var_must_leave (v));
803   v->leave = leave;
804   dict_var_changed (v);
805 }
806
807 /* Returns true if V must be left from case to case,
808    false if it can be set either way. */
809 bool
810 var_must_leave (const struct variable *v)
811 {
812   return var_get_dict_class (v) == DC_SCRATCH;
813 }
814 \f
815 /* Returns the number of short names stored in VAR.
816
817    Short names are used only for system and portable file input
818    and output.  They are upper-case only, not necessarily unique,
819    and limited to SHORT_NAME_LEN characters (plus a null
820    terminator).  Ordinarily a variable has at most one short
821    name, but very long string variables (longer than 255 bytes)
822    may have more.  A variable might not have any short name at
823    all if it hasn't been saved to or read from a system or
824    portable file. */
825 size_t
826 var_get_short_name_cnt (const struct variable *var) 
827 {
828   return var->short_name_cnt;
829 }
830
831 /* Returns VAR's short name with the given IDX, if it has one
832    with that index, or a null pointer otherwise.  Short names may
833    be sparse: even if IDX is less than the number of short names
834    in VAR, this function may return a null pointer. */
835 const char *
836 var_get_short_name (const struct variable *var, size_t idx)
837 {
838   return idx < var->short_name_cnt ? var->short_names[idx] : NULL;
839 }
840
841 /* Sets VAR's short name with the given IDX to SHORT_NAME,
842    truncating it to SHORT_NAME_LEN characters and converting it
843    to uppercase in the process.  Specifying a null pointer for
844    SHORT_NAME clears the specified short name. */
845 void
846 var_set_short_name (struct variable *var, size_t idx, const char *short_name)
847 {
848   assert (short_name == NULL || var_is_plausible_name (short_name, false));
849
850   /* Clear old short name numbered IDX, if any. */
851   if (idx < var->short_name_cnt) 
852     {
853       free (var->short_names[idx]);
854       var->short_names[idx] = NULL; 
855     }
856
857   /* Install new short name for IDX. */
858   if (short_name != NULL) 
859     {
860       if (idx >= var->short_name_cnt)
861         {
862           size_t old_cnt = var->short_name_cnt;
863           size_t i;
864           var->short_name_cnt = MAX (idx * 2, 1);
865           var->short_names = xnrealloc (var->short_names, var->short_name_cnt,
866                                         sizeof *var->short_names);
867           for (i = old_cnt; i < var->short_name_cnt; i++)
868             var->short_names[i] = NULL;
869         }
870       var->short_names[idx] = xstrndup (short_name, MAX_SHORT_STRING);
871       str_uppercase (var->short_names[idx]);
872     }
873
874   dict_var_changed (var);
875 }
876
877 /* Clears V's short names. */
878 void
879 var_clear_short_names (struct variable *v)
880 {
881   size_t i;
882
883   for (i = 0; i < v->short_name_cnt; i++)
884     free (v->short_names[i]);
885   free (v->short_names);
886   v->short_names = NULL;
887   v->short_name_cnt = 0;
888 }
889 \f
890 /* Relationship with dictionary. */
891
892 /* Returns V's index within its dictionary, the value
893    for which "dict_get_var (dict, index)" will return V.
894    V must be in a dictionary. */
895 size_t
896 var_get_dict_index (const struct variable *v)
897 {
898   assert (var_has_vardict (v));
899   return vardict_get_dict_index (v->vardict);
900 }
901
902 /* Returns V's index within the case represented by its
903    dictionary, that is, the value for which "case_data_idx (case,
904    index)" will return the data for V in that case.
905    V must be in a dictionary. */
906 size_t
907 var_get_case_index (const struct variable *v)
908 {
909   assert (var_has_vardict (v));
910   return vardict_get_case_index (v->vardict);
911 }
912 \f
913 /* Returns V's auxiliary data, or a null pointer if none has been
914    attached. */
915 void *
916 var_get_aux (const struct variable *v)
917 {
918   return v->aux;
919 }
920
921 /* Assign auxiliary data AUX to variable V, which must not
922    already have auxiliary data.  Before V's auxiliary data is
923    cleared, AUX_DTOR(V) will be called.  (var_dtor_free, below,
924    may be appropriate for use as AUX_DTOR.) */
925 void *
926 var_attach_aux (const struct variable *v_,
927                 void *aux, void (*aux_dtor) (struct variable *))
928 {
929   struct variable *v = CONST_CAST (struct variable *, v_);
930   assert (v->aux == NULL);
931   assert (aux != NULL);
932   v->aux = aux;
933   v->aux_dtor = aux_dtor;
934   return aux;
935 }
936
937 /* Remove auxiliary data, if any, from V, and return it, without
938    calling any associated destructor. */
939 void *
940 var_detach_aux (struct variable *v)
941 {
942   void *aux = v->aux;
943   assert (aux != NULL);
944   v->aux = NULL;
945   return aux;
946 }
947
948 /* Clears auxiliary data, if any, from V, and calls any
949    associated destructor. */
950 void
951 var_clear_aux (struct variable *v)
952 {
953   if (v->aux != NULL)
954     {
955       if (v->aux_dtor != NULL)
956         v->aux_dtor (v);
957       v->aux = NULL;
958     }
959 }
960
961 /* This function is appropriate for use an auxiliary data
962    destructor (passed as AUX_DTOR to var_attach_aux()) for the
963    case where the auxiliary data should be passed to free(). */
964 void
965 var_dtor_free (struct variable *v)
966 {
967   free (v->aux);
968 }
969 \f
970 /* Observed categorical values. */
971
972 /* Returns V's observed categorical values,
973    which V must have. */
974 struct cat_vals *
975 var_get_obs_vals (const struct variable *v)
976 {
977   assert (v->obs_vals != NULL);
978   return v->obs_vals;
979 }
980
981 /* Sets V's observed categorical values to CAT_VALS.
982    V becomes the owner of CAT_VALS. */
983 void
984 var_set_obs_vals (const struct variable *v_, struct cat_vals *cat_vals)
985 {
986   struct variable *v = CONST_CAST (struct variable *, v_ );
987   cat_stored_values_destroy (v->obs_vals);
988   v->obs_vals = cat_vals;
989 }
990
991 /* Returns true if V has observed categorical values,
992    false otherwise. */
993 bool
994 var_has_obs_vals (const struct variable *v)
995 {
996   return v->obs_vals != NULL;
997 }
998 \f
999 /* Returns variable V's attribute set.  The caller may examine or
1000    modify the attribute set, but must not destroy it.  Destroying
1001    V, or calling var_set_attributes() on V, will also destroy its
1002    attribute set. */
1003 struct attrset *
1004 var_get_attributes (const struct variable *v) 
1005 {
1006   return CONST_CAST (struct attrset *, &v->attributes);
1007 }
1008
1009 /* Replaces variable V's attributes set by a copy of ATTRS. */
1010 void
1011 var_set_attributes (struct variable *v, const struct attrset *attrs) 
1012 {
1013   attrset_destroy (&v->attributes);
1014   attrset_clone (&v->attributes, attrs);
1015 }
1016
1017 /* Returns true if V has any custom attributes, false if it has none. */
1018 bool
1019 var_has_attributes (const struct variable *v)
1020 {
1021   return attrset_count (&v->attributes) > 0;
1022 }
1023 \f
1024 /* Returns the encoding of values of variable VAR.  (This is actually a
1025    property of the dictionary.)  Returns null if no specific encoding has been
1026    set.  */
1027 const char *
1028 var_get_encoding (const struct variable *var)
1029 {
1030   return (var_has_vardict (var)
1031           ? dict_get_encoding (vardict_get_dictionary (var->vardict))
1032           : NULL);
1033 }
1034 \f
1035 /* Returns V's vardict structure. */
1036 struct vardict_info *
1037 var_get_vardict (const struct variable *v)
1038 {
1039   return CONST_CAST (struct vardict_info *, v->vardict);
1040 }
1041
1042 /* Sets V's vardict data to VARDICT. */
1043 void
1044 var_set_vardict (struct variable *v, struct vardict_info *vardict)
1045 {
1046   v->vardict = vardict;
1047 }
1048
1049 /* Returns true if V has vardict data. */
1050 bool
1051 var_has_vardict (const struct variable *v)
1052 {
1053   return v->vardict != NULL;
1054 }
1055
1056 /* Clears V's vardict data. */
1057 void
1058 var_clear_vardict (struct variable *v)
1059 {
1060   v->vardict = NULL;
1061 }