From 8a4a4e05579eb697721b59b8e718cdb1d96aeb5f Mon Sep 17 00:00:00 2001 From: Niklas Schnelle Date: Wed, 17 Jun 2026 21:45:35 +0200 Subject: [PATCH] libutil/util_fmt: Add util_fmt_type_to_name() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a function to get the textual name of an enum util_fmt_t value. To make this robust to changes in the order of elements in the format array initialize this using named indices. Reviewed-by: Jan Höppner Signed-off-by: Niklas Schnelle Signed-off-by: Jan Höppner --- include/lib/util_fmt.h | 10 ++++++++++ libutil/util_fmt.c | 30 +++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/lib/util_fmt.h b/include/lib/util_fmt.h index 1d3b9af9..ac57cd4f 100644 --- a/include/lib/util_fmt.h +++ b/include/lib/util_fmt.h @@ -157,6 +157,16 @@ void util_fmt_exit(void); */ bool util_fmt_name_to_type(const char *name, enum util_fmt_t *type); +/** + * util_fmt_type_to_name() - Get name of given format type identifier. + * @type: Format type identifier + * + * Get the name corresponding to the given format type identifier. + * + * Return: name of the format type + */ +const char *util_fmt_type_to_name(enum util_fmt_t type); + /** * util_fmt_set_indent() - Set indentation parameters. * @base : Base indentation level to apply to all output lines (default 0) diff --git a/libutil/util_fmt.c b/libutil/util_fmt.c index 31bbe704..f4e05fae 100644 --- a/libutil/util_fmt.c +++ b/libutil/util_fmt.c @@ -89,11 +89,26 @@ static const struct { const char *name; enum util_fmt_t fmt; } formats[] = { - { "json", FMT_JSON }, - { "json-seq", FMT_JSONSEQ }, - { "jsonl", FMT_JSONL }, - { "pairs", FMT_PAIRS }, - { "csv", FMT_CSV }, + [FMT_JSON] = { + .name = "json", + .fmt = FMT_JSON + }, + [FMT_JSONSEQ] = { + .name = "json-seq", + .fmt = FMT_JSONSEQ + }, + [FMT_JSONL] = { + .name = "jsonl", + .fmt = FMT_JSONL + }, + [FMT_PAIRS] = { + .name = "pairs", + .fmt = FMT_PAIRS + }, + [FMT_CSV] = { + .name = "csv", + .fmt = FMT_CSV + }, }; /* Signal mask for blocking INT and TERM signals. */ @@ -112,6 +127,11 @@ bool util_fmt_name_to_type(const char *name, enum util_fmt_t *type) return false; } +const char *util_fmt_type_to_name(enum util_fmt_t type) +{ + return formats[type].name; +} + bool util_fmt_is_json(enum util_fmt_t type) { switch (type) {