+/* Returns the width corresponding to the format specifier. The
+ return value is the value of the `width' member of a `struct
+ variable' for such an input format. */
+int
+fmt_var_width (const struct fmt_spec *spec)
+{
+ return (spec->type == FMT_AHEX ? spec->w / 2
+ : spec->type == FMT_A ? spec->w
+ : 0);
+}
+
+/* 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. */