+ return hsh_hash_case_string (var_get_name (*v));
+}
+\f
+/* Returns the type of variable V. */
+enum var_type
+var_get_type (const struct variable *v)
+{
+ return var_type_from_width (v->width);
+}
+
+/* Returns the width of variable V. */
+int
+var_get_width (const struct variable *v)
+{
+ return v->width;
+}
+
+/* Sets the width of V to WIDTH. */
+void
+var_set_width (struct variable *v, int new_width)
+{
+ enum var_type new_type = var_type_from_width (new_width);
+
+ if (mv_is_resizable (&v->miss, new_width))
+ mv_resize (&v->miss, new_width);
+ else
+ mv_init (&v->miss, new_width);
+
+ if (v->val_labs != NULL)
+ {
+ if (val_labs_can_set_width (v->val_labs, new_width))
+ val_labs_set_width (v->val_labs, new_width);
+ else
+ {
+ val_labs_destroy (v->val_labs);
+ v->val_labs = NULL;
+ }
+ }
+
+ if (var_get_type (v) != new_type)
+ {
+ v->print = (new_type == VAR_NUMERIC
+ ? fmt_for_output (FMT_F, 8, 2)
+ : fmt_for_output (FMT_A, new_width, 0));
+ v->write = v->print;
+ }
+ else if (new_type == VAR_STRING)
+ {
+ v->print.w = v->print.type == FMT_AHEX ? new_width * 2 : new_width;
+ v->write.w = v->write.type == FMT_AHEX ? new_width * 2 : new_width;
+ }
+
+ v->width = new_width;
+
+ dict_var_changed (v);
+}
+
+/* Returns true if variable V is numeric, false otherwise. */
+bool
+var_is_numeric (const struct variable *v)
+{
+ return var_get_type (v) == VAR_NUMERIC;
+}
+
+/* Returns true if variable V is a string variable, false
+ otherwise. */
+bool
+var_is_alpha (const struct variable *v)
+{
+ return var_get_type (v) == VAR_STRING;
+}
+
+/* Returns true if variable V is a short string variable, false
+ otherwise. */
+bool
+var_is_short_string (const struct variable *v)
+{
+ return v->width > 0 && v->width <= MAX_SHORT_STRING;
+}
+
+/* Returns true if variable V is a long string variable, false
+ otherwise. */
+bool
+var_is_long_string (const struct variable *v)
+{
+ return v->width > MAX_SHORT_STRING;
+}
+
+/* Returns the number of "union value"s need to store a value of
+ variable V. */
+size_t
+var_get_value_cnt (const struct variable *v)
+{
+ return v->width == 0 ? 1 : DIV_RND_UP (v->width, MAX_SHORT_STRING);
+}
+\f
+/* Returns variable V's missing values. */
+const struct missing_values *
+var_get_missing_values (const struct variable *v)
+{
+ return &v->miss;
+}
+
+/* Sets variable V's missing values to MISS, which must be of V's
+ width or at least resizable to V's width.
+ If MISS is null, then V's missing values, if any, are
+ cleared. */
+void
+var_set_missing_values (struct variable *v, const struct missing_values *miss)
+{
+ if (miss != NULL)
+ {
+ assert (mv_is_resizable (miss, v->width));
+ mv_copy (&v->miss, miss);
+ mv_resize (&v->miss, v->width);
+ }
+ else
+ mv_init (&v->miss, v->width);
+
+ dict_var_changed (v);
+}
+
+/* Sets variable V to have no user-missing values. */
+void
+var_clear_missing_values (struct variable *v)
+{
+ var_set_missing_values (v, NULL);
+}
+
+/* Returns true if V has any user-missing values,
+ false otherwise. */
+bool
+var_has_missing_values (const struct variable *v)
+{
+ return !mv_is_empty (&v->miss);
+}
+
+/* Returns true if VALUE is system missing or user-missing value
+ for V, false otherwise. */
+bool
+var_is_value_missing (const struct variable *v, const union value *value)
+{
+ return mv_is_value_missing (&v->miss, value);
+}
+
+/* Returns true if D is system missing or a missing value in V,
+ false otherwise.
+ V must be a numeric variable. */
+bool
+var_is_num_missing (const struct variable *v, double d)
+{
+ return mv_is_num_missing (&v->miss, d);
+}
+
+/* Returns true if S[] is a missing value for V, false otherwise.
+ S[] must contain exactly as many characters as V's width.
+ V must be a string variable. */
+bool
+var_is_str_missing (const struct variable *v, const char s[])
+{
+ return mv_is_str_missing (&v->miss, s);
+}
+
+/* Returns true if VALUE is a missing value for V, false
+ otherwise. */
+bool
+var_is_value_user_missing (const struct variable *v, const union value *value)
+{
+ return mv_is_value_user_missing (&v->miss, value);
+}
+
+/* Returns true if D is a user-missing value for V, false
+ otherwise. V must be a numeric variable. */
+bool
+var_is_num_user_missing (const struct variable *v, double d)
+{
+ return mv_is_num_user_missing (&v->miss, d);
+}
+
+/* Returns true if S[] is a missing value for V, false otherwise.
+ V must be a string variable.
+ S[] must contain exactly as many characters as V's width. */
+bool
+var_is_str_user_missing (const struct variable *v, const char s[])
+{
+ return mv_is_str_user_missing (&v->miss, s);
+}
+
+/* Returns true if V is a numeric variable and VALUE is the
+ system missing value. */
+bool
+var_is_value_system_missing (const struct variable *v,
+ const union value *value)
+{
+ return mv_is_value_system_missing (&v->miss, value);
+}
+\f
+/* Returns variable V's value labels,
+ possibly a null pointer if it has none. */
+const struct val_labs *
+var_get_value_labels (const struct variable *v)
+{
+ return v->val_labs;
+}
+
+/* Returns true if variable V has at least one value label. */
+bool
+var_has_value_labels (const struct variable *v)
+{
+ return val_labs_count (v->val_labs) > 0;
+}
+
+/* Sets variable V's value labels to a copy of VLS,
+ which must have a width equal to V's width or one that can be
+ changed to V's width.
+ If VLS is null, then V's value labels, if any, are removed. */
+void
+var_set_value_labels (struct variable *v, const struct val_labs *vls)
+{
+ val_labs_destroy (v->val_labs);
+ v->val_labs = NULL;
+
+ if (vls != NULL)
+ {
+ assert (val_labs_can_set_width (vls, v->width));
+ v->val_labs = val_labs_copy (vls);
+ val_labs_set_width (v->val_labs, v->width);
+ dict_var_changed (v);
+ }
+}
+
+/* Makes sure that V has a set of value labels,
+ by assigning one to it if necessary. */
+static void
+alloc_value_labels (struct variable *v)
+{
+ assert (!var_is_long_string (v));
+ if (v->val_labs == NULL)
+ v->val_labs = val_labs_create (v->width);
+}
+
+/* Attempts to add a value label with the given VALUE and LABEL
+ to V. Returns true if successful, false if VALUE has an
+ existing label.
+ V must not be a long string variable. */
+bool
+var_add_value_label (struct variable *v,
+ const union value *value, const char *label)
+{
+ alloc_value_labels (v);
+ return val_labs_add (v->val_labs, *value, label);
+}
+
+/* Adds or replaces a value label with the given VALUE and LABEL
+ to V.
+ V must not be a long string variable. */
+void
+var_replace_value_label (struct variable *v,
+ const union value *value, const char *label)
+{
+ alloc_value_labels (v);
+ val_labs_replace (v->val_labs, *value, label);
+}
+
+/* Removes V's value labels, if any. */
+void
+var_clear_value_labels (struct variable *v)
+{
+ var_set_value_labels (v, NULL);
+}
+
+/* Returns the label associated with VALUE for variable V,
+ or a null pointer if none. */
+const char *
+var_lookup_value_label (const struct variable *v, const union value *value)
+{
+ return val_labs_find (v->val_labs, *value);
+}
+
+/* Get a string representing VALUE for variable V.
+ That is, if VALUE has a label, return that label,
+ otherwise format VALUE and return the formatted string. */
+const char *
+var_get_value_name (const struct variable *v, const union value *value)
+{
+ const char *name = var_lookup_value_label (v, value);
+ if (name == NULL)
+ {
+ static char buf[MAX_STRING + 1];
+ data_out (value, &v->print, buf);
+ buf[v->print.w] = '\0';
+ name = buf;
+ }
+ return name;
+}
+\f
+/* Print and write formats. */
+
+/* Returns V's print format specification. */
+const struct fmt_spec *
+var_get_print_format (const struct variable *v)
+{
+ return &v->print;
+}
+
+/* Sets V's print format specification to PRINT, which must be a
+ valid format specification for outputting a variable of V's
+ width. */
+void
+var_set_print_format (struct variable *v, const struct fmt_spec *print)
+{
+ assert (fmt_check_width_compat (print, v->width));
+ v->print = *print;
+ dict_var_changed (v);
+}
+
+/* Returns V's write format specification. */
+const struct fmt_spec *
+var_get_write_format (const struct variable *v)
+{
+ return &v->write;
+}
+
+/* Sets V's write format specification to WRITE, which must be a
+ valid format specification for outputting a variable of V's
+ width. */
+void
+var_set_write_format (struct variable *v, const struct fmt_spec *write)
+{
+ assert (fmt_check_width_compat (write, v->width));
+ v->write = *write;
+ dict_var_changed (v);
+}
+
+/* Sets V's print and write format specifications to FORMAT,
+ which must be a valid format specification for outputting a
+ variable of V's width. */
+void
+var_set_both_formats (struct variable *v, const struct fmt_spec *format)
+{
+ var_set_print_format (v, format);
+ var_set_write_format (v, format);
+}
+\f
+/* Return a string representing this variable, in the form most
+ appropriate from a human factors perspective, that is, its
+ variable label if it has one, otherwise its name. */
+const char *
+var_to_string (const struct variable *v)
+{
+ return v->label != NULL ? v->label : v->name;
+}
+
+/* Returns V's variable label, or a null pointer if it has none. */
+const char *
+var_get_label (const struct variable *v)
+{
+ return v->label;
+}
+
+/* Sets V's variable label to LABEL, stripping off leading and
+ trailing white space and truncating to 255 characters.
+ If LABEL is a null pointer or if LABEL is an empty string
+ (after stripping white space), then V's variable label (if
+ any) is removed. */
+void
+var_set_label (struct variable *v, const char *label)
+{
+ free (v->label);
+ v->label = NULL;
+
+ if (label != NULL)
+ {
+ struct substring s = ss_cstr (label);
+ ss_trim (&s, ss_cstr (CC_SPACES));
+ ss_truncate (&s, 255);
+ if (!ss_is_empty (s))
+ v->label = ss_xstrdup (s);
+ dict_var_changed (v);
+ }
+}
+
+/* Removes any variable label from V. */
+void
+var_clear_label (struct variable *v)
+{
+ var_set_label (v, NULL);
+}
+
+/* Returns true if V has a variable V,
+ false otherwise. */
+bool
+var_has_label (const struct variable *v)
+{
+ return v->label != NULL;
+}
+\f
+/* Returns true if M is a valid variable measurement level,
+ false otherwise. */
+bool
+measure_is_valid (enum measure m)
+{
+ return m == MEASURE_NOMINAL || m == MEASURE_ORDINAL || m == MEASURE_SCALE;
+}
+
+/* Returns V's measurement level. */
+enum measure
+var_get_measure (const struct variable *v)
+{
+ return v->measure;
+}
+
+/* Sets V's measurement level to MEASURE. */
+void
+var_set_measure (struct variable *v, enum measure measure)
+{
+ assert (measure_is_valid (measure));
+ v->measure = measure;
+ dict_var_changed (v);
+}
+\f
+/* Returns V's display width, which applies only to GUIs. */
+int
+var_get_display_width (const struct variable *v)
+{
+ return v->display_width;
+}
+
+
+
+
+/* Sets V's display width to DISPLAY_WIDTH. */
+void
+var_set_display_width (struct variable *v, int display_width)
+{
+ v->display_width = display_width;
+ dict_var_changed (v);
+}
+\f
+/* Returns true if A is a valid alignment,
+ false otherwise. */
+bool
+alignment_is_valid (enum alignment a)
+{
+ return a == ALIGN_LEFT || a == ALIGN_RIGHT || a == ALIGN_CENTRE;
+}
+
+/* Returns V's display alignment, which applies only to GUIs. */
+enum alignment
+var_get_alignment (const struct variable *v)
+{
+ return v->alignment;
+}
+
+/* Sets V's display alignment to ALIGNMENT. */
+void
+var_set_alignment (struct variable *v, enum alignment alignment)
+{
+ assert (alignment_is_valid (alignment));
+ v->alignment = alignment;
+ dict_var_changed (v);