Add base integer attribute, and use it in ctf-text
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 13 May 2011 23:35:43 +0000 (19:35 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 13 May 2011 23:35:43 +0000 (19:35 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
formats/ctf-text/types/integer.c
formats/ctf/metadata/ctf-visitor-generate-io-struct.c
include/babeltrace/types.h
tests/ctf-traces/fail/fail1/dummystream
tests/ctf-traces/fail/fail1/metadata
tests/ctf-traces/succeed/succeed2/metadata
types/float.c
types/integer.c

index 8b2df2c005a2f2e65b826d75dd422c77dac98a2b..a01284583f6ecdd64e83cc2a9d34c31b00bc0a8f 100644 (file)
@@ -39,20 +39,41 @@ int ctf_text_integer_write(struct stream_pos *ppos, struct definition *definitio
                fprintf(pos->fp, "%s = ",
                        g_quark_to_string(definition->name));
 
-       if (!compare_definition_path(definition, g_quark_from_static_string("stream.event.header.timestamp"))) {
-               fprintf(pos->fp, "%" PRIu64,
-                       integer_definition->value._unsigned);
-               return 0;
-       }
+       switch (integer_declaration->base) {
+       case 2:
+       {
+               int bitnr;
+               uint64_t v = integer_definition->value._unsigned;
 
-       if (!integer_declaration->signedness) {
-               fprintf(pos->fp, "%" PRIu64" (0x%" PRIX64 ")",
-                       integer_definition->value._unsigned,
+               fprintf(pos->fp, "b");
+               for (bitnr = 0; bitnr < integer_declaration->len; bitnr++)
+                       v <<= 1;
+               for (; bitnr < sizeof(v) * CHAR_BIT; bitnr++) {
+                       fprintf(pos->fp, "%u", ((v & 1ULL) << 63) ? 1 : 0);
+                       v <<= 1;
+               }
+               break;
+       }
+       case 8:
+               fprintf(pos->fp, "0%" PRIo64,
+                       integer_definition->value._unsigned);
+               break;
+       case 10:
+               if (!integer_declaration->signedness) {
+                       fprintf(pos->fp, "%" PRIu64,
+                               integer_definition->value._unsigned);
+               } else {
+                       fprintf(pos->fp, "%" PRId64,
+                               integer_definition->value._signed);
+               }
+               break;
+       case 16:
+               fprintf(pos->fp, "0x%" PRIX64,
                        integer_definition->value._unsigned);
-       } else {
-               fprintf(pos->fp, "%" PRId64" (0x%" PRIX64 ")",
-                       integer_definition->value._signed,
-                       integer_definition->value._signed);
+               break;
+       default:
+               return -EINVAL;
        }
+
        return 0;
 }
index 23734284678996cc0ee0ddd16e4b82df2dc042e2..de760ca019f1e863f9f26e49989fd172c4e8bc47 100644 (file)
@@ -1069,6 +1069,7 @@ struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth,
        int byte_order = trace->byte_order;
        int signedness = 0;
        int has_alignment = 0, has_size = 0;
+       int base = 10;
        struct declaration_integer *integer_declaration;
 
        cds_list_for_each_entry(expression, expressions, siblings) {
@@ -1107,6 +1108,56 @@ struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth,
                                return NULL;
                        }
                        has_alignment = 1;
+               } else if (!strcmp(left->u.unary_expression.u.string, "base")) {
+                       switch (right->u.unary_expression.type) {
+                       case UNARY_UNSIGNED_CONSTANT:
+                               switch (right->u.unary_expression.u.unsigned_constant) {
+                               case 2: 
+                               case 8:
+                               case 10:
+                               case 16:
+                                       base = right->u.unary_expression.u.unsigned_constant;
+                                       break;
+                               default:
+                                       fprintf(fd, "[error] %s: base not supported (%" PRIu64 ")\n",
+                                               __func__, right->u.unary_expression.u.unsigned_constant);
+                               return NULL;
+                               }
+                               break;
+                       case UNARY_STRING:
+                       {
+                               char *s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
+                               if (!s_right) {
+                                       fprintf(fd, "[error] %s: unexpected unary expression for integer base\n", __func__);
+                                       g_free(s_right);
+                                       return NULL;
+                               }
+                               if (!strcmp(s_right, "decimal") || !strcmp(s_right, "dec") || !strcmp(s_right, "d")
+                                   || !strcmp(s_right, "i") || !strcmp(s_right, "u")) {
+                                       base = 10;
+                               } else if (!strcmp(s_right, "hexadecimal") || !strcmp(s_right, "hex")
+                                   || !strcmp(s_right, "x") || !strcmp(s_right, "X")
+                                   || !strcmp(s_right, "p")) {
+                                       base = 16;
+                               } else if (!strcmp(s_right, "octal") || !strcmp(s_right, "oct")
+                                   || !strcmp(s_right, "o")) {
+                                       base = 8;
+                               } else if (!strcmp(s_right, "binary") || !strcmp(s_right, "b")) {
+                                       base = 2;
+                               } else {
+                                       fprintf(fd, "[error] %s: unexpected expression for integer base (%s)\n", __func__, s_right);
+                                       g_free(s_right);
+                                       return NULL;
+                               }
+
+                               g_free(s_right);
+                               break;
+                       }
+                       default:
+                               fprintf(fd, "[error] %s: base: expecting unsigned constant or unary string\n",
+                                       __func__);
+                               return NULL;
+                       }
                } else {
                        fprintf(fd, "[error] %s: unknown attribute name %s\n",
                                __func__, left->u.unary_expression.u.string);
@@ -1127,7 +1178,7 @@ struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth,
                }
        }
        integer_declaration = integer_declaration_new(size,
-                               byte_order, signedness, alignment);
+                               byte_order, signedness, alignment, base);
        return &integer_declaration->p;
 }
 
index a4dcdee933be4f19dc9fda9d684534ca85f556c3..33311a683dae1056ff48928b4f4f0e3f83a788a4 100644 (file)
@@ -138,6 +138,7 @@ struct declaration_integer {
        size_t len;             /* length, in bits. */
        int byte_order;         /* byte order */
        int signedness;
+       int base;               /* Base for pretty-printing: 2, 8, 10, 16 */
 };
 
 struct definition_integer {
@@ -378,7 +379,8 @@ void definition_ref(struct definition *definition);
 void definition_unref(struct definition *definition);
 
 struct declaration_integer *integer_declaration_new(size_t len, int byte_order,
-                                     int signedness, size_t alignment);
+                                 int signedness, size_t alignment,
+                                 int base);
 
 /*
  * mantissa_len is the length of the number of bytes represented by the mantissa
index 87d8c06d2c82b37646e9f50941b28bcd985cb05c..26010671613e67c00fcd3c4c8de8e216834a637f 100644 (file)
Binary files a/tests/ctf-traces/fail/fail1/dummystream and b/tests/ctf-traces/fail/fail1/dummystream differ
index 9e34537a1c885b61adf411c66e13ace310ca4dc5..ffdf52c5e9f757d3fde1f2e27feb74d949c98169 100644 (file)
@@ -4,8 +4,8 @@ typealias integer { size = 1; align = 1; signed = false; } := uint1_t;
 typealias integer { size = 8; align = 8; signed = false; } := uint8_t;
 typealias integer { size = 63; align = 1; signed = false; } := timestamp_t;
 
-typealias integer { size = 32; align = 32; signed = false; } := uint32_t;
-typealias integer { size = 32; align = 32; signed = false; } := void *;
+typealias integer { size = 32; align = 32; signed = false; base = 10; } := uint32_t;
+typealias integer { size = 32; align = 32; signed = false; base = hex; } := void *;
 
 
 trace {
index c8cf5d218e34a2c9e67a97b753b5f7c7ab36b227..9a2958d05735a9e0628ee79cb2b8baec873d2d99 100644 (file)
@@ -1,5 +1,5 @@
-typealias integer { size = 8; align = 8; signed = false; } := uint8_t;
-typealias integer { size = 32; align = 32; signed = false; } := uint32_t;
+typealias integer { size = 8; align = 8; signed = false; base = 10; } := uint8_t;
+typealias integer { size = 32; align = 32; signed = false; base = hex; } := uint32_t;
 
 trace {
        major = 0;
index 5ce6bb43628cfa907c32228be83c03696de8d30a..2306d94f1b17cecd48df03b89343efe507fe8256 100644 (file)
@@ -57,11 +57,11 @@ struct declaration_float *
        float_declaration->byte_order = byte_order;
 
        float_declaration->sign = integer_declaration_new(1,
-                                           byte_order, false, 1);
+                                           byte_order, false, 1, 2);
        float_declaration->mantissa = integer_declaration_new(mantissa_len - 1,
-                                               byte_order, false, 1);
+                                               byte_order, false, 1, 10);
        float_declaration->exp = integer_declaration_new(exp_len,
-                                          byte_order, true, 1);
+                                          byte_order, true, 1, 10);
        return float_declaration;
 }
 
index 5aa9c44f2440ae93118cc66cd1e1a4143e8c9265..059c6d898b7951e31951e35b3bd7e5d3be9eac05 100644 (file)
@@ -38,7 +38,7 @@ void _integer_declaration_free(struct declaration *declaration)
 
 struct declaration_integer *
        integer_declaration_new(size_t len, int byte_order,
-                        int signedness, size_t alignment)
+                        int signedness, size_t alignment, int base)
 {
        struct declaration_integer *integer_declaration;
 
@@ -52,6 +52,7 @@ struct declaration_integer *
        integer_declaration->len = len;
        integer_declaration->byte_order = byte_order;
        integer_declaration->signedness = signedness;
+       integer_declaration->base = base;
        return integer_declaration;
 }
 
This page took 0.030159 seconds and 4 git commands to generate.