+static void
+write_long_string_value_labels (struct sfm_writer *w,
+ const struct dictionary *dict)
+{
+ size_t n_vars = dict_get_var_cnt (dict);
+ size_t size, i;
+ off_t start UNUSED;
+
+ /* Figure out the size in advance. */
+ size = 0;
+ for (i = 0; i < n_vars; i++)
+ {
+ struct variable *var = dict_get_var (dict, i);
+ const struct val_labs *val_labs = var_get_value_labels (var);
+ const char *encoding = var_get_encoding (var);
+ int width = var_get_width (var);
+ const struct val_lab *val_lab;
+
+ if (val_labs_count (val_labs) == 0 || width < 9)
+ continue;
+
+ size += 12;
+ size += recode_string_len (encoding, "UTF-8", var_get_name (var), -1);
+ for (val_lab = val_labs_first (val_labs); val_lab != NULL;
+ val_lab = val_labs_next (val_labs, val_lab))
+ {
+ size += 8 + width;
+ size += recode_string_len (encoding, "UTF-8",
+ val_lab_get_escaped_label (val_lab), -1);
+ }
+ }
+ if (size == 0)
+ return;
+
+ write_int (w, 7); /* Record type. */
+ write_int (w, 21); /* Record subtype */
+ write_int (w, 1); /* Data item (byte) size. */
+ write_int (w, size); /* Number of data items. */
+
+ start = ftello (w->file);
+ for (i = 0; i < n_vars; i++)
+ {
+ struct variable *var = dict_get_var (dict, i);
+ const struct val_labs *val_labs = var_get_value_labels (var);
+ const char *encoding = var_get_encoding (var);
+ int width = var_get_width (var);
+ const struct val_lab *val_lab;
+ char *var_name;
+
+ if (val_labs_count (val_labs) == 0 || width < 9)
+ continue;
+
+ var_name = recode_string (encoding, "UTF-8", var_get_name (var), -1);
+ write_int (w, strlen (var_name));
+ write_bytes (w, var_name, strlen (var_name));
+ free (var_name);
+
+ write_int (w, width);
+ write_int (w, val_labs_count (val_labs));
+ for (val_lab = val_labs_first (val_labs); val_lab != NULL;
+ val_lab = val_labs_next (val_labs, val_lab))
+ {
+ char *label;
+ size_t len;
+
+ write_int (w, width);
+ write_bytes (w, value_str (val_lab_get_value (val_lab), width),
+ width);
+
+ label = recode_string (var_get_encoding (var), "UTF-8",
+ val_lab_get_escaped_label (val_lab), -1);
+ len = strlen (label);
+ write_int (w, len);
+ write_bytes (w, label, len);
+ free (label);
+ }
+ }
+ assert (ftello (w->file) == start + size);
+}
+