diff options
-rw-r--r-- | include/osmocom/core/utils.h | 2 | ||||
-rw-r--r-- | src/utils.c | 21 |
2 files changed, 20 insertions, 3 deletions
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index bc395193..476e492b 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -25,6 +25,8 @@ struct value_string { }; const char *get_value_string(const struct value_string *vs, uint32_t val); +const char *get_value_string_or_null(const struct value_string *vs, + uint32_t val); int get_string_value(const struct value_string *vs, const char *str); diff --git a/src/utils.c b/src/utils.c index e76297ff..4a548022 100644 --- a/src/utils.c +++ b/src/utils.c @@ -50,6 +50,23 @@ static char namebuf[255]; */ const char *get_value_string(const struct value_string *vs, uint32_t val) { + const char *str = get_value_string_or_null(vs, val); + if (str) + return str; + + snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val); + namebuf[sizeof(namebuf) - 1] = '\0'; + return namebuf; +} + +/*! \brief get human-readable string or NULL for given value + * \param[in] vs Array of value_string tuples + * \param[in] val Value to be converted + * \returns pointer to human-readable string or NULL if val is not found + */ +const char *get_value_string_or_null(const struct value_string *vs, + uint32_t val) +{ int i; for (i = 0;; i++) { @@ -59,9 +76,7 @@ const char *get_value_string(const struct value_string *vs, uint32_t val) return vs[i].str; } - snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val); - namebuf[sizeof(namebuf) - 1] = '\0'; - return namebuf; + return NULL; } /*! \brief get numeric value for given human-readable string |