+ 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)
+{
+ const int old_width = v->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;
+
+ {
+ const int old_val_count = value_cnt_from_width (old_width);
+ const int new_val_count = value_cnt_from_width (new_width);
+
+ if ( old_val_count != new_val_count)
+ dict_var_resized (v, new_val_count - old_val_count);
+ }
+
+ 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 value_cnt_from_width (v->width);
+}
+\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 in the given CLASS of missing values
+ in V, false otherwise. */
+bool
+var_is_value_missing (const struct variable *v, const union value *value,
+ enum mv_class class)
+{
+ return mv_is_value_missing (&v->miss, value, class);
+}
+
+/* Returns true if D is in the given CLASS of missing values in
+ V, false otherwise.
+ V must be a numeric variable. */
+bool
+var_is_num_missing (const struct variable *v, double d, enum mv_class class)
+{
+ return mv_is_num_missing (&v->miss, d, class);
+}
+
+/* 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[],
+ enum mv_class class)
+{
+ return mv_is_str_missing (&v->miss, s, class);
+}
+\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;