+/* Converts F to its string representation (for instance, "F8.2")
+ in BUFFER. Returns BUFFER.
+
+ If F has decimals, they are included in the output string,
+ even if F's format type does not allow decimals, to allow
+ accurately presenting incorrect formats to the user. */
+char *
+fmt_to_string (const struct fmt_spec *f, char buffer[FMT_STRING_LEN_MAX + 1])
+{
+ if (fmt_takes_decimals (f->type) || f->d > 0)
+ snprintf (buffer, FMT_STRING_LEN_MAX + 1,
+ "%s%d.%d", fmt_name (f->type), f->w, f->d);
+ else
+ snprintf (buffer, FMT_STRING_LEN_MAX + 1,
+ "%s%d", fmt_name (f->type), f->w);
+ return buffer;
+}
+
+/* Returns true if A and B are identical formats,
+ false otherwise. */
+bool
+fmt_equal (const struct fmt_spec *a, const struct fmt_spec *b)
+{
+ return a->type == b->type && a->w == b->w && a->d == b->d;
+}
+\f
+/* Describes a display format. */
+struct fmt_desc
+ {
+ char name[9];
+ int min_input_width, min_output_width;
+ int io;
+ enum fmt_category category;
+ };
+
+static const struct fmt_desc *get_fmt_desc (enum fmt_type type);
+
+/* Returns the name of the given format TYPE. */
+const char *
+fmt_name (enum fmt_type type)
+{
+ return get_fmt_desc (type)->name;
+}
+
+/* Tries to parse NAME as a format type.
+ If successful, stores the type in *TYPE and returns true.
+ On failure, returns false. */
+bool
+fmt_from_name (const char *name, enum fmt_type *type)
+{
+ int i;
+
+ for (i = 0; i < FMT_NUMBER_OF_FORMATS; i++)
+ if (!strcasecmp (name, get_fmt_desc (i)->name))
+ {
+ *type = i;
+ return true;
+ }
+ return false;
+}
+
+/* Returns true if TYPE accepts decimal places,
+ false otherwise. */
+bool
+fmt_takes_decimals (enum fmt_type type)
+{
+ return fmt_max_output_decimals (type, fmt_max_output_width (type)) > 0;
+}
+
+/* Returns the minimum acceptable width for an input field
+ formatted with the given TYPE. */
+int
+fmt_min_input_width (enum fmt_type type)
+{
+ return get_fmt_desc (type)->min_input_width;
+}
+
+/* Returns the maximum acceptable width for an input field
+ formatted with the given TYPE. */
+int
+fmt_max_input_width (enum fmt_type type)
+{
+ return max_width (type);
+}
+
+/* Returns the maximum number of decimal places allowed in an
+ input field of the given TYPE and WIDTH. */
+int
+fmt_max_input_decimals (enum fmt_type type, int width)
+{
+ assert (valid_width (type, width, true));
+ return max_decimals (type, width, true);
+}
+
+/* Returns the minimum acceptable width for an output field
+ formatted with the given TYPE. */
+int
+fmt_min_output_width (enum fmt_type type)
+{
+ return get_fmt_desc (type)->min_output_width;
+}
+
+/* Returns the maximum acceptable width for an output field
+ formatted with the given TYPE. */
+int
+fmt_max_output_width (enum fmt_type type)
+{
+ return max_width (type);
+}
+
+/* Returns the maximum number of decimal places allowed in an
+ output field of the given TYPE and WIDTH. */
+int
+fmt_max_output_decimals (enum fmt_type type, int width)
+{
+ assert (valid_width (type, width, false));
+ return max_decimals (type, width, false);
+}
+
+/* Returns the width step for a field formatted with the given
+ TYPE. Field width must be a multiple of the width step. */
+int
+fmt_step_width (enum fmt_type type)
+{
+ return fmt_get_category (type) == FMT_CAT_HEXADECIMAL ? 2 : 1;
+}
+
+/* Returns true if TYPE is used for string fields,
+ false if it is used for numeric fields. */
+bool
+fmt_is_string (enum fmt_type type)
+{
+ return fmt_get_category (type) == FMT_CAT_STRING;
+}
+
+/* Returns true if TYPE is used for numeric fields,
+ false if it is used for string fields. */
+bool
+fmt_is_numeric (enum fmt_type type)
+{
+ return !fmt_is_string (type);
+}
+
+/* Returns the format TYPE's category.
+ Each format type is in exactly one category,
+ and each category's value is bitwise disjoint from every other
+ category. Thus, the return value may be tested for equality
+ or compared bitwise against a mask of FMT_CAT_* values. */
+enum fmt_category
+fmt_get_category (enum fmt_type type)
+{
+ return get_fmt_desc (type)->category;
+}