+ return type < FMT_NUMBER_OF_FORMATS;
+}
+
+/* Returns true if WIDTH is a valid width for the given format
+ TYPE,
+ for input if FOR_INPUT is true,
+ for output otherwise. */
+static bool
+valid_width (enum fmt_type type, int width, bool for_input)
+{
+ return (width >= fmt_min_width (type, for_input)
+ && width <= fmt_max_width (type, for_input));
+}
+
+/* Returns the maximum number of decimal digits in an unsigned
+ binary number that is BYTES bytes long. */
+static int
+max_digits_for_bytes (int bytes)
+{
+ int map[8] = {3, 5, 8, 10, 13, 15, 17, 20};
+ assert (bytes > 0 && bytes <= sizeof map / sizeof *map);
+ return map[bytes - 1];
+}
+\f
+
+
+void
+fmt_number_style_init (struct fmt_number_style *style)
+{
+ style->neg_prefix = ss_empty ();
+ style->prefix = ss_empty ();
+ style->suffix = ss_empty ();
+ style->neg_suffix = ss_empty ();
+ style->decimal = '.';
+ style->grouping = 0;
+}
+
+
+/* Destroys a struct fmt_number_style. */
+void
+fmt_number_style_destroy (struct fmt_number_style *style)
+{
+ if (style != NULL)
+ {
+ ss_dealloc (&style->neg_prefix);
+ ss_dealloc (&style->prefix);
+ ss_dealloc (&style->suffix);
+ ss_dealloc (&style->neg_suffix);
+ }
+}
+
+/* Returns the number formatting style associated with the given
+ format TYPE. */
+const struct fmt_number_style *
+fmt_get_style (const struct fmt_number_style *styles, enum fmt_type type)
+{
+ assert (is_fmt_type (type));
+ return &styles[type];
+}
+
+
+/* Checks that style is STYLE sane */
+void
+fmt_check_style (const struct fmt_number_style *style)
+{
+ assert (ss_length (style->neg_prefix) <= FMT_STYLE_AFFIX_MAX);
+ assert (ss_length (style->prefix) <= FMT_STYLE_AFFIX_MAX);
+ assert (ss_length (style->suffix) <= FMT_STYLE_AFFIX_MAX);
+ assert (ss_length (style->neg_suffix) <= FMT_STYLE_AFFIX_MAX);
+ assert (style->decimal == '.' || style->decimal == ',');
+ assert (style->grouping == '.' || style->grouping == ','
+ || style->grouping == 0);
+ assert (style->grouping != style->decimal);
+}
+
+
+/* Returns the total width of the standard prefix and suffix for
+ STYLE. */
+int
+fmt_affix_width (const struct fmt_number_style *style)
+{
+ return ss_length (style->prefix) + ss_length (style->suffix);
+}
+
+/* Returns the total width of the negative prefix and suffix for
+ STYLE. */
+int
+fmt_neg_affix_width (const struct fmt_number_style *style)
+{
+ return ss_length (style->neg_prefix) + ss_length (style->neg_suffix);
+}
+
+
+/* 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_number_style *styles, 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 = &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 to DECIMAL. */
+void
+fmt_set_decimal (struct fmt_number_style *styles, char decimal)
+{
+ int grouping = decimal == '.' ? ',' : '.';
+ assert (decimal == '.' || decimal == ',');
+
+ set_style (styles, FMT_F, "", "", decimal, 0);
+ set_style (styles, FMT_E, "", "", decimal, 0);
+ set_style (styles, FMT_COMMA, "", "", decimal, grouping);
+ set_style (styles, FMT_DOT, "", "", grouping, decimal);
+ set_style (styles, FMT_DOLLAR, "$", "", decimal, grouping);
+ set_style (styles, FMT_PCT, "", "%", decimal, 0);
+}
+\f
+/* Returns the struct fmt_desc for the given format TYPE. */
+static const struct fmt_desc *
+get_fmt_desc (enum fmt_type type)
+{
+ static const struct fmt_desc formats[FMT_NUMBER_OF_FORMATS] =
+ {
+#define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) \
+ {#NAME, IMIN, OMIN, IO, CATEGORY},
+#include "format.def"
+ };
+
+ assert (is_fmt_type (type));
+ return &formats[type];