Use 0b prefix for binary pretty print
[babeltrace.git] / formats / ctf-text / types / integer.c
1 /*
2 * Common Trace Format
3 *
4 * Integers read/write functions.
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/ctf-text/types.h>
20 #include <stdio.h>
21 #include <inttypes.h>
22 #include <stdint.h>
23 #include <babeltrace/bitfield.h>
24
25 int ctf_text_integer_write(struct stream_pos *ppos, struct definition *definition)
26 {
27 struct definition_integer *integer_definition =
28 container_of(definition, struct definition_integer, p);
29 const struct declaration_integer *integer_declaration =
30 integer_definition->declaration;
31 struct ctf_text_stream_pos *pos = ctf_text_pos(ppos);
32
33 if (pos->dummy)
34 return 0;
35
36 if (pos->field_nr++ != 0)
37 fprintf(pos->fp, ",");
38 fprintf(pos->fp, " ");
39 if (pos->print_names)
40 fprintf(pos->fp, "%s = ",
41 g_quark_to_string(definition->name));
42
43 switch (integer_declaration->base) {
44 case 0: /* default */
45 case 10:
46 if (!integer_declaration->signedness) {
47 fprintf(pos->fp, "%" PRIu64,
48 integer_definition->value._unsigned);
49 } else {
50 fprintf(pos->fp, "%" PRId64,
51 integer_definition->value._signed);
52 }
53 break;
54 case 2:
55 {
56 int bitnr;
57 uint64_t v;
58
59 if (!integer_declaration->signedness)
60 v = integer_definition->value._unsigned;
61 else
62 v = (uint64_t) integer_definition->value._signed;
63
64 fprintf(pos->fp, "0b");
65 v = _bt_piecewise_lshift(v, 64 - integer_declaration->len);
66 for (bitnr = 0; bitnr < integer_declaration->len; bitnr++) {
67 fprintf(pos->fp, "%u", (v & (1ULL << 63)) ? 1 : 0);
68 v = _bt_piecewise_lshift(v, 1);
69 }
70 break;
71 }
72 case 8:
73 {
74 uint64_t v;
75
76 if (!integer_declaration->signedness)
77 v = integer_definition->value._unsigned;
78 else
79 v = (uint64_t) integer_definition->value._signed;
80
81 fprintf(pos->fp, "0%" PRIo64,
82 integer_definition->value._unsigned);
83 break;
84 }
85 case 16:
86 {
87 uint64_t v;
88
89 if (!integer_declaration->signedness)
90 v = integer_definition->value._unsigned;
91 else
92 v = (uint64_t) integer_definition->value._signed;
93
94 fprintf(pos->fp, "0x%" PRIX64,
95 integer_definition->value._unsigned);
96 break;
97 }
98 default:
99 return -EINVAL;
100 }
101
102 return 0;
103 }
This page took 0.031734 seconds and 4 git commands to generate.