From 4d25c35030415c091380f500aede9cf71ea6480f Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 23 Aug 2012 20:39:04 -0400 Subject: [PATCH] Fix: add missing enum support to API Signed-off-by: Mathieu Desnoyers --- formats/ctf/events.c | 48 +++++++++++++++++++++++++++++++++ include/babeltrace/ctf/events.h | 6 +++++ 2 files changed, 54 insertions(+) diff --git a/formats/ctf/events.c b/formats/ctf/events.c index a9f5c22e..890956e9 100644 --- a/formats/ctf/events.c +++ b/formats/ctf/events.c @@ -405,6 +405,54 @@ ssize_t bt_ctf_get_int_len(const struct definition *field) return ret; } +const struct definition *bt_ctf_get_enum_int(const struct definition *field) +{ + struct definition_enum *def_enum; + + if (!field || bt_ctf_field_type(field) != CTF_TYPE_ENUM) { + bt_ctf_field_set_error(-EINVAL); + return NULL; + } + def_enum = container_of(field, struct definition_enum, p); + return &def_enum->integer->p; +} + +const char *bt_ctf_get_enum_str(const struct definition *field) +{ + struct definition_enum *def_enum; + struct declaration_enum *decl_enum; + GArray *array; + const char *ret; + + if (!field || bt_ctf_field_type(field) != CTF_TYPE_ENUM) { + bt_ctf_field_set_error(-EINVAL); + return NULL; + } + def_enum = container_of(field, struct definition_enum, p); + decl_enum = def_enum->declaration; + if (get_int_signedness(&def_enum->integer->p)) { + array = enum_int_to_quark_set(decl_enum, + get_signed_int(&def_enum->integer->p)); + } else { + array = enum_uint_to_quark_set(decl_enum, + get_unsigned_int(&def_enum->integer->p)); + } + if (!array) { + bt_ctf_field_set_error(-ENOENT); + return NULL; + } + + if (array->len == 0) { + g_array_unref(array); + bt_ctf_field_set_error(-ENOENT); + return NULL; + } + /* Return first string. Arbitrary choice. */ + ret = g_quark_to_string(g_array_index(array, GQuark, 0)); + g_array_unref(array); + return ret; +} + enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field) { enum ctf_string_encoding ret = 0; diff --git a/include/babeltrace/ctf/events.h b/include/babeltrace/ctf/events.h index 88795fa0..c34ae9a4 100644 --- a/include/babeltrace/ctf/events.h +++ b/include/babeltrace/ctf/events.h @@ -193,9 +193,15 @@ int bt_ctf_get_array_len(const struct definition *field); * If the field does not exist or is not of the type requested, the value * returned is undefined. To check if an error occured, use the * bt_ctf_field_get_error() function after accessing a field. + * + * bt_ctf_get_enum_int gets the integer field of an enumeration. + * bt_ctf_get_enum_str gets the string matching the current enumeration + * value, or NULL if the current value does not match any string. */ uint64_t bt_ctf_get_uint64(const struct definition *field); int64_t bt_ctf_get_int64(const struct definition *field); +const struct definition *bt_ctf_get_enum_int(const struct definition *field); +const char *bt_ctf_get_enum_str(const struct definition *field); char *bt_ctf_get_char_array(const struct definition *field); char *bt_ctf_get_string(const struct definition *field); -- 2.34.1