Extract int and char arrays from fields
authorJulien Desfossez <julien.desfossez@polymtl.ca>
Tue, 27 Sep 2011 01:00:10 +0000 (21:00 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 27 Sep 2011 01:00:10 +0000 (21:00 -0400)
Three helper functions to ease the extraction of basic types from event
fields. For now we support signed and unsigned integers and strings.
Other functions could be added following the same principle for other
types.

Signed-off-by: Julien Desfossez <julien.desfossez@polymtl.ca>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/babeltrace/types.h
types/array.c
types/integer.c

index 15b82bd74e86a47d7c3e586c434caa34053fbc35..41f70383c254af13d1f1a8d03fbee4652f00389c 100644 (file)
@@ -383,6 +383,8 @@ void definition_unref(struct definition *definition);
 struct declaration_integer *integer_declaration_new(size_t len, int byte_order,
                                  int signedness, size_t alignment,
                                  int base, enum ctf_string_encoding encoding);
+uint64_t get_unsigned_int(struct definition *field);
+int64_t get_signed_int(struct definition *field);
 
 /*
  * mantissa_len is the length of the number of bytes represented by the mantissa
@@ -493,6 +495,7 @@ struct declaration_array *
 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(struct definition *field);
 
 /*
  * int_declaration and elem_declaration passed as parameter now belong
index db6853f5ebcbf902a60cdcb9a86782c6bb225f5e..87b2083bcfdf4120ff41976ae6aa4689f887ac51 100644 (file)
@@ -205,3 +205,30 @@ struct definition *array_index(struct definition_array *array, uint64_t i)
                return NULL;
        return g_ptr_array_index(array->elems, i);
 }
+
+GString *get_char_array(struct definition *field)
+{
+       struct definition_array *array_definition;
+       struct declaration_array *array_declaration;
+       struct declaration *elem;
+
+       array_definition = container_of(field, struct definition_array, p);
+       array_declaration = array_definition->declaration;
+       elem = array_declaration->elem;
+       if (elem->id == CTF_TYPE_INTEGER) {
+               struct declaration_integer *integer_declaration =
+                       container_of(elem, struct declaration_integer, p);
+
+               if (integer_declaration->encoding == CTF_STRING_UTF8
+                               || integer_declaration->encoding == CTF_STRING_ASCII) {
+
+                       if (integer_declaration->len == CHAR_BIT
+                                       && integer_declaration->p.alignment == CHAR_BIT) {
+
+                               return array_definition->string;
+                       }
+               }
+       }
+       fprintf(stderr, "[warning] Extracting string\n");
+       return NULL;
+}
index e55c03e8a3cb353a96aff3019d700e573584f278..9e6df588f9764537a9969a0937274b941fa70aa8 100644 (file)
@@ -103,3 +103,33 @@ void _integer_definition_free(struct definition *definition)
        declaration_unref(integer->p.declaration);
        g_free(integer);
 }
+
+uint64_t get_unsigned_int(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;
+
+       if (!integer_declaration->signedness) {
+               return integer_definition->value._unsigned;
+       }
+       fprintf(stderr, "[warning] Extracting unsigned value in a signed int\n");
+       return (uint64_t)integer_definition->value._signed;
+}
+
+int64_t get_signed_int(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;
+
+       if (integer_declaration->signedness) {
+               return integer_definition->value._signed;
+       }
+       fprintf(stderr, "[warning] Extracting signed value in an unsigned int\n");
+       return (int64_t)integer_definition->value._unsigned;
+}
This page took 0.035579 seconds and 4 git commands to generate.