+ return (width == 0
+ ? fmt_for_output (FMT_F, 8, 2)
+ : fmt_for_output (FMT_A, width, 0));
+}
+\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 UTF-8 encoded string LABEL, stripping off leading
+ and trailing white space. 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.
+
+ Variable labels are limited to 255 bytes in the dictionary encoding, which
+ should be specified as DICT_ENCODING. If LABEL fits within this limit, this
+ function returns true. Otherwise, the variable label is set to a truncated
+ value, this function returns false and, if ISSUE_WARNING is true, issues a
+ warning. */
+bool
+var_set_label (struct variable *v, const char *label,
+ const char *dict_encoding, bool issue_warning)
+{
+ bool truncated = false;
+
+ free (v->label);
+ v->label = NULL;
+
+ if (label != NULL)
+ {
+ struct substring s = ss_cstr (label);
+ size_t trunc_len;
+
+ if (dict_encoding != NULL)
+ {
+ enum { MAX_LABEL_LEN = 255 };
+
+ trunc_len = utf8_encoding_trunc_len (label, dict_encoding,
+ MAX_LABEL_LEN);
+ if (ss_length (s) > trunc_len)
+ {
+ if (issue_warning)
+ msg (SW, _("Truncating variable label for variable `%s' to %d "
+ "bytes."), var_get_name (v), MAX_LABEL_LEN);
+ ss_truncate (&s, trunc_len);
+ truncated = true;
+ }
+ }
+
+ ss_trim (&s, ss_cstr (CC_SPACES));
+ if (!ss_is_empty (s))
+ v->label = ss_xstrdup (s);
+ }
+
+ dict_var_changed (v);