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 /* Each command may use these fields as needed. */
77 void (*aux_dtor) (struct variable *);
79 /* Custom attributes. */
80 struct attrset attributes;
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 (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 /* Creates and returns a clone of OLD_VAR. Most properties of
112 the new variable are copied from OLD_VAR, except:
114 - The variable's short name is not copied, because there is
115 no reason to give a new variable with potentially a new
116 name the same short name.
118 - The new variable is not added to OLD_VAR's dictionary by
119 default. Use dict_clone_var, instead, to do that.
122 var_clone (const struct variable *old_var)
124 struct variable *new_var = var_create (var_get_name (old_var),
125 var_get_width (old_var));
127 var_set_missing_values (new_var, var_get_missing_values (old_var));
128 var_set_print_format (new_var, var_get_print_format (old_var));
129 var_set_write_format (new_var, var_get_write_format (old_var));
130 var_set_value_labels (new_var, var_get_value_labels (old_var));
131 var_set_label (new_var, var_get_label (old_var), false);
132 var_set_measure (new_var, var_get_measure (old_var));
133 var_set_display_width (new_var, var_get_display_width (old_var));
134 var_set_alignment (new_var, var_get_alignment (old_var));
135 var_set_leave (new_var, var_get_leave (old_var));
136 var_set_attributes (new_var, var_get_attributes (old_var));
141 /* Destroys variable V.
142 V must not belong to a dictionary. If it does, use
143 dict_delete_var instead. */
145 var_destroy (struct variable *v)
149 assert (!var_has_vardict (v));
150 mv_destroy (&v->miss);
151 var_clear_short_names (v);
153 val_labs_destroy (v->val_labs);
155 attrset_destroy (var_get_attributes (v));
157 ds_destroy (&v->name_and_label);
162 /* Variable names. */
164 /* Return variable V's name, as a UTF-8 encoded string. */
166 var_get_name (const struct variable *v)
173 /* Sets V's name to NAME, a UTF-8 encoded string.
174 Do not use this function for a variable in a dictionary. Use
175 dict_rename_var instead. */
177 var_set_name (struct variable *v, const char *name)
179 assert (!var_has_vardict (v));
180 assert (id_is_plausible (name, false));
183 v->name = xstrdup (name);
184 ds_destroy (&v->name_and_label);
185 ds_init_empty (&v->name_and_label);
186 dict_var_changed (v);
189 /* Returns VAR's dictionary class. */
191 var_get_dict_class (const struct variable *var)
193 return dict_class_from_id (var->name);
196 /* A hsh_compare_func that orders variables A and B by their
199 compare_vars_by_name (const void *a_, const void *b_, const void *aux UNUSED)
201 const struct variable *a = a_;
202 const struct variable *b = b_;
204 return utf8_strcasecmp (a->name, b->name);
207 /* A hsh_hash_func that hashes variable V based on its name. */
209 hash_var_by_name (const void *v_, const void *aux UNUSED)
211 const struct variable *v = v_;
213 return utf8_hash_case_string (v->name, 0);
216 /* A hsh_compare_func that orders pointers to variables A and B
219 compare_var_ptrs_by_name (const void *a_, const void *b_,
220 const void *aux UNUSED)
222 struct variable *const *a = a_;
223 struct variable *const *b = b_;
225 return utf8_strcasecmp (var_get_name (*a), var_get_name (*b));
228 /* A hsh_compare_func that orders pointers to variables A and B
229 by their dictionary indexes. */
231 compare_var_ptrs_by_dict_index (const void *a_, const void *b_,
232 const void *aux UNUSED)
234 struct variable *const *a = a_;
235 struct variable *const *b = b_;
236 size_t a_index = var_get_dict_index (*a);
237 size_t b_index = var_get_dict_index (*b);
239 return a_index < b_index ? -1 : a_index > b_index;
242 /* A hsh_hash_func that hashes pointer to variable V based on its
245 hash_var_ptr_by_name (const void *v_, const void *aux UNUSED)
247 struct variable *const *v = v_;
249 return utf8_hash_case_string (var_get_name (*v), 0);
252 /* Returns the type of variable V. */
254 var_get_type (const struct variable *v)
256 return val_type_from_width (v->width);
259 /* Returns the width of variable V. */
261 var_get_width (const struct variable *v)
266 /* Changes the width of V to NEW_WIDTH.
267 This function should be used cautiously. */
269 var_set_width (struct variable *v, int new_width)
271 const int old_width = v->width;
273 if (old_width == new_width)
276 if (mv_is_resizable (&v->miss, new_width))
277 mv_resize (&v->miss, new_width);
280 mv_destroy (&v->miss);
281 mv_init (&v->miss, new_width);
284 if (v->val_labs != NULL)
286 if (val_labs_can_set_width (v->val_labs, new_width))
287 val_labs_set_width (v->val_labs, new_width);
290 val_labs_destroy (v->val_labs);
295 fmt_resize (&v->print, new_width);
296 fmt_resize (&v->write, new_width);
298 v->width = new_width;
299 dict_var_resized (v, old_width);
300 dict_var_changed (v);
303 /* Returns true if variable V is numeric, false otherwise. */
305 var_is_numeric (const struct variable *v)
307 return var_get_type (v) == VAL_NUMERIC;
310 /* Returns true if variable V is a string variable, false
313 var_is_alpha (const struct variable *v)
315 return var_get_type (v) == VAL_STRING;
318 /* Returns variable V's missing values. */
319 const struct missing_values *
320 var_get_missing_values (const struct variable *v)
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)
334 assert (mv_is_resizable (miss, v->width));
335 mv_destroy (&v->miss);
336 mv_copy (&v->miss, miss);
337 mv_resize (&v->miss, v->width);
342 dict_var_changed (v);
345 /* Sets variable V to have no user-missing values. */
347 var_clear_missing_values (struct variable *v)
349 var_set_missing_values (v, NULL);
352 /* Returns true if V has any user-missing values,
355 var_has_missing_values (const struct variable *v)
357 return !mv_is_empty (&v->miss);
360 /* Returns true if VALUE is in the given CLASS of missing values
361 in V, false otherwise. */
363 var_is_value_missing (const struct variable *v, const union value *value,
366 return mv_is_value_missing (&v->miss, value, class);
369 /* Returns true if D is in the given CLASS of missing values in
371 V must be a numeric variable. */
373 var_is_num_missing (const struct variable *v, double d, enum mv_class class)
375 return mv_is_num_missing (&v->miss, d, class);
378 /* Returns true if S[] is a missing value for V, false otherwise.
379 S[] must contain exactly as many characters as V's width.
380 V must be a string variable. */
382 var_is_str_missing (const struct variable *v, const uint8_t s[],
385 return mv_is_str_missing (&v->miss, s, class);
388 /* Returns variable V's value labels,
389 possibly a null pointer if it has none. */
390 const struct val_labs *
391 var_get_value_labels (const struct variable *v)
396 /* Returns true if variable V has at least one value label. */
398 var_has_value_labels (const struct variable *v)
400 return val_labs_count (v->val_labs) > 0;
403 /* Sets variable V's value labels to a copy of VLS,
404 which must have a width equal to V's width or one that can be
405 changed to V's width.
406 If VLS is null, then V's value labels, if any, are removed. */
408 var_set_value_labels (struct variable *v, const struct val_labs *vls)
410 val_labs_destroy (v->val_labs);
415 assert (val_labs_can_set_width (vls, v->width));
416 v->val_labs = val_labs_clone (vls);
417 val_labs_set_width (v->val_labs, v->width);
418 dict_var_changed (v);
422 /* Makes sure that V has a set of value labels,
423 by assigning one to it if necessary. */
425 alloc_value_labels (struct variable *v)
427 if (v->val_labs == NULL)
428 v->val_labs = val_labs_create (v->width);
431 /* Attempts to add a value label with the given VALUE and UTF-8 encoded LABEL
432 to V. Returns true if successful, false otherwise (probably due to an
435 In LABEL, the two-byte sequence "\\n" is interpreted as a new-line. */
437 var_add_value_label (struct variable *v,
438 const union value *value, const char *label)
440 alloc_value_labels (v);
441 return val_labs_add (v->val_labs, value, label);
444 /* Adds or replaces a value label with the given VALUE and UTF-8 encoded LABEL
447 In LABEL, the two-byte sequence "\\n" is interpreted as a new-line. */
449 var_replace_value_label (struct variable *v,
450 const union value *value, const char *label)
452 alloc_value_labels (v);
453 val_labs_replace (v->val_labs, value, label);
456 /* Removes V's value labels, if any. */
458 var_clear_value_labels (struct variable *v)
460 var_set_value_labels (v, NULL);
463 /* Returns the label associated with VALUE for variable V, as a UTF-8 string in
464 a format suitable for output, or a null pointer if none. */
466 var_lookup_value_label (const struct variable *v, const union value *value)
468 return val_labs_find (v->val_labs, value);
472 Append to STR the string representation of VALUE for variable V.
473 STR must be a pointer to an initialised struct string.
476 append_value (const struct variable *v, const union value *value,
479 char *s = data_out (value, var_get_encoding (v), &v->print);
480 ds_put_cstr (str, s);
484 /* Append STR with a string representing VALUE for variable V.
485 That is, if VALUE has a label, append that label,
486 otherwise format VALUE and append the formatted string.
487 STR must be a pointer to an initialised struct string.
490 var_append_value_name (const struct variable *v, const union value *value,
493 enum settings_value_style style = settings_get_value_style ();
494 const char *name = var_lookup_value_label (v, value);
498 case SETTINGS_VAL_STYLE_VALUES:
499 append_value (v, value, str);
502 case SETTINGS_VAL_STYLE_LABELS:
504 append_value (v, value, str);
506 ds_put_cstr (str, name);
509 case SETTINGS_VAL_STYLE_BOTH:
511 append_value (v, value, str);
514 ds_put_cstr (str, " (");
515 ds_put_cstr (str, name);
516 ds_put_cstr (str, ")");
522 /* Print and write formats. */
524 /* Returns V's print format specification. */
525 const struct fmt_spec *
526 var_get_print_format (const struct variable *v)
531 /* Sets V's print format specification to PRINT, which must be a
532 valid format specification for a variable of V's width
533 (ordinarily an output format, but input formats are not
536 var_set_print_format (struct variable *v, const struct fmt_spec *print)
538 if (!fmt_equal (&v->print, print))
540 assert (fmt_check_width_compat (print, v->width));
542 dict_var_changed (v);
546 /* Returns V's write format specification. */
547 const struct fmt_spec *
548 var_get_write_format (const struct variable *v)
553 /* Sets V's write format specification to WRITE, which must be a
554 valid format specification for a variable of V's width
555 (ordinarily an output format, but input formats are not
558 var_set_write_format (struct variable *v, const struct fmt_spec *write)
560 if (!fmt_equal (&v->write, write))
562 assert (fmt_check_width_compat (write, v->width));
564 dict_var_changed (v);
568 /* Sets V's print and write format specifications to FORMAT,
569 which must be a valid format specification for a variable of
570 V's width (ordinarily an output format, but input formats are
573 var_set_both_formats (struct variable *v, const struct fmt_spec *format)
575 var_set_print_format (v, format);
576 var_set_write_format (v, format);
579 /* Returns the default print and write format for a variable of
580 the given TYPE, as set by var_create. The return value can be
581 used to reset a variable's print and write formats to the
584 var_default_formats (int width)
587 ? fmt_for_output (FMT_F, 8, 2)
588 : fmt_for_output (FMT_A, width, 0));
594 /* Update the combined name and label string if necessary */
596 update_vl_string (const struct variable *v)
598 /* Cast away const! */
599 struct string *str = (struct string *) &v->name_and_label;
601 if (ds_is_empty (str))
604 ds_put_format (str, _("%s (%s)"), v->label, v->name);
606 ds_put_cstr (str, v->name);
611 /* Return a string representing this variable, in the form most
612 appropriate from a human factors perspective, that is, its
613 variable label if it has one, otherwise its name. */
615 var_to_string (const struct variable *v)
617 enum settings_var_style style = settings_get_var_style ();
621 case SETTINGS_VAR_STYLE_NAMES:
624 case SETTINGS_VAR_STYLE_LABELS:
625 return v->label != NULL ? v->label : v->name;
627 case SETTINGS_VAR_STYLE_BOTH:
628 update_vl_string (v);
629 return ds_cstr (&v->name_and_label);
637 /* Returns V's variable label, or a null pointer if it has none. */
639 var_get_label (const struct variable *v)
644 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
645 and trailing white space. If LABEL is a null pointer or if LABEL is an
646 empty string (after stripping white space), then V's variable label (if any)
649 Variable labels are limited to 255 bytes in V's encoding (as returned by
650 var_get_encoding()). If LABEL fits within this limit, this function returns
651 true. Otherwise, the variable label is set to a truncated value, this
652 function returns false and, if ISSUE_WARNING is true, issues a warning. */
654 var_set_label (struct variable *v, const char *label, bool issue_warning)
656 bool truncated = false;
661 if (label != NULL && label[strspn (label, CC_SPACES)])
663 const char *dict_encoding = var_get_encoding (v);
664 struct substring s = ss_cstr (label);
667 if (dict_encoding != NULL)
669 enum { MAX_LABEL_LEN = 255 };
671 trunc_len = utf8_encoding_trunc_len (label, dict_encoding,
673 if (ss_length (s) > trunc_len)
676 msg (SW, _("Truncating variable label for variable `%s' to %d "
677 "bytes."), var_get_name (v), MAX_LABEL_LEN);
678 ss_truncate (&s, trunc_len);
683 v->label = ss_xstrdup (s);
686 ds_destroy (&v->name_and_label);
687 ds_init_empty (&v->name_and_label);
689 dict_var_changed (v);
694 /* Removes any variable label from V. */
696 var_clear_label (struct variable *v)
698 var_set_label (v, NULL, false);
701 /* Returns true if V has a variable V,
704 var_has_label (const struct variable *v)
706 return v->label != NULL;
709 /* Returns true if M is a valid variable measurement level,
712 measure_is_valid (enum measure m)
714 return m == MEASURE_NOMINAL || m == MEASURE_ORDINAL || m == MEASURE_SCALE;
717 /* Returns a string version of measurement level M, for display to a user. */
719 measure_to_string (enum measure m)
723 case MEASURE_NOMINAL:
726 case MEASURE_ORDINAL:
737 /* Returns V's measurement level. */
739 var_get_measure (const struct variable *v)
744 /* Sets V's measurement level to MEASURE. */
746 var_set_measure (struct variable *v, enum measure measure)
748 assert (measure_is_valid (measure));
749 v->measure = measure;
750 dict_var_changed (v);
753 /* Returns the default measurement level for a variable of the
754 given TYPE, as set by var_create. The return value can be
755 used to reset a variable's measurement level to the
758 var_default_measure (enum val_type type)
760 return type == VAL_NUMERIC ? MEASURE_SCALE : MEASURE_NOMINAL;
763 /* Returns V's display width, which applies only to GUIs. */
765 var_get_display_width (const struct variable *v)
767 return v->display_width;
770 /* Sets V's display width to DISPLAY_WIDTH. */
772 var_set_display_width (struct variable *v, int new_width)
774 if (v->display_width != new_width)
776 v->display_width = new_width;
777 dict_var_display_width_changed (v);
778 dict_var_changed (v);
782 /* Returns the default display width for a variable of the given
783 WIDTH, as set by var_create. The return value can be used to
784 reset a variable's display width to the default. */
786 var_default_display_width (int width)
788 return width == 0 ? 8 : MIN (width, 32);
791 /* Returns true if A is a valid alignment,
794 alignment_is_valid (enum alignment a)
796 return a == ALIGN_LEFT || a == ALIGN_RIGHT || a == ALIGN_CENTRE;
799 /* Returns a string version of alignment A, for display to a user. */
801 alignment_to_string (enum alignment a)
819 /* Returns V's display alignment, which applies only to GUIs. */
821 var_get_alignment (const struct variable *v)
826 /* Sets V's display alignment to ALIGNMENT. */
828 var_set_alignment (struct variable *v, enum alignment alignment)
830 assert (alignment_is_valid (alignment));
831 v->alignment = alignment;
832 dict_var_changed (v);
835 /* Returns the default display alignment for a variable of the
836 given TYPE, as set by var_create. The return value can be
837 used to reset a variable's display alignment to the default. */
839 var_default_alignment (enum val_type type)
841 return type == VAL_NUMERIC ? ALIGN_RIGHT : ALIGN_LEFT;
844 /* Whether variables' values should be preserved from case to
847 /* Returns true if variable V's value should be left from case to
848 case, instead of being reset to system-missing or blanks. */
850 var_get_leave (const struct variable *v)
855 /* Sets V's leave setting to LEAVE. */
857 var_set_leave (struct variable *v, bool leave)
859 assert (leave || !var_must_leave (v));
861 dict_var_changed (v);
864 /* Returns true if V must be left from case to case,
865 false if it can be set either way. */
867 var_must_leave (const struct variable *v)
869 return var_get_dict_class (v) == DC_SCRATCH;
872 /* Returns the number of short names stored in VAR.
874 Short names are used only for system and portable file input
875 and output. They are upper-case only, not necessarily unique,
876 and limited to SHORT_NAME_LEN characters (plus a null
877 terminator). Ordinarily a variable has at most one short
878 name, but very long string variables (longer than 255 bytes)
879 may have more. A variable might not have any short name at
880 all if it hasn't been saved to or read from a system or
883 var_get_short_name_cnt (const struct variable *var)
885 return var->short_name_cnt;
888 /* Returns VAR's short name with the given IDX, if it has one
889 with that index, or a null pointer otherwise. Short names may
890 be sparse: even if IDX is less than the number of short names
891 in VAR, this function may return a null pointer. */
893 var_get_short_name (const struct variable *var, size_t idx)
895 return idx < var->short_name_cnt ? var->short_names[idx] : NULL;
898 /* Sets VAR's short name with the given IDX to the UTF-8 string SHORT_NAME.
899 The caller must already have checked that, in the dictionary encoding,
900 SHORT_NAME is no more than SHORT_NAME_LEN bytes long. The new short name
901 will be converted to uppercase.
903 Specifying a null pointer for SHORT_NAME clears the specified short name. */
905 var_set_short_name (struct variable *var, size_t idx, const char *short_name)
907 assert (short_name == NULL || id_is_plausible (short_name, false));
909 /* Clear old short name numbered IDX, if any. */
910 if (idx < var->short_name_cnt)
912 free (var->short_names[idx]);
913 var->short_names[idx] = NULL;
916 /* Install new short name for IDX. */
917 if (short_name != NULL)
919 if (idx >= var->short_name_cnt)
921 size_t old_cnt = var->short_name_cnt;
923 var->short_name_cnt = MAX (idx * 2, 1);
924 var->short_names = xnrealloc (var->short_names, var->short_name_cnt,
925 sizeof *var->short_names);
926 for (i = old_cnt; i < var->short_name_cnt; i++)
927 var->short_names[i] = NULL;
929 var->short_names[idx] = utf8_to_upper (short_name);
932 dict_var_changed (var);
935 /* Clears V's short names. */
937 var_clear_short_names (struct variable *v)
941 for (i = 0; i < v->short_name_cnt; i++)
942 free (v->short_names[i]);
943 free (v->short_names);
944 v->short_names = NULL;
945 v->short_name_cnt = 0;
948 /* Relationship with dictionary. */
950 /* Returns V's index within its dictionary, the value
951 for which "dict_get_var (dict, index)" will return V.
952 V must be in a dictionary. */
954 var_get_dict_index (const struct variable *v)
956 assert (var_has_vardict (v));
957 return vardict_get_dict_index (v->vardict);
960 /* Returns V's index within the case represented by its
961 dictionary, that is, the value for which "case_data_idx (case,
962 index)" will return the data for V in that case.
963 V must be in a dictionary. */
965 var_get_case_index (const struct variable *v)
967 assert (var_has_vardict (v));
968 return vardict_get_case_index (v->vardict);
971 /* Returns V's auxiliary data, or a null pointer if none has been
974 var_get_aux (const struct variable *v)
979 /* Assign auxiliary data AUX to variable V, which must not
980 already have auxiliary data. Before V's auxiliary data is
981 cleared, AUX_DTOR(V) will be called. (var_dtor_free, below,
982 may be appropriate for use as AUX_DTOR.) */
984 var_attach_aux (const struct variable *v_,
985 void *aux, void (*aux_dtor) (struct variable *))
987 struct variable *v = CONST_CAST (struct variable *, v_);
988 assert (v->aux == NULL);
989 assert (aux != NULL);
991 v->aux_dtor = aux_dtor;
995 /* Remove auxiliary data, if any, from V, and return it, without
996 calling any associated destructor. */
998 var_detach_aux (struct variable *v)
1001 assert (aux != NULL);
1006 /* Clears auxiliary data, if any, from V, and calls any
1007 associated destructor. */
1009 var_clear_aux (struct variable *v)
1013 if (v->aux_dtor != NULL)
1019 /* This function is appropriate for use an auxiliary data
1020 destructor (passed as AUX_DTOR to var_attach_aux()) for the
1021 case where the auxiliary data should be passed to free(). */
1023 var_dtor_free (struct variable *v)
1028 /* Returns variable V's attribute set. The caller may examine or
1029 modify the attribute set, but must not destroy it. Destroying
1030 V, or calling var_set_attributes() on V, will also destroy its
1033 var_get_attributes (const struct variable *v)
1035 return CONST_CAST (struct attrset *, &v->attributes);
1038 /* Replaces variable V's attributes set by a copy of ATTRS. */
1040 var_set_attributes (struct variable *v, const struct attrset *attrs)
1042 attrset_destroy (&v->attributes);
1043 attrset_clone (&v->attributes, attrs);
1046 /* Returns true if V has any custom attributes, false if it has none. */
1048 var_has_attributes (const struct variable *v)
1050 return attrset_count (&v->attributes) > 0;
1053 /* Returns the encoding of values of variable VAR. (This is actually a
1054 property of the dictionary.) Returns null if no specific encoding has been
1057 var_get_encoding (const struct variable *var)
1059 return (var_has_vardict (var)
1060 ? dict_get_encoding (vardict_get_dictionary (var->vardict))
1064 /* Returns V's vardict structure. */
1065 struct vardict_info *
1066 var_get_vardict (const struct variable *v)
1068 return CONST_CAST (struct vardict_info *, v->vardict);
1071 /* Sets V's vardict data to VARDICT. */
1073 var_set_vardict (struct variable *v, struct vardict_info *vardict)
1075 v->vardict = vardict;
1078 /* Returns true if V has vardict data. */
1080 var_has_vardict (const struct variable *v)
1082 return v->vardict != NULL;
1085 /* Clears V's vardict data. */
1087 var_clear_vardict (struct variable *v)