Sequence: add missing handling of integer encoding
[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 if (pos->string
44 && (integer_declaration->encoding == CTF_STRING_ASCII
45 || integer_declaration->encoding == CTF_STRING_UTF8)) {
46
47 if (!integer_declaration->signedness) {
48 g_string_append_c(pos->string,
49 (int) integer_definition->value._unsigned);
50 } else {
51 g_string_append_c(pos->string,
52 (int) integer_definition->value._signed);
53 }
54 return 0;
55 }
56
57 switch (integer_declaration->base) {
58 case 0: /* default */
59 case 10:
60 if (!integer_declaration->signedness) {
61 fprintf(pos->fp, "%" PRIu64,
62 integer_definition->value._unsigned);
63 } else {
64 fprintf(pos->fp, "%" PRId64,
65 integer_definition->value._signed);
66 }
67 break;
68 case 2:
69 {
70 int bitnr;
71 uint64_t v;
72
73 if (!integer_declaration->signedness)
74 v = integer_definition->value._unsigned;
75 else
76 v = (uint64_t) integer_definition->value._signed;
77
78 fprintf(pos->fp, "0b");
79 v = _bt_piecewise_lshift(v, 64 - integer_declaration->len);
80 for (bitnr = 0; bitnr < integer_declaration->len; bitnr++) {
81 fprintf(pos->fp, "%u", (v & (1ULL << 63)) ? 1 : 0);
82 v = _bt_piecewise_lshift(v, 1);
83 }
84 break;
85 }
86 case 8:
87 {
88 uint64_t v;
89
90 if (!integer_declaration->signedness)
91 v = integer_definition->value._unsigned;
92 else
93 v = (uint64_t) integer_definition->value._signed;
94
95 fprintf(pos->fp, "0%" PRIo64,
96 integer_definition->value._unsigned);
97 break;
98 }
99 case 16:
100 {
101 uint64_t v;
102
103 if (!integer_declaration->signedness)
104 v = integer_definition->value._unsigned;
105 else
106 v = (uint64_t) integer_definition->value._signed;
107
108 fprintf(pos->fp, "0x%" PRIX64,
109 integer_definition->value._unsigned);
110 break;
111 }
112 default:
113 return -EINVAL;
114 }
115
116 return 0;
117 }
This page took 0.030917 seconds and 4 git commands to generate.