+/* Returns a copy of SETTINGS. */
+struct fmt_settings *
+fmt_settings_clone (const struct fmt_settings *old)
+{
+ struct fmt_settings *new;
+ int t;
+
+ new = xmalloc (sizeof *new);
+ for (t = 0 ; t < FMT_NUMBER_OF_FORMATS ; ++t )
+ fmt_number_style_clone (&new->styles[t], &old->styles[t]);
+
+ return new;
+}
+
+/* Returns the number formatting style associated with the given
+ format TYPE. */
+const struct fmt_number_style *
+fmt_settings_get_style (const struct fmt_settings *settings,
+ enum fmt_type type)
+{
+ assert (is_fmt_type (type));
+ return &settings->styles[type];
+}
+
+void
+fmt_settings_set_style (struct fmt_settings *settings, enum fmt_type type,
+ const struct fmt_number_style *style)
+{
+ fmt_check_style (style);
+ fmt_number_style_destroy (&settings->styles[type]);
+ fmt_number_style_clone (&settings->styles[type], style);
+}
+
+/* Sets the number style for TYPE to have the given standard
+ PREFIX and SUFFIX, "-" as prefix suffix, an empty negative
+ suffix, DECIMAL as the decimal point character, and GROUPING
+ as the grouping character. */
+static void
+set_style (struct fmt_settings *settings, enum fmt_type type,
+ const char *prefix, const char *suffix,
+ char decimal, char grouping)
+{
+ struct fmt_number_style *style;
+
+ assert (is_fmt_type (type));
+
+ style = &settings->styles[type];
+
+ fmt_number_style_destroy (style);
+
+ ss_alloc_substring (&style->neg_prefix, ss_cstr ("-"));
+ ss_alloc_substring (&style->prefix, ss_cstr (prefix));
+ ss_alloc_substring (&style->suffix, ss_cstr (suffix));
+ style->decimal = decimal;
+ style->grouping = grouping;
+}
+
+/* Sets the decimal point character for SETTINGS to DECIMAL. */
+void
+fmt_settings_set_decimal (struct fmt_settings *settings, char decimal)
+{
+ int grouping = decimal == '.' ? ',' : '.';
+ assert (decimal == '.' || decimal == ',');
+
+ set_style (settings, FMT_F, "", "", decimal, 0);
+ set_style (settings, FMT_E, "", "", decimal, 0);
+ set_style (settings, FMT_COMMA, "", "", decimal, grouping);
+ set_style (settings, FMT_DOT, "", "", grouping, decimal);
+ set_style (settings, FMT_DOLLAR, "$", "", decimal, grouping);
+ set_style (settings, FMT_PCT, "", "%", decimal, 0);
+}
+
+/* Returns an input format specification with type TYPE, width W,
+ and D decimals. */
+struct fmt_spec
+fmt_for_input (enum fmt_type type, int w, int d)
+{
+ struct fmt_spec f;
+ f.type = type;
+ f.w = w;
+ f.d = d;
+ assert (fmt_check_input (&f));
+ return f;
+}
+
+/* Returns an output format specification with type TYPE, width
+ W, and D decimals. */
+struct fmt_spec
+fmt_for_output (enum fmt_type type, int w, int d)
+{
+ struct fmt_spec f;
+ f.type = type;
+ f.w = w;
+ f.d = d;
+ assert (fmt_check_output (&f));
+ return f;
+}
+
+/* Returns the output format specifier corresponding to input
+ format specifier INPUT. */
+struct fmt_spec
+fmt_for_output_from_input (const struct fmt_spec *input)
+{
+ struct fmt_spec output;
+
+ assert (fmt_check_input (input));
+
+ output.type = fmt_input_to_output (input->type);
+ output.w = input->w;
+ if (output.w > fmt_max_output_width (output.type))
+ output.w = fmt_max_output_width (output.type);
+ else if (output.w < fmt_min_output_width (output.type))
+ output.w = fmt_min_output_width (output.type);
+ output.d = input->d;
+
+ switch (input->type)
+ {
+ case FMT_Z:
+ output.w++;
+ if (output.d > 0)
+ output.w++;
+ break;
+
+ case FMT_F:
+ case FMT_COMMA:
+ case FMT_DOT:
+ case FMT_DOLLAR:
+ case FMT_PCT:
+ {
+ const struct fmt_number_style *style =
+ settings_get_style (input->type);
+
+ output.w += fmt_affix_width (style);
+ if (style->grouping != 0 && input->w - input->d >= 3)
+ output.w += (input->w - input->d - 1) / 3;
+ if (output.d > 0)
+ output.w++;
+ }
+ break;
+
+ case FMT_N:
+ if (output.d > 0)
+ output.w++;
+ break;
+
+ case FMT_E:
+ output.d = MAX (input->d, 3);
+ output.w = MAX (input->w, output.d + 7);
+ break;
+
+ case FMT_PIBHEX:
+ output.w = max_digits_for_bytes (input->w / 2) + 1;
+ break;
+
+ case FMT_RB:
+ case FMT_RBHEX:
+ output.w = 8;
+ output.d = 2;
+ break;
+
+ case FMT_P:
+ case FMT_PK:
+ output.w = 2 * input->w + (input->d > 0);
+ break;
+
+ case FMT_IB:
+ case FMT_PIB:
+ output.w = max_digits_for_bytes (input->w) + 1;
+ if (output.d > 0)
+ output.w++;
+ break;
+
+ case FMT_CCA:
+ case FMT_CCB:
+ case FMT_CCC:
+ case FMT_CCD:
+ case FMT_CCE:
+ NOT_REACHED ();
+
+ case FMT_A:
+ break;
+
+ case FMT_AHEX:
+ output.w = input->w / 2;
+ break;
+
+ case FMT_DATE:
+ case FMT_EDATE:
+ case FMT_SDATE:
+ case FMT_ADATE:
+ case FMT_JDATE:
+ case FMT_QYR:
+ case FMT_MOYR:
+ case FMT_WKYR:
+ case FMT_TIME:
+ case FMT_DTIME:
+ case FMT_DATETIME:
+ case FMT_WKDAY:
+ case FMT_MONTH:
+ break;
+
+ default:
+ NOT_REACHED ();
+ }
+
+ if (output.w > fmt_max_output_width (output.type))
+ output.w = fmt_max_output_width (output.type);
+
+ assert (fmt_check_output (&output));
+ return output;
+}
+
+/* Returns the default format for the given WIDTH: F8.2 format
+ for a numeric value, A format for a string value. */
+struct fmt_spec
+fmt_default_for_width (int width)
+{
+ return (width == 0
+ ? fmt_for_output (FMT_F, 8, 2)
+ : fmt_for_output (FMT_A, width, 0));
+}
+
+/* Checks whether SPEC is valid as an input format (if FOR_INPUT)
+ or an output format (otherwise) and returns nonzero if so.
+ Otherwise, emits an error message and returns zero. */
+bool
+fmt_check (const struct fmt_spec *spec, bool for_input)