+/* 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);
+}
+
+/* Returns the default display width for a variable of the given
+ WIDTH, as set by var_create. The return value can be used to
+ reset a variable's display width to the default. */
+int
+var_default_display_width (int width)
+{
+ return width == 0 ? 8 : MIN (width, 32);
+}
+\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);
+}
+\f
+/* Whether variables' values should be preserved from case to
+ case. */
+
+/* Returns true if variable V's value should be left from case to
+ case, instead of being reset to 0, system-missing, or blanks. */
+bool
+var_get_leave (const struct variable *v)
+{
+ return v->leave;
+}
+
+/* Sets V's leave setting to LEAVE. */
+void
+var_set_leave (struct variable *v, bool leave)
+{
+ assert (leave || !var_must_leave (v));
+ v->leave = leave;
+ dict_var_changed (v);