+ 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. */
+
+ 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);
+ 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, val_lab_get_value (val_lab)->s, 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);
+ }
+ }
+}
+
+static void
+write_long_string_missing_values (struct sfm_writer *w,
+ const struct dictionary *dict)
+{
+ const char *encoding = dict_get_encoding (dict);
+ size_t n_vars = dict_get_var_cnt (dict);
+ size_t size, i;
+
+ /* 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 missing_values *mv = var_get_missing_values (var);
+ int width = var_get_width (var);
+
+ if (mv_is_empty (mv) || width < 9)
+ continue;
+
+ size += 4;
+ size += recode_string_len (encoding, "UTF-8", var_get_name (var), -1);
+ size += 1;
+ size += mv_n_values (mv) * (4 + 8);
+ }
+ if (size == 0)
+ return;
+
+ write_int (w, 7); /* Record type. */
+ write_int (w, 22); /* Record subtype */
+ write_int (w, 1); /* Data item (byte) size. */
+ write_int (w, size); /* Number of data items. */
+
+ for (i = 0; i < n_vars; i++)
+ {
+ struct variable *var = dict_get_var (dict, i);
+ const struct missing_values *mv = var_get_missing_values (var);
+ int width = var_get_width (var);
+ uint8_t n_missing_values;
+ char *var_name;
+ int j;
+
+ if (mv_is_empty (mv) || 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);
+
+ n_missing_values = mv_n_values (mv);
+ write_bytes (w, &n_missing_values, 1);
+
+ for (j = 0; j < n_missing_values; j++)
+ {
+ const union value *value = mv_get_value (mv, j);
+
+ write_int (w, 8);
+ write_bytes (w, value->s, 8);
+ }
+ }
+}
+
+static void
+write_encoding_record (struct sfm_writer *w,
+ const struct dictionary *d)
+{
+ /* IANA says "...character set names may be up to 40 characters taken
+ from the printable characters of US-ASCII," so character set names
+ don't need to be recoded to be in UTF-8.
+
+ We convert encoding names to uppercase because SPSS writes encoding
+ names in uppercase. */
+ char *encoding = xstrdup (dict_get_encoding (d));
+ str_uppercase (encoding);
+ write_string_record (w, ss_cstr (encoding), 20);
+ free (encoding);