+ enum fmt_type type;
+
+ for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
+ if (get_fmt_desc (type)->io == io)
+ {
+ *fmt_type = type;
+ return true;
+ }
+ return false;
+}
+
+/* Returns true if TYPE may be used as an input format,
+ false otherwise. */
+bool
+fmt_usable_for_input (enum fmt_type type)
+{
+ assert (is_fmt_type (type));
+ return fmt_get_category (type) != FMT_CAT_CUSTOM;
+}
+
+/* For time and date formats, returns a template used for input
+ and output. */
+const char *
+fmt_date_template (enum fmt_type type)
+{
+ switch (type)
+ {
+ case FMT_DATE:
+ return "dd-mmm-yy";
+ case FMT_ADATE:
+ return "mm/dd/yy";
+ case FMT_EDATE:
+ return "dd.mm.yy";
+ case FMT_JDATE:
+ return "yyddd";
+ case FMT_SDATE:
+ return "yy/mm/dd";
+ case FMT_QYR:
+ return "q Q yy";
+ case FMT_MOYR:
+ return "mmmXyy";
+ case FMT_WKYR:
+ return "ww WK yy";
+ case FMT_DATETIME:
+ return "dd-mmm-yyyy HH:MM";
+ case FMT_TIME:
+ return "H:MM";
+ case FMT_DTIME:
+ return "D HH:MM";
+ default:
+ NOT_REACHED ();
+ }
+}
+
+\f
+/* Returns true if TYPE is a valid format type,
+ false otherwise. */
+bool
+is_fmt_type (enum fmt_type type)
+{
+ 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);