1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "data/variable.h"
23 #include "data/attributes.h"
24 #include "data/data-out.h"
25 #include "data/dictionary.h"
26 #include "data/format.h"
27 #include "data/identifier.h"
28 #include "data/missing-values.h"
29 #include "data/settings.h"
30 #include "data/value-labels.h"
31 #include "data/vardict.h"
32 #include "libpspp/assertion.h"
33 #include "libpspp/compiler.h"
34 #include "libpspp/hash-functions.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/message.h"
37 #include "libpspp/misc.h"
38 #include "libpspp/str.h"
40 #include "gl/minmax.h"
41 #include "gl/xalloc.h"
44 #define _(msgid) gettext (msgid)
49 /* Dictionary information. */
50 char *name; /* Variable name. Mixed case. */
51 int width; /* 0 for numeric, otherwise string width. */
52 struct missing_values miss; /* Missing values. */
53 struct fmt_spec print; /* Default format for PRINT. */
54 struct fmt_spec write; /* Default format for WRITE. */
55 struct val_labs *val_labs; /* Value labels. */
56 char *label; /* Variable label. */
57 struct string name_and_label; /* The name and label in the same string */
59 /* GUI information. */
60 enum measure measure; /* Nominal, ordinal, or continuous. */
61 int display_width; /* Width of data editor column. */
62 enum alignment alignment; /* Alignment of data in GUI. */
64 /* Case information. */
65 bool leave; /* Leave value from case to case? */
67 /* Data for use by containing dictionary. */
68 struct vardict_info *vardict;
70 /* Used only for system and portable file input and output.
73 size_t short_name_cnt;
75 /* Custom attributes. */
76 struct attrset attributes;
80 static bool var_set_label_quiet (struct variable *v, const char *label, bool issue_warning);
81 static void var_set_name_quiet (struct variable *v, const char *name);
83 /* Creates and returns a new variable with the given NAME and
84 WIDTH and other fields initialized to default values. The
85 variable is not added to a dictionary; for that, use
86 dict_create_var instead. */
88 var_create (const char *name, int width)
93 assert (width >= 0 && width <= MAX_STRING);
95 v = xzalloc (sizeof *v);
96 var_set_name_quiet (v, name);
98 mv_init (&v->miss, width);
99 v->leave = var_must_leave (v);
100 type = val_type_from_width (width);
101 v->alignment = var_default_alignment (type);
102 v->measure = var_default_measure (type);
103 v->display_width = var_default_display_width (width);
104 v->print = v->write = var_default_formats (width);
105 attrset_init (&v->attributes);
106 ds_init_empty (&v->name_and_label);
111 /* Destroys variable V.
112 V must not belong to a dictionary. If it does, use
113 dict_delete_var instead. */
115 var_destroy (struct variable *v)
119 assert (!var_has_vardict (v));
120 mv_destroy (&v->miss);
121 var_clear_short_names (v);
122 val_labs_destroy (v->val_labs);
123 var_set_label_quiet (v, NULL, false);
124 attrset_destroy (var_get_attributes (v));
126 ds_destroy (&v->name_and_label);
131 /* Variable names. */
133 /* Return variable V's name, as a UTF-8 encoded string. */
135 var_get_name (const struct variable *v)
142 /* Sets V's name to NAME, a UTF-8 encoded string.
143 Do not use this function for a variable in a dictionary. Use
144 dict_rename_var instead. */
146 var_set_name_quiet (struct variable *v, const char *name)
148 assert (!var_has_vardict (v));
149 assert (id_is_plausible (name, false));
152 v->name = xstrdup (name);
153 ds_destroy (&v->name_and_label);
154 ds_init_empty (&v->name_and_label);
157 /* Sets V's name to NAME, a UTF-8 encoded string.
158 Do not use this function for a variable in a dictionary. Use
159 dict_rename_var instead. */
161 var_set_name (struct variable *v, const char *name)
163 struct variable *ov = var_clone (v);
164 var_set_name_quiet (v, name);
165 dict_var_changed (v, VAR_TRAIT_NAME, ov);
168 /* Returns VAR's dictionary class. */
170 var_get_dict_class (const struct variable *var)
172 return dict_class_from_id (var->name);
175 /* A hsh_compare_func that orders variables A and B by their
178 compare_vars_by_name (const void *a_, const void *b_, const void *aux UNUSED)
180 const struct variable *a = a_;
181 const struct variable *b = b_;
183 return utf8_strcasecmp (a->name, b->name);
186 /* A hsh_hash_func that hashes variable V based on its name. */
188 hash_var_by_name (const void *v_, const void *aux UNUSED)
190 const struct variable *v = v_;
192 return utf8_hash_case_string (v->name, 0);
195 /* A hsh_compare_func that orders pointers to variables A and B
198 compare_var_ptrs_by_name (const void *a_, const void *b_,
199 const void *aux UNUSED)
201 struct variable *const *a = a_;
202 struct variable *const *b = b_;
204 return utf8_strcasecmp (var_get_name (*a), var_get_name (*b));
207 /* A hsh_compare_func that orders pointers to variables A and B
208 by their dictionary indexes. */
210 compare_var_ptrs_by_dict_index (const void *a_, const void *b_,
211 const void *aux UNUSED)
213 struct variable *const *a = a_;
214 struct variable *const *b = b_;
215 size_t a_index = var_get_dict_index (*a);
216 size_t b_index = var_get_dict_index (*b);
218 return a_index < b_index ? -1 : a_index > b_index;
221 /* A hsh_hash_func that hashes pointer to variable V based on its
224 hash_var_ptr_by_name (const void *v_, const void *aux UNUSED)
226 struct variable *const *v = v_;
228 return utf8_hash_case_string (var_get_name (*v), 0);
231 /* Returns the type of variable V. */
233 var_get_type (const struct variable *v)
235 return val_type_from_width (v->width);
238 /* Returns the width of variable V. */
240 var_get_width (const struct variable *v)
245 /* Changes the width of V to NEW_WIDTH.
246 This function should be used cautiously. */
248 var_set_width (struct variable *v, int new_width)
251 const int old_width = v->width;
252 unsigned int traits = 0;
254 if (old_width == new_width)
259 if (var_has_missing_values (v))
261 if (mv_is_resizable (&v->miss, new_width))
262 mv_resize (&v->miss, new_width);
265 mv_destroy (&v->miss);
266 mv_init (&v->miss, new_width);
268 traits |= VAR_TRAIT_MISSING_VALUES;
271 if (v->val_labs != NULL)
273 if (val_labs_can_set_width (v->val_labs, new_width))
274 val_labs_set_width (v->val_labs, new_width);
277 val_labs_destroy (v->val_labs);
280 traits |= VAR_TRAIT_VALUE_LABELS;
283 if (fmt_resize (&v->print, new_width))
284 traits |= VAR_TRAIT_PRINT_FORMAT;
286 if (fmt_resize (&v->write, new_width))
287 traits |= VAR_TRAIT_WRITE_FORMAT;
289 v->width = new_width;
290 traits |= VAR_TRAIT_WIDTH;
291 dict_var_changed (v, traits, ov);
294 /* Returns true if variable V is numeric, false otherwise. */
296 var_is_numeric (const struct variable *v)
298 return var_get_type (v) == VAL_NUMERIC;
301 /* Returns true if variable V is a string variable, false
304 var_is_alpha (const struct variable *v)
306 return var_get_type (v) == VAL_STRING;
309 /* Returns variable V's missing values. */
310 const struct missing_values *
311 var_get_missing_values (const struct variable *v)
316 /* Sets variable V's missing values to MISS, which must be of V's
317 width or at least resizable to V's width.
318 If MISS is null, then V's missing values, if any, are
321 var_set_missing_values_quiet (struct variable *v, const struct missing_values *miss)
325 assert (mv_is_resizable (miss, v->width));
326 mv_destroy (&v->miss);
327 mv_copy (&v->miss, miss);
328 mv_resize (&v->miss, v->width);
334 /* Sets variable V's missing values to MISS, which must be of V's
335 width or at least resizable to V's width.
336 If MISS is null, then V's missing values, if any, are
339 var_set_missing_values (struct variable *v, const struct missing_values *miss)
341 struct variable *ov = var_clone (v);
342 var_set_missing_values_quiet (v, miss);
343 dict_var_changed (v, VAR_TRAIT_MISSING_VALUES, ov);
346 /* Sets variable V to have no user-missing values. */
348 var_clear_missing_values (struct variable *v)
350 var_set_missing_values (v, NULL);
353 /* Returns true if V has any user-missing values,
356 var_has_missing_values (const struct variable *v)
358 return !mv_is_empty (&v->miss);
361 /* Returns true if VALUE is in the given CLASS of missing values
362 in V, false otherwise. */
364 var_is_value_missing (const struct variable *v, const union value *value,
367 return mv_is_value_missing (&v->miss, value, class);
370 /* Returns true if D is in the given CLASS of missing values in
372 V must be a numeric variable. */
374 var_is_num_missing (const struct variable *v, double d, enum mv_class class)
376 return mv_is_num_missing (&v->miss, d, class);
379 /* Returns true if S[] is a missing value for V, false otherwise.
380 S[] must contain exactly as many characters as V's width.
381 V must be a string variable. */
383 var_is_str_missing (const struct variable *v, const uint8_t s[],
386 return mv_is_str_missing (&v->miss, s, class);
389 /* Returns variable V's value labels,
390 possibly a null pointer if it has none. */
391 const struct val_labs *
392 var_get_value_labels (const struct variable *v)
397 /* Returns true if variable V has at least one value label. */
399 var_has_value_labels (const struct variable *v)
401 return val_labs_count (v->val_labs) > 0;
404 /* Sets variable V's value labels to a copy of VLS,
405 which must have a width equal to V's width or one that can be
406 changed to V's width.
407 If VLS is null, then V's value labels, if any, are removed. */
409 var_set_value_labels_quiet (struct variable *v, const struct val_labs *vls)
411 val_labs_destroy (v->val_labs);
416 assert (val_labs_can_set_width (vls, v->width));
417 v->val_labs = val_labs_clone (vls);
418 val_labs_set_width (v->val_labs, v->width);
423 /* Sets variable V's value labels to a copy of VLS,
424 which must have a width equal to V's width or one that can be
425 changed to V's width.
426 If VLS is null, then V's value labels, if any, are removed. */
428 var_set_value_labels (struct variable *v, const struct val_labs *vls)
430 struct variable *ov = var_clone (v);
431 var_set_value_labels_quiet (v, vls);
432 dict_var_changed (v, VAR_TRAIT_LABEL, ov);
436 /* Makes sure that V has a set of value labels,
437 by assigning one to it if necessary. */
439 alloc_value_labels (struct variable *v)
441 if (v->val_labs == NULL)
442 v->val_labs = val_labs_create (v->width);
445 /* Attempts to add a value label with the given VALUE and UTF-8 encoded LABEL
446 to V. Returns true if successful, false otherwise (probably due to an
449 In LABEL, the two-byte sequence "\\n" is interpreted as a new-line. */
451 var_add_value_label (struct variable *v,
452 const union value *value, const char *label)
454 alloc_value_labels (v);
455 return val_labs_add (v->val_labs, value, label);
458 /* Adds or replaces a value label with the given VALUE and UTF-8 encoded LABEL
461 In LABEL, the two-byte sequence "\\n" is interpreted as a new-line. */
463 var_replace_value_label (struct variable *v,
464 const union value *value, const char *label)
466 alloc_value_labels (v);
467 val_labs_replace (v->val_labs, value, label);
470 /* Removes V's value labels, if any. */
472 var_clear_value_labels (struct variable *v)
474 var_set_value_labels (v, NULL);
477 /* Returns the label associated with VALUE for variable V, as a UTF-8 string in
478 a format suitable for output, or a null pointer if none. */
480 var_lookup_value_label (const struct variable *v, const union value *value)
482 return val_labs_find (v->val_labs, value);
486 Append to STR the string representation of VALUE for variable V.
487 STR must be a pointer to an initialised struct string.
490 append_value (const struct variable *v, const union value *value,
493 char *s = data_out (value, var_get_encoding (v), &v->print);
494 ds_put_cstr (str, s);
498 /* Append STR with a string representing VALUE for variable V.
499 That is, if VALUE has a label, append that label,
500 otherwise format VALUE and append the formatted string.
501 STR must be a pointer to an initialised struct string.
504 var_append_value_name (const struct variable *v, const union value *value,
507 enum settings_value_style style = settings_get_value_style ();
508 const char *name = var_lookup_value_label (v, value);
512 case SETTINGS_VAL_STYLE_VALUES:
513 append_value (v, value, str);
516 case SETTINGS_VAL_STYLE_LABELS:
518 append_value (v, value, str);
520 ds_put_cstr (str, name);
523 case SETTINGS_VAL_STYLE_BOTH:
525 append_value (v, value, str);
528 ds_put_cstr (str, " (");
529 ds_put_cstr (str, name);
530 ds_put_cstr (str, ")");
536 /* Print and write formats. */
538 /* Returns V's print format specification. */
539 const struct fmt_spec *
540 var_get_print_format (const struct variable *v)
545 /* Sets V's print format specification to PRINT, which must be a
546 valid format specification for a variable of V's width
547 (ordinarily an output format, but input formats are not
550 var_set_print_format_quiet (struct variable *v, const struct fmt_spec *print)
552 if (!fmt_equal (&v->print, print))
554 assert (fmt_check_width_compat (print, v->width));
559 /* Sets V's print format specification to PRINT, which must be a
560 valid format specification for a variable of V's width
561 (ordinarily an output format, but input formats are not
564 var_set_print_format (struct variable *v, const struct fmt_spec *print)
566 struct variable *ov = var_clone (v);
567 var_set_print_format_quiet (v, print);
568 dict_var_changed (v, VAR_TRAIT_PRINT_FORMAT, ov);
571 /* Returns V's write format specification. */
572 const struct fmt_spec *
573 var_get_write_format (const struct variable *v)
578 /* Sets V's write format specification to WRITE, which must be a
579 valid format specification for a variable of V's width
580 (ordinarily an output format, but input formats are not
583 var_set_write_format_quiet (struct variable *v, const struct fmt_spec *write)
585 if (!fmt_equal (&v->write, write))
587 assert (fmt_check_width_compat (write, v->width));
592 /* Sets V's write format specification to WRITE, which must be a
593 valid format specification for a variable of V's width
594 (ordinarily an output format, but input formats are not
597 var_set_write_format (struct variable *v, const struct fmt_spec *write)
599 struct variable *ov = var_clone (v);
600 var_set_write_format_quiet (v, write);
601 dict_var_changed (v, VAR_TRAIT_WRITE_FORMAT, ov);
605 /* Sets V's print and write format specifications to FORMAT,
606 which must be a valid format specification for a variable of
607 V's width (ordinarily an output format, but input formats are
610 var_set_both_formats (struct variable *v, const struct fmt_spec *format)
612 struct variable *ov = var_clone (v);
613 var_set_print_format_quiet (v, format);
614 var_set_write_format_quiet (v, format);
615 dict_var_changed (v, VAR_TRAIT_PRINT_FORMAT | VAR_TRAIT_WRITE_FORMAT, ov);
618 /* Returns the default print and write format for a variable of
619 the given TYPE, as set by var_create. The return value can be
620 used to reset a variable's print and write formats to the
623 var_default_formats (int width)
626 ? fmt_for_output (FMT_F, 8, 2)
627 : fmt_for_output (FMT_A, width, 0));
633 /* Update the combined name and label string if necessary */
635 update_vl_string (const struct variable *v)
637 /* Cast away const! */
638 struct string *str = (struct string *) &v->name_and_label;
640 if (ds_is_empty (str))
643 ds_put_format (str, _("%s (%s)"), v->label, v->name);
645 ds_put_cstr (str, v->name);
650 /* Return a string representing this variable, in the form most
651 appropriate from a human factors perspective, that is, its
652 variable label if it has one, otherwise its name. */
654 var_to_string (const struct variable *v)
656 enum settings_var_style style = settings_get_var_style ();
660 case SETTINGS_VAR_STYLE_NAMES:
663 case SETTINGS_VAR_STYLE_LABELS:
664 return v->label != NULL ? v->label : v->name;
666 case SETTINGS_VAR_STYLE_BOTH:
667 update_vl_string (v);
668 return ds_cstr (&v->name_and_label);
676 /* Returns V's variable label, or a null pointer if it has none. */
678 var_get_label (const struct variable *v)
683 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
684 and trailing white space. If LABEL is a null pointer or if LABEL is an
685 empty string (after stripping white space), then V's variable label (if any)
688 Variable labels are limited to 255 bytes in V's encoding (as returned by
689 var_get_encoding()). If LABEL fits within this limit, this function returns
690 true. Otherwise, the variable label is set to a truncated value, this
691 function returns false and, if ISSUE_WARNING is true, issues a warning. */
693 var_set_label_quiet (struct variable *v, const char *label, bool issue_warning)
695 bool truncated = false;
700 if (label != NULL && label[strspn (label, CC_SPACES)])
702 const char *dict_encoding = var_get_encoding (v);
703 struct substring s = ss_cstr (label);
706 if (dict_encoding != NULL)
708 enum { MAX_LABEL_LEN = 255 };
710 trunc_len = utf8_encoding_trunc_len (label, dict_encoding,
712 if (ss_length (s) > trunc_len)
715 msg (SW, _("Truncating variable label for variable `%s' to %d "
716 "bytes."), var_get_name (v), MAX_LABEL_LEN);
717 ss_truncate (&s, trunc_len);
722 v->label = ss_xstrdup (s);
725 ds_destroy (&v->name_and_label);
726 ds_init_empty (&v->name_and_label);
733 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
734 and trailing white space. If LABEL is a null pointer or if LABEL is an
735 empty string (after stripping white space), then V's variable label (if any)
738 Variable labels are limited to 255 bytes in V's encoding (as returned by
739 var_get_encoding()). If LABEL fits within this limit, this function returns
740 true. Otherwise, the variable label is set to a truncated value, this
741 function returns false and, if ISSUE_WARNING is true, issues a warning. */
743 var_set_label (struct variable *v, const char *label, bool issue_warning)
745 struct variable *ov = var_clone (v);
746 bool truncated = var_set_label_quiet (v, label, issue_warning);
748 dict_var_changed (v, VAR_TRAIT_LABEL, ov);
754 /* Removes any variable label from V. */
756 var_clear_label (struct variable *v)
758 var_set_label (v, NULL, false);
761 /* Returns true if V has a variable V,
764 var_has_label (const struct variable *v)
766 return v->label != NULL;
769 /* Returns true if M is a valid variable measurement level,
772 measure_is_valid (enum measure m)
774 return m == MEASURE_NOMINAL || m == MEASURE_ORDINAL || m == MEASURE_SCALE;
777 /* Returns a string version of measurement level M, for display to a user. */
779 measure_to_string (enum measure m)
783 case MEASURE_NOMINAL:
786 case MEASURE_ORDINAL:
797 /* Returns V's measurement level. */
799 var_get_measure (const struct variable *v)
804 /* Sets V's measurement level to MEASURE. */
806 var_set_measure_quiet (struct variable *v, enum measure measure)
808 assert (measure_is_valid (measure));
809 v->measure = measure;
813 /* Sets V's measurement level to MEASURE. */
815 var_set_measure (struct variable *v, enum measure measure)
817 struct variable *ov = var_clone (v);
818 var_set_measure_quiet (v, measure);
819 dict_var_changed (v, VAR_TRAIT_MEASURE, ov);
823 /* Returns the default measurement level for a variable of the
824 given TYPE, as set by var_create. The return value can be
825 used to reset a variable's measurement level to the
828 var_default_measure (enum val_type type)
830 return type == VAL_NUMERIC ? MEASURE_SCALE : MEASURE_NOMINAL;
833 /* Returns V's display width, which applies only to GUIs. */
835 var_get_display_width (const struct variable *v)
837 return v->display_width;
840 /* Sets V's display width to DISPLAY_WIDTH. */
842 var_set_display_width_quiet (struct variable *v, int new_width)
844 if (v->display_width != new_width)
846 v->display_width = new_width;
851 var_set_display_width (struct variable *v, int new_width)
853 struct variable *ov = var_clone (v);
854 var_set_display_width_quiet (v, new_width);
855 dict_var_changed (v, VAR_TRAIT_DISPLAY_WIDTH, ov);
859 /* Returns the default display width for a variable of the given
860 WIDTH, as set by var_create. The return value can be used to
861 reset a variable's display width to the default. */
863 var_default_display_width (int width)
865 return width == 0 ? 8 : MIN (width, 32);
868 /* Returns true if A is a valid alignment,
871 alignment_is_valid (enum alignment a)
873 return a == ALIGN_LEFT || a == ALIGN_RIGHT || a == ALIGN_CENTRE;
876 /* Returns a string version of alignment A, for display to a user. */
878 alignment_to_string (enum alignment a)
896 /* Returns V's display alignment, which applies only to GUIs. */
898 var_get_alignment (const struct variable *v)
903 /* Sets V's display alignment to ALIGNMENT. */
905 var_set_alignment_quiet (struct variable *v, enum alignment alignment)
907 assert (alignment_is_valid (alignment));
908 v->alignment = alignment;
911 /* Sets V's display alignment to ALIGNMENT. */
913 var_set_alignment (struct variable *v, enum alignment alignment)
915 struct variable *ov = var_clone (v);
916 var_set_alignment_quiet (v, alignment);
917 dict_var_changed (v, VAR_TRAIT_ALIGNMENT, ov);
921 /* Returns the default display alignment for a variable of the
922 given TYPE, as set by var_create. The return value can be
923 used to reset a variable's display alignment to the default. */
925 var_default_alignment (enum val_type type)
927 return type == VAL_NUMERIC ? ALIGN_RIGHT : ALIGN_LEFT;
930 /* Whether variables' values should be preserved from case to
933 /* Returns true if variable V's value should be left from case to
934 case, instead of being reset to system-missing or blanks. */
936 var_get_leave (const struct variable *v)
941 /* Sets V's leave setting to LEAVE. */
943 var_set_leave_quiet (struct variable *v, bool leave)
945 assert (leave || !var_must_leave (v));
950 /* Sets V's leave setting to LEAVE. */
952 var_set_leave (struct variable *v, bool leave)
954 struct variable *ov = var_clone (v);
955 var_set_leave_quiet (v, leave);
956 dict_var_changed (v, VAR_TRAIT_LEAVE, ov);
960 /* Returns true if V must be left from case to case,
961 false if it can be set either way. */
963 var_must_leave (const struct variable *v)
965 return var_get_dict_class (v) == DC_SCRATCH;
968 /* Returns the number of short names stored in VAR.
970 Short names are used only for system and portable file input
971 and output. They are upper-case only, not necessarily unique,
972 and limited to SHORT_NAME_LEN characters (plus a null
973 terminator). Ordinarily a variable has at most one short
974 name, but very long string variables (longer than 255 bytes)
975 may have more. A variable might not have any short name at
976 all if it hasn't been saved to or read from a system or
979 var_get_short_name_cnt (const struct variable *var)
981 return var->short_name_cnt;
984 /* Returns VAR's short name with the given IDX, if it has one
985 with that index, or a null pointer otherwise. Short names may
986 be sparse: even if IDX is less than the number of short names
987 in VAR, this function may return a null pointer. */
989 var_get_short_name (const struct variable *var, size_t idx)
991 return idx < var->short_name_cnt ? var->short_names[idx] : NULL;
994 /* Sets VAR's short name with the given IDX to the UTF-8 string SHORT_NAME.
995 The caller must already have checked that, in the dictionary encoding,
996 SHORT_NAME is no more than SHORT_NAME_LEN bytes long. The new short name
997 will be converted to uppercase.
999 Specifying a null pointer for SHORT_NAME clears the specified short name. */
1001 var_set_short_name (struct variable *var, size_t idx, const char *short_name)
1003 struct variable *ov = var_clone (var);
1005 assert (short_name == NULL || id_is_plausible (short_name, false));
1007 /* Clear old short name numbered IDX, if any. */
1008 if (idx < var->short_name_cnt)
1010 free (var->short_names[idx]);
1011 var->short_names[idx] = NULL;
1014 /* Install new short name for IDX. */
1015 if (short_name != NULL)
1017 if (idx >= var->short_name_cnt)
1019 size_t old_cnt = var->short_name_cnt;
1021 var->short_name_cnt = MAX (idx * 2, 1);
1022 var->short_names = xnrealloc (var->short_names, var->short_name_cnt,
1023 sizeof *var->short_names);
1024 for (i = old_cnt; i < var->short_name_cnt; i++)
1025 var->short_names[i] = NULL;
1027 var->short_names[idx] = utf8_to_upper (short_name);
1030 dict_var_changed (var, VAR_TRAIT_NAME, ov);
1033 /* Clears V's short names. */
1035 var_clear_short_names (struct variable *v)
1039 for (i = 0; i < v->short_name_cnt; i++)
1040 free (v->short_names[i]);
1041 free (v->short_names);
1042 v->short_names = NULL;
1043 v->short_name_cnt = 0;
1046 /* Relationship with dictionary. */
1048 /* Returns V's index within its dictionary, the value
1049 for which "dict_get_var (dict, index)" will return V.
1050 V must be in a dictionary. */
1052 var_get_dict_index (const struct variable *v)
1054 assert (var_has_vardict (v));
1055 return vardict_get_dict_index (v->vardict);
1058 /* Returns V's index within the case represented by its
1059 dictionary, that is, the value for which "case_data_idx (case,
1060 index)" will return the data for V in that case.
1061 V must be in a dictionary. */
1063 var_get_case_index (const struct variable *v)
1065 assert (var_has_vardict (v));
1066 return vardict_get_case_index (v->vardict);
1069 /* Returns variable V's attribute set. The caller may examine or
1070 modify the attribute set, but must not destroy it. Destroying
1071 V, or calling var_set_attributes() on V, will also destroy its
1074 var_get_attributes (const struct variable *v)
1076 return CONST_CAST (struct attrset *, &v->attributes);
1079 /* Replaces variable V's attributes set by a copy of ATTRS. */
1081 var_set_attributes_quiet (struct variable *v, const struct attrset *attrs)
1083 attrset_destroy (&v->attributes);
1084 attrset_clone (&v->attributes, attrs);
1087 /* Replaces variable V's attributes set by a copy of ATTRS. */
1089 var_set_attributes (struct variable *v, const struct attrset *attrs)
1091 struct variable *ov = var_clone (v);
1092 var_set_attributes_quiet (v, attrs);
1093 dict_var_changed (v, VAR_TRAIT_ATTRIBUTES, ov);
1097 /* Returns true if V has any custom attributes, false if it has none. */
1099 var_has_attributes (const struct variable *v)
1101 return attrset_count (&v->attributes) > 0;
1105 /* Creates and returns a clone of OLD_VAR. Most properties of
1106 the new variable are copied from OLD_VAR, except:
1108 - The variable's short name is not copied, because there is
1109 no reason to give a new variable with potentially a new
1110 name the same short name.
1112 - The new variable is not added to OLD_VAR's dictionary by
1113 default. Use dict_clone_var, instead, to do that.
1116 var_clone (const struct variable *old_var)
1118 struct variable *new_var = var_create (var_get_name (old_var),
1119 var_get_width (old_var));
1121 var_set_missing_values_quiet (new_var, var_get_missing_values (old_var));
1122 var_set_print_format_quiet (new_var, var_get_print_format (old_var));
1123 var_set_write_format_quiet (new_var, var_get_write_format (old_var));
1124 var_set_value_labels_quiet (new_var, var_get_value_labels (old_var));
1125 var_set_label_quiet (new_var, var_get_label (old_var), false);
1126 var_set_measure_quiet (new_var, var_get_measure (old_var));
1127 var_set_display_width_quiet (new_var, var_get_display_width (old_var));
1128 var_set_alignment_quiet (new_var, var_get_alignment (old_var));
1129 var_set_leave_quiet (new_var, var_get_leave (old_var));
1130 var_set_attributes_quiet (new_var, var_get_attributes (old_var));
1137 /* Returns the encoding of values of variable VAR. (This is actually a
1138 property of the dictionary.) Returns null if no specific encoding has been
1141 var_get_encoding (const struct variable *var)
1143 return (var_has_vardict (var)
1144 ? dict_get_encoding (vardict_get_dictionary (var->vardict))
1148 /* Returns V's vardict structure. */
1149 struct vardict_info *
1150 var_get_vardict (const struct variable *v)
1152 return CONST_CAST (struct vardict_info *, v->vardict);
1155 /* Sets V's vardict data to VARDICT. */
1157 var_set_vardict (struct variable *v, struct vardict_info *vardict)
1159 v->vardict = vardict;
1162 /* Returns true if V has vardict data. */
1164 var_has_vardict (const struct variable *v)
1166 return v->vardict != NULL;
1169 /* Clears V's vardict data. */
1171 var_clear_vardict (struct variable *v)