- {
- const union value *from = &items[j]->from;
- size_t len;
- char *recoded_value = NULL;
- char *c;
-
- union value to_val;
- value_init (&to_val, 0);
-
- items[j]->to = direction == ASCENDING ? j + 1 : n_items - j;
-
- to_val.f = items[j]->to;
-
- /* Add value labels to the destination variable which indicate
- the source value from whence the new value comes. */
-
- if (src_width > 0)
- {
- const char *str = (const char *) value_str (from, src_width);
-
- recoded_value = recode_string (UTF8, dict_get_encoding (dict), str, src_width);
- }
- else
- recoded_value = asnprintf (NULL, &len, "%g", from->f);
-
- /* Remove trailing whitespace */
- for (c = recoded_value; *c != '\0'; c++)
- if ( *c == ' ')
- {
- *c = '\0';
- break;
- }
-
- var_add_value_label (spec->dst, &to_val, recoded_value);
- value_destroy (&to_val, 0);
- free (recoded_value);
- }
+ items[j]->to = j + 1;
+
+ if (print && (!group || i == 0))
+ {
+ struct pivot_value *title
+ = (group
+ ? pivot_value_new_text (N_("Recoding grouped variables."))
+ : spec->label && spec->label[0]
+ ? pivot_value_new_text_format (N_("Recoding %s into %s (%s)."),
+ spec->src_name,
+ var_get_name (spec->dst),
+ spec->label)
+ : pivot_value_new_text_format (N_("Recoding %s into %s."),
+ spec->src_name,
+ var_get_name (spec->dst)));
+ struct pivot_table *table = pivot_table_create__ (title, "Recoding");
+
+ pivot_dimension_create (
+ table, PIVOT_AXIS_COLUMN, N_("Attributes"),
+ N_("New Value"), N_("Value Label"));
+
+ struct pivot_dimension *old_values = pivot_dimension_create (
+ table, PIVOT_AXIS_ROW, N_("Old Value"));
+ old_values->root->show_label = true;
+
+ for (size_t k = 0; k < n_items; k++)
+ {
+ const struct arc_item *item = items[k];
+ int old_value_idx = pivot_category_create_leaf (
+ old_values->root, pivot_value_new_value (
+ &item->from, item->width,
+ (item->width
+ ? &(struct fmt_spec) { FMT_F, item->width, 0 }
+ : &spec->format),
+ dict_get_encoding (dict)));
+ pivot_table_put2 (table, 0, old_value_idx,
+ pivot_value_new_integer (item->to));
+
+ const char *value_label = item->value_label;
+ if (value_label && value_label[0])
+ pivot_table_put2 (table, 1, old_value_idx,
+ pivot_value_new_user_text (value_label, -1));
+ }
+
+ pivot_table_submit (table);
+ }
+
+ /* Assign user-missing values.
+
+ User-missing values in the source variable(s) must be marked
+ as user-missing values in the destination variable. There
+ might be an arbitrary number of missing values, since the
+ source variable might have a range. Our sort function always
+ puts missing values together at the top of the range, so that
+ means that we can use a missing value range to cover all of
+ the user-missing values in any case (but we avoid it unless
+ necessary because user-missing value ranges are an obscure
+ feature). */
+ size_t n_missing = n_items;
+ for (size_t k = 0; k < n_items; k++)
+ if (!items[n_items - k - 1]->missing)
+ {
+ n_missing = k;
+ break;
+ }
+ if (n_missing > 0)
+ {
+ size_t lo = n_items - (n_missing - 1);
+ size_t hi = n_items;
+
+ struct missing_values mv;
+ mv_init (&mv, 0);
+ if (n_missing > 3)
+ mv_add_range (&mv, lo, hi);
+ else if (n_missing > 0)
+ for (size_t k = 0; k < n_missing; k++)
+ mv_add_num (&mv, lo + k);
+ var_set_missing_values (spec->dst, &mv);
+ mv_destroy (&mv);
+ }
+
+ /* Add value labels to the destination variable. */
+ for (j = 0; j < n_items; j++)
+ {
+ const char *value_label = items[j]->value_label;
+ if (value_label && value_label[0])
+ {
+ union value to_val = { .f = items[j]->to };
+ var_add_value_label (spec->dst, &to_val, value_label);
+ }
+ }