1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012 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;
253 if (old_width == new_width)
258 if (mv_is_resizable (&v->miss, new_width))
259 mv_resize (&v->miss, new_width);
262 mv_destroy (&v->miss);
263 mv_init (&v->miss, new_width);
266 if (v->val_labs != NULL)
268 if (val_labs_can_set_width (v->val_labs, new_width))
269 val_labs_set_width (v->val_labs, new_width);
272 val_labs_destroy (v->val_labs);
277 fmt_resize (&v->print, new_width);
278 fmt_resize (&v->write, new_width);
280 v->width = new_width;
281 dict_var_resized (v, old_width);
282 dict_var_changed (v, VAR_TRAIT_WIDTH, ov);
285 /* Returns true if variable V is numeric, false otherwise. */
287 var_is_numeric (const struct variable *v)
289 return var_get_type (v) == VAL_NUMERIC;
292 /* Returns true if variable V is a string variable, false
295 var_is_alpha (const struct variable *v)
297 return var_get_type (v) == VAL_STRING;
300 /* Returns variable V's missing values. */
301 const struct missing_values *
302 var_get_missing_values (const struct variable *v)
307 /* Sets variable V's missing values to MISS, which must be of V's
308 width or at least resizable to V's width.
309 If MISS is null, then V's missing values, if any, are
312 var_set_missing_values_quiet (struct variable *v, const struct missing_values *miss)
316 assert (mv_is_resizable (miss, v->width));
317 mv_destroy (&v->miss);
318 mv_copy (&v->miss, miss);
319 mv_resize (&v->miss, v->width);
325 /* Sets variable V's missing values to MISS, which must be of V's
326 width or at least resizable to V's width.
327 If MISS is null, then V's missing values, if any, are
330 var_set_missing_values (struct variable *v, const struct missing_values *miss)
332 struct variable *ov = var_clone (v);
333 var_set_missing_values_quiet (v, miss);
334 dict_var_changed (v, VAR_TRAIT_MISSING_VALUES, ov);
337 /* Sets variable V to have no user-missing values. */
339 var_clear_missing_values (struct variable *v)
341 var_set_missing_values (v, NULL);
344 /* Returns true if V has any user-missing values,
347 var_has_missing_values (const struct variable *v)
349 return !mv_is_empty (&v->miss);
352 /* Returns true if VALUE is in the given CLASS of missing values
353 in V, false otherwise. */
355 var_is_value_missing (const struct variable *v, const union value *value,
358 return mv_is_value_missing (&v->miss, value, class);
361 /* Returns true if D is in the given CLASS of missing values in
363 V must be a numeric variable. */
365 var_is_num_missing (const struct variable *v, double d, enum mv_class class)
367 return mv_is_num_missing (&v->miss, d, class);
370 /* Returns true if S[] is a missing value for V, false otherwise.
371 S[] must contain exactly as many characters as V's width.
372 V must be a string variable. */
374 var_is_str_missing (const struct variable *v, const uint8_t s[],
377 return mv_is_str_missing (&v->miss, s, class);
380 /* Returns variable V's value labels,
381 possibly a null pointer if it has none. */
382 const struct val_labs *
383 var_get_value_labels (const struct variable *v)
388 /* Returns true if variable V has at least one value label. */
390 var_has_value_labels (const struct variable *v)
392 return val_labs_count (v->val_labs) > 0;
395 /* Sets variable V's value labels to a copy of VLS,
396 which must have a width equal to V's width or one that can be
397 changed to V's width.
398 If VLS is null, then V's value labels, if any, are removed. */
400 var_set_value_labels_quiet (struct variable *v, const struct val_labs *vls)
402 val_labs_destroy (v->val_labs);
407 assert (val_labs_can_set_width (vls, v->width));
408 v->val_labs = val_labs_clone (vls);
409 val_labs_set_width (v->val_labs, v->width);
414 /* Sets variable V's value labels to a copy of VLS,
415 which must have a width equal to V's width or one that can be
416 changed to V's width.
417 If VLS is null, then V's value labels, if any, are removed. */
419 var_set_value_labels (struct variable *v, const struct val_labs *vls)
421 struct variable *ov = var_clone (v);
422 var_set_value_labels_quiet (v, vls);
423 dict_var_changed (v, VAR_TRAIT_LABEL, ov);
427 /* Makes sure that V has a set of value labels,
428 by assigning one to it if necessary. */
430 alloc_value_labels (struct variable *v)
432 if (v->val_labs == NULL)
433 v->val_labs = val_labs_create (v->width);
436 /* Attempts to add a value label with the given VALUE and UTF-8 encoded LABEL
437 to V. Returns true if successful, false otherwise (probably due to an
440 In LABEL, the two-byte sequence "\\n" is interpreted as a new-line. */
442 var_add_value_label (struct variable *v,
443 const union value *value, const char *label)
445 alloc_value_labels (v);
446 return val_labs_add (v->val_labs, value, label);
449 /* Adds or replaces a value label with the given VALUE and UTF-8 encoded LABEL
452 In LABEL, the two-byte sequence "\\n" is interpreted as a new-line. */
454 var_replace_value_label (struct variable *v,
455 const union value *value, const char *label)
457 alloc_value_labels (v);
458 val_labs_replace (v->val_labs, value, label);
461 /* Removes V's value labels, if any. */
463 var_clear_value_labels (struct variable *v)
465 var_set_value_labels (v, NULL);
468 /* Returns the label associated with VALUE for variable V, as a UTF-8 string in
469 a format suitable for output, or a null pointer if none. */
471 var_lookup_value_label (const struct variable *v, const union value *value)
473 return val_labs_find (v->val_labs, value);
477 Append to STR the string representation of VALUE for variable V.
478 STR must be a pointer to an initialised struct string.
481 append_value (const struct variable *v, const union value *value,
484 char *s = data_out (value, var_get_encoding (v), &v->print);
485 ds_put_cstr (str, s);
489 /* Append STR with a string representing VALUE for variable V.
490 That is, if VALUE has a label, append that label,
491 otherwise format VALUE and append the formatted string.
492 STR must be a pointer to an initialised struct string.
495 var_append_value_name (const struct variable *v, const union value *value,
498 enum settings_value_style style = settings_get_value_style ();
499 const char *name = var_lookup_value_label (v, value);
503 case SETTINGS_VAL_STYLE_VALUES:
504 append_value (v, value, str);
507 case SETTINGS_VAL_STYLE_LABELS:
509 append_value (v, value, str);
511 ds_put_cstr (str, name);
514 case SETTINGS_VAL_STYLE_BOTH:
516 append_value (v, value, str);
519 ds_put_cstr (str, " (");
520 ds_put_cstr (str, name);
521 ds_put_cstr (str, ")");
527 /* Print and write formats. */
529 /* Returns V's print format specification. */
530 const struct fmt_spec *
531 var_get_print_format (const struct variable *v)
536 /* Sets V's print format specification to PRINT, which must be a
537 valid format specification for a variable of V's width
538 (ordinarily an output format, but input formats are not
541 var_set_print_format_quiet (struct variable *v, const struct fmt_spec *print)
543 if (!fmt_equal (&v->print, print))
545 assert (fmt_check_width_compat (print, v->width));
550 /* Sets V's print format specification to PRINT, which must be a
551 valid format specification for a variable of V's width
552 (ordinarily an output format, but input formats are not
555 var_set_print_format (struct variable *v, const struct fmt_spec *print)
557 struct variable *ov = var_clone (v);
558 var_set_print_format_quiet (v, print);
559 dict_var_changed (v, VAR_TRAIT_FORMAT, ov);
562 /* Returns V's write format specification. */
563 const struct fmt_spec *
564 var_get_write_format (const struct variable *v)
569 /* Sets V's write format specification to WRITE, which must be a
570 valid format specification for a variable of V's width
571 (ordinarily an output format, but input formats are not
574 var_set_write_format_quiet (struct variable *v, const struct fmt_spec *write)
576 if (!fmt_equal (&v->write, write))
578 assert (fmt_check_width_compat (write, v->width));
583 /* Sets V's write format specification to WRITE, 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
588 var_set_write_format (struct variable *v, const struct fmt_spec *write)
590 struct variable *ov = var_clone (v);
591 var_set_write_format_quiet (v, write);
592 dict_var_changed (v, VAR_TRAIT_FORMAT, ov);
596 /* Sets V's print and write format specifications to FORMAT,
597 which must be a valid format specification for a variable of
598 V's width (ordinarily an output format, but input formats are
601 var_set_both_formats (struct variable *v, const struct fmt_spec *format)
603 struct variable *ov = var_clone (v);
604 var_set_print_format_quiet (v, format);
605 var_set_write_format_quiet (v, format);
606 dict_var_changed (v, VAR_TRAIT_FORMAT, ov);
609 /* Returns the default print and write format for a variable of
610 the given TYPE, as set by var_create. The return value can be
611 used to reset a variable's print and write formats to the
614 var_default_formats (int width)
617 ? fmt_for_output (FMT_F, 8, 2)
618 : fmt_for_output (FMT_A, width, 0));
624 /* Update the combined name and label string if necessary */
626 update_vl_string (const struct variable *v)
628 /* Cast away const! */
629 struct string *str = (struct string *) &v->name_and_label;
631 if (ds_is_empty (str))
634 ds_put_format (str, _("%s (%s)"), v->label, v->name);
636 ds_put_cstr (str, v->name);
641 /* Return a string representing this variable, in the form most
642 appropriate from a human factors perspective, that is, its
643 variable label if it has one, otherwise its name. */
645 var_to_string (const struct variable *v)
647 enum settings_var_style style = settings_get_var_style ();
651 case SETTINGS_VAR_STYLE_NAMES:
654 case SETTINGS_VAR_STYLE_LABELS:
655 return v->label != NULL ? v->label : v->name;
657 case SETTINGS_VAR_STYLE_BOTH:
658 update_vl_string (v);
659 return ds_cstr (&v->name_and_label);
667 /* Returns V's variable label, or a null pointer if it has none. */
669 var_get_label (const struct variable *v)
674 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
675 and trailing white space. If LABEL is a null pointer or if LABEL is an
676 empty string (after stripping white space), then V's variable label (if any)
679 Variable labels are limited to 255 bytes in V's encoding (as returned by
680 var_get_encoding()). If LABEL fits within this limit, this function returns
681 true. Otherwise, the variable label is set to a truncated value, this
682 function returns false and, if ISSUE_WARNING is true, issues a warning. */
684 var_set_label_quiet (struct variable *v, const char *label, bool issue_warning)
686 bool truncated = false;
691 if (label != NULL && label[strspn (label, CC_SPACES)])
693 const char *dict_encoding = var_get_encoding (v);
694 struct substring s = ss_cstr (label);
697 if (dict_encoding != NULL)
699 enum { MAX_LABEL_LEN = 255 };
701 trunc_len = utf8_encoding_trunc_len (label, dict_encoding,
703 if (ss_length (s) > trunc_len)
706 msg (SW, _("Truncating variable label for variable `%s' to %d "
707 "bytes."), var_get_name (v), MAX_LABEL_LEN);
708 ss_truncate (&s, trunc_len);
713 v->label = ss_xstrdup (s);
716 ds_destroy (&v->name_and_label);
717 ds_init_empty (&v->name_and_label);
724 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
725 and trailing white space. If LABEL is a null pointer or if LABEL is an
726 empty string (after stripping white space), then V's variable label (if any)
729 Variable labels are limited to 255 bytes in V's encoding (as returned by
730 var_get_encoding()). If LABEL fits within this limit, this function returns
731 true. Otherwise, the variable label is set to a truncated value, this
732 function returns false and, if ISSUE_WARNING is true, issues a warning. */
734 var_set_label (struct variable *v, const char *label, bool issue_warning)
736 struct variable *ov = var_clone (v);
737 bool truncated = var_set_label_quiet (v, label, issue_warning);
739 dict_var_changed (v, VAR_TRAIT_LABEL, ov);
745 /* Removes any variable label from V. */
747 var_clear_label (struct variable *v)
749 var_set_label (v, NULL, false);
752 /* Returns true if V has a variable V,
755 var_has_label (const struct variable *v)
757 return v->label != NULL;
760 /* Returns true if M is a valid variable measurement level,
763 measure_is_valid (enum measure m)
765 return m == MEASURE_NOMINAL || m == MEASURE_ORDINAL || m == MEASURE_SCALE;
768 /* Returns a string version of measurement level M, for display to a user. */
770 measure_to_string (enum measure m)
774 case MEASURE_NOMINAL:
777 case MEASURE_ORDINAL:
788 /* Returns V's measurement level. */
790 var_get_measure (const struct variable *v)
795 /* Sets V's measurement level to MEASURE. */
797 var_set_measure_quiet (struct variable *v, enum measure measure)
799 assert (measure_is_valid (measure));
800 v->measure = measure;
804 /* Sets V's measurement level to MEASURE. */
806 var_set_measure (struct variable *v, enum measure measure)
808 struct variable *ov = var_clone (v);
809 var_set_measure_quiet (v, measure);
810 dict_var_changed (v, VAR_TRAIT_MEASURE, ov);
814 /* Returns the default measurement level for a variable of the
815 given TYPE, as set by var_create. The return value can be
816 used to reset a variable's measurement level to the
819 var_default_measure (enum val_type type)
821 return type == VAL_NUMERIC ? MEASURE_SCALE : MEASURE_NOMINAL;
824 /* Returns V's display width, which applies only to GUIs. */
826 var_get_display_width (const struct variable *v)
828 return v->display_width;
831 /* Sets V's display width to DISPLAY_WIDTH. */
833 var_set_display_width_quiet (struct variable *v, int new_width)
835 if (v->display_width != new_width)
837 v->display_width = new_width;
838 dict_var_display_width_changed (v);
843 var_set_display_width (struct variable *v, int new_width)
845 struct variable *ov = var_clone (v);
846 var_set_display_width_quiet (v, new_width);
847 dict_var_changed (v, VAR_TRAIT_DISPLAY_WIDTH, ov);
851 /* Returns the default display width for a variable of the given
852 WIDTH, as set by var_create. The return value can be used to
853 reset a variable's display width to the default. */
855 var_default_display_width (int width)
857 return width == 0 ? 8 : MIN (width, 32);
860 /* Returns true if A is a valid alignment,
863 alignment_is_valid (enum alignment a)
865 return a == ALIGN_LEFT || a == ALIGN_RIGHT || a == ALIGN_CENTRE;
868 /* Returns a string version of alignment A, for display to a user. */
870 alignment_to_string (enum alignment a)
888 /* Returns V's display alignment, which applies only to GUIs. */
890 var_get_alignment (const struct variable *v)
895 /* Sets V's display alignment to ALIGNMENT. */
897 var_set_alignment_quiet (struct variable *v, enum alignment alignment)
899 assert (alignment_is_valid (alignment));
900 v->alignment = alignment;
903 /* Sets V's display alignment to ALIGNMENT. */
905 var_set_alignment (struct variable *v, enum alignment alignment)
907 struct variable *ov = var_clone (v);
908 var_set_alignment_quiet (v, alignment);
909 dict_var_changed (v, VAR_TRAIT_ALIGNMENT, ov);
913 /* Returns the default display alignment for a variable of the
914 given TYPE, as set by var_create. The return value can be
915 used to reset a variable's display alignment to the default. */
917 var_default_alignment (enum val_type type)
919 return type == VAL_NUMERIC ? ALIGN_RIGHT : ALIGN_LEFT;
922 /* Whether variables' values should be preserved from case to
925 /* Returns true if variable V's value should be left from case to
926 case, instead of being reset to system-missing or blanks. */
928 var_get_leave (const struct variable *v)
933 /* Sets V's leave setting to LEAVE. */
935 var_set_leave_quiet (struct variable *v, bool leave)
937 assert (leave || !var_must_leave (v));
942 /* Sets V's leave setting to LEAVE. */
944 var_set_leave (struct variable *v, bool leave)
946 struct variable *ov = var_clone (v);
947 var_set_leave_quiet (v, leave);
948 dict_var_changed (v, VAR_TRAIT_LEAVE, ov);
952 /* Returns true if V must be left from case to case,
953 false if it can be set either way. */
955 var_must_leave (const struct variable *v)
957 return var_get_dict_class (v) == DC_SCRATCH;
960 /* Returns the number of short names stored in VAR.
962 Short names are used only for system and portable file input
963 and output. They are upper-case only, not necessarily unique,
964 and limited to SHORT_NAME_LEN characters (plus a null
965 terminator). Ordinarily a variable has at most one short
966 name, but very long string variables (longer than 255 bytes)
967 may have more. A variable might not have any short name at
968 all if it hasn't been saved to or read from a system or
971 var_get_short_name_cnt (const struct variable *var)
973 return var->short_name_cnt;
976 /* Returns VAR's short name with the given IDX, if it has one
977 with that index, or a null pointer otherwise. Short names may
978 be sparse: even if IDX is less than the number of short names
979 in VAR, this function may return a null pointer. */
981 var_get_short_name (const struct variable *var, size_t idx)
983 return idx < var->short_name_cnt ? var->short_names[idx] : NULL;
986 /* Sets VAR's short name with the given IDX to the UTF-8 string SHORT_NAME.
987 The caller must already have checked that, in the dictionary encoding,
988 SHORT_NAME is no more than SHORT_NAME_LEN bytes long. The new short name
989 will be converted to uppercase.
991 Specifying a null pointer for SHORT_NAME clears the specified short name. */
993 var_set_short_name (struct variable *var, size_t idx, const char *short_name)
995 struct variable *ov = var_clone (var);
997 assert (short_name == NULL || id_is_plausible (short_name, false));
999 /* Clear old short name numbered IDX, if any. */
1000 if (idx < var->short_name_cnt)
1002 free (var->short_names[idx]);
1003 var->short_names[idx] = NULL;
1006 /* Install new short name for IDX. */
1007 if (short_name != NULL)
1009 if (idx >= var->short_name_cnt)
1011 size_t old_cnt = var->short_name_cnt;
1013 var->short_name_cnt = MAX (idx * 2, 1);
1014 var->short_names = xnrealloc (var->short_names, var->short_name_cnt,
1015 sizeof *var->short_names);
1016 for (i = old_cnt; i < var->short_name_cnt; i++)
1017 var->short_names[i] = NULL;
1019 var->short_names[idx] = utf8_to_upper (short_name);
1022 dict_var_changed (var, VAR_TRAIT_NAME, ov);
1025 /* Clears V's short names. */
1027 var_clear_short_names (struct variable *v)
1031 for (i = 0; i < v->short_name_cnt; i++)
1032 free (v->short_names[i]);
1033 free (v->short_names);
1034 v->short_names = NULL;
1035 v->short_name_cnt = 0;
1038 /* Relationship with dictionary. */
1040 /* Returns V's index within its dictionary, the value
1041 for which "dict_get_var (dict, index)" will return V.
1042 V must be in a dictionary. */
1044 var_get_dict_index (const struct variable *v)
1046 assert (var_has_vardict (v));
1047 return vardict_get_dict_index (v->vardict);
1050 /* Returns V's index within the case represented by its
1051 dictionary, that is, the value for which "case_data_idx (case,
1052 index)" will return the data for V in that case.
1053 V must be in a dictionary. */
1055 var_get_case_index (const struct variable *v)
1057 assert (var_has_vardict (v));
1058 return vardict_get_case_index (v->vardict);
1061 /* Returns variable V's attribute set. The caller may examine or
1062 modify the attribute set, but must not destroy it. Destroying
1063 V, or calling var_set_attributes() on V, will also destroy its
1066 var_get_attributes (const struct variable *v)
1068 return CONST_CAST (struct attrset *, &v->attributes);
1071 /* Replaces variable V's attributes set by a copy of ATTRS. */
1073 var_set_attributes_quiet (struct variable *v, const struct attrset *attrs)
1075 attrset_destroy (&v->attributes);
1076 attrset_clone (&v->attributes, attrs);
1079 /* Replaces variable V's attributes set by a copy of ATTRS. */
1081 var_set_attributes (struct variable *v, const struct attrset *attrs)
1083 struct variable *ov = var_clone (v);
1084 var_set_attributes_quiet (v, attrs);
1085 dict_var_changed (v, VAR_TRAIT_ATTRIBUTES, ov);
1089 /* Returns true if V has any custom attributes, false if it has none. */
1091 var_has_attributes (const struct variable *v)
1093 return attrset_count (&v->attributes) > 0;
1097 /* Creates and returns a clone of OLD_VAR. Most properties of
1098 the new variable are copied from OLD_VAR, except:
1100 - The variable's short name is not copied, because there is
1101 no reason to give a new variable with potentially a new
1102 name the same short name.
1104 - The new variable is not added to OLD_VAR's dictionary by
1105 default. Use dict_clone_var, instead, to do that.
1108 var_clone (const struct variable *old_var)
1110 struct variable *new_var = var_create (var_get_name (old_var),
1111 var_get_width (old_var));
1113 var_set_missing_values_quiet (new_var, var_get_missing_values (old_var));
1114 var_set_print_format_quiet (new_var, var_get_print_format (old_var));
1115 var_set_write_format_quiet (new_var, var_get_write_format (old_var));
1116 var_set_value_labels_quiet (new_var, var_get_value_labels (old_var));
1117 var_set_label_quiet (new_var, var_get_label (old_var), false);
1118 var_set_measure_quiet (new_var, var_get_measure (old_var));
1119 var_set_display_width_quiet (new_var, var_get_display_width (old_var));
1120 var_set_alignment_quiet (new_var, var_get_alignment (old_var));
1121 var_set_leave_quiet (new_var, var_get_leave (old_var));
1122 var_set_attributes_quiet (new_var, var_get_attributes (old_var));
1129 /* Returns the encoding of values of variable VAR. (This is actually a
1130 property of the dictionary.) Returns null if no specific encoding has been
1133 var_get_encoding (const struct variable *var)
1135 return (var_has_vardict (var)
1136 ? dict_get_encoding (vardict_get_dictionary (var->vardict))
1140 /* Returns V's vardict structure. */
1141 struct vardict_info *
1142 var_get_vardict (const struct variable *v)
1144 return CONST_CAST (struct vardict_info *, v->vardict);
1147 /* Sets V's vardict data to VARDICT. */
1149 var_set_vardict (struct variable *v, struct vardict_info *vardict)
1151 v->vardict = vardict;
1154 /* Returns true if V has vardict data. */
1156 var_has_vardict (const struct variable *v)
1158 return v->vardict != NULL;
1161 /* Clears V's vardict data. */
1163 var_clear_vardict (struct variable *v)