From: Julien Desfossez Date: Thu, 1 Mar 2012 14:10:50 +0000 (-0500) Subject: Fix API : functions to access fields properties X-Git-Tag: v1.0.0-pre3~11 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=8673030f3cb8d157a30e79af524fd5cca253025e;hp=305c65e5d7156ae7936f07ad93dd45ac318b4ce2 Fix API : functions to access fields properties Add the ability to know : - the signedness of an int - the base of an int - the encoding of an int - the len of an array - the encoding of a string refs #139 Signed-off-by: Julien Desfossez Signed-off-by: Mathieu Desnoyers --- diff --git a/formats/ctf/events.c b/formats/ctf/events.c index 39620383..eea58cfd 100644 --- a/formats/ctf/events.c +++ b/formats/ctf/events.c @@ -351,6 +351,84 @@ int bt_ctf_field_get_error(void) return ret; } +int bt_ctf_get_int_signedness(const struct definition *field) +{ + int ret; + + if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) { + ret = get_int_signedness(field); + } else { + ret = -1; + bt_ctf_field_set_error(-EINVAL); + } + + return ret; +} + +int bt_ctf_get_int_base(const struct definition *field) +{ + int ret; + + if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) { + ret = get_int_base(field); + } else { + ret = -1; + bt_ctf_field_set_error(-EINVAL); + } + + return ret; +} + +int bt_ctf_get_int_byte_order(const struct definition *field) +{ + int ret; + + if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) { + ret = get_int_byte_order(field); + } else { + ret = -1; + bt_ctf_field_set_error(-EINVAL); + } + + return ret; +} + +enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field) +{ + enum ctf_string_encoding ret = 0; + + if (!field) + goto end; + + if (bt_ctf_field_type(field) == CTF_TYPE_INTEGER) + ret = get_int_encoding(field); + else if (bt_ctf_field_type(field) == CTF_TYPE_STRING) + ret = get_string_encoding(field); + else + goto error; + +end: + return ret; + +error: + bt_ctf_field_set_error(-EINVAL); + return -1; +} + +int bt_ctf_get_array_len(const struct definition *field) +{ + int ret; + + if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) { + ret = get_array_len(field); + } else { + ret = -1; + bt_ctf_field_set_error(-EINVAL); + } + + return ret; +} + uint64_t bt_ctf_get_uint64(const struct definition *field) { unsigned int ret = 0; diff --git a/include/babeltrace/ctf/events.h b/include/babeltrace/ctf/events.h index 01ee7a99..a08518b8 100644 --- a/include/babeltrace/ctf/events.h +++ b/include/babeltrace/ctf/events.h @@ -60,6 +60,16 @@ enum ctf_type_id { NR_CTF_TYPES, }; +/* + * the supported CTF string encodings + */ +enum ctf_string_encoding { + CTF_STRING_NONE = 0, + CTF_STRING_UTF8, + CTF_STRING_ASCII, + CTF_STRING_UNKNOWN, +}; + /* * the structure to manipulate events */ @@ -169,6 +179,38 @@ const char *bt_ctf_field_name(const struct definition *def); */ enum ctf_type_id bt_ctf_field_type(const struct definition *def); +/* + * bt_ctf_get_int_signedness: return the signedness of an integer + * + * return 0 if unsigned + * return 1 if signed + * return -1 on error + */ +int bt_ctf_get_int_signedness(const struct definition *field); + +/* + * bt_ctf_get_int_base: return the base of an int or a negative value on error + */ +int bt_ctf_get_int_base(const struct definition *field); + +/* + * bt_ctf_get_int_byte_order: return the byte order of an int or a negative + * value on error + */ +int bt_ctf_get_int_byte_order(const struct definition *field); + +/* + * bt_ctf_get_encoding: return the encoding of an int or a string. + * return a negative value on error + */ +enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field); + +/* + * bt_ctf_get_array_len: return the len of an array or a negative + * value on error + */ +int bt_ctf_get_array_len(const struct definition *field); + /* * Field access functions * diff --git a/include/babeltrace/types.h b/include/babeltrace/types.h index ad2b42d0..cfc74a2f 100644 --- a/include/babeltrace/types.h +++ b/include/babeltrace/types.h @@ -120,13 +120,6 @@ int generic_rw(struct stream_pos *pos, struct definition *definition) return call(pos, definition); } -enum ctf_string_encoding { - CTF_STRING_NONE = 0, - CTF_STRING_UTF8, - CTF_STRING_ASCII, - CTF_STRING_UNKNOWN, -}; - /* * Because we address in bits, bitfields end up being exactly the same as * integers, except that their read/write functions must be able to deal with @@ -375,6 +368,10 @@ struct declaration_integer *integer_declaration_new(size_t len, int byte_order, struct ctf_clock *clock); uint64_t get_unsigned_int(const struct definition *field); int64_t get_signed_int(const struct definition *field); +int get_int_signedness(const struct definition *field); +int get_int_byte_order(const struct definition *field); +int get_int_base(const struct definition *field); +enum ctf_string_encoding get_int_encoding(const struct definition *field); /* * mantissa_len is the length of the number of bytes represented by the mantissa @@ -422,6 +419,7 @@ struct declaration_enum * struct declaration_string * string_declaration_new(enum ctf_string_encoding encoding); char *get_string(const struct definition *field); +enum ctf_string_encoding get_string_encoding(const struct definition *field); struct declaration_struct * struct_declaration_new(struct declaration_scope *parent_scope, @@ -487,6 +485,7 @@ uint64_t array_len(struct definition_array *array); struct definition *array_index(struct definition_array *array, uint64_t i); int array_rw(struct stream_pos *pos, struct definition *definition); GString *get_char_array(const struct definition *field); +int get_array_len(const struct definition *field); /* * int_declaration and elem_declaration passed as parameter now belong diff --git a/types/array.c b/types/array.c index dbdcb4be..047adac2 100644 --- a/types/array.c +++ b/types/array.c @@ -207,6 +207,17 @@ struct definition *array_index(struct definition_array *array, uint64_t i) return g_ptr_array_index(array->elems, i); } +int get_array_len(const struct definition *field) +{ + struct definition_array *array_definition; + struct declaration_array *array_declaration; + + array_definition = container_of(field, struct definition_array, p); + array_declaration = array_definition->declaration; + + return array_declaration->len; +} + GString *get_char_array(const struct definition *field) { struct definition_array *array_definition; diff --git a/types/integer.c b/types/integer.c index 6e124305..808a4893 100644 --- a/types/integer.c +++ b/types/integer.c @@ -107,6 +107,50 @@ void _integer_definition_free(struct definition *definition) g_free(integer); } +enum ctf_string_encoding get_int_encoding(const struct definition *field) +{ + struct definition_integer *integer_definition; + const struct declaration_integer *integer_declaration; + + integer_definition = container_of(field, struct definition_integer, p); + integer_declaration = integer_definition->declaration; + + return integer_declaration->encoding; +} + +int get_int_base(const struct definition *field) +{ + struct definition_integer *integer_definition; + const struct declaration_integer *integer_declaration; + + integer_definition = container_of(field, struct definition_integer, p); + integer_declaration = integer_definition->declaration; + + return integer_declaration->base; +} + +int get_int_byte_order(const struct definition *field) +{ + struct definition_integer *integer_definition; + const struct declaration_integer *integer_declaration; + + integer_definition = container_of(field, struct definition_integer, p); + integer_declaration = integer_definition->declaration; + + return integer_declaration->byte_order; +} + +int get_int_signedness(const struct definition *field) +{ + struct definition_integer *integer_definition; + const struct declaration_integer *integer_declaration; + + integer_definition = container_of(field, struct definition_integer, p); + integer_declaration = integer_definition->declaration; + + return integer_declaration->signedness; +} + uint64_t get_unsigned_int(const struct definition *field) { struct definition_integer *integer_definition; diff --git a/types/string.c b/types/string.c index dadcd080..daaeef5a 100644 --- a/types/string.c +++ b/types/string.c @@ -101,6 +101,17 @@ void _string_definition_free(struct definition *definition) g_free(string); } +enum ctf_string_encoding get_string_encoding(const struct definition *field) +{ + struct definition_string *string_definition; + const struct declaration_string *string_declaration; + + string_definition = container_of(field, struct definition_string, p); + string_declaration = string_definition->declaration; + + return string_declaration->encoding; +} + char *get_string(const struct definition *field) { struct definition_string *string_definition =