Commit | Line | Data |
---|---|---|
1ae19169 MD |
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> | |
4d5fc303 | 23 | #include <babeltrace/bitfield.h> |
1ae19169 | 24 | |
c5e74408 | 25 | int ctf_text_integer_write(struct stream_pos *ppos, struct definition *definition) |
1ae19169 MD |
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 | ||
d335f0f7 MD |
33 | if (!print_field(definition)) |
34 | return 0; | |
35 | ||
1ae19169 | 36 | if (pos->dummy) |
c5e74408 | 37 | return 0; |
31262354 | 38 | |
fd3382e8 | 39 | if (pos->field_nr++ != 0) |
31262354 | 40 | fprintf(pos->fp, ","); |
fd3382e8 | 41 | fprintf(pos->fp, " "); |
31262354 MD |
42 | if (pos->print_names) |
43 | fprintf(pos->fp, "%s = ", | |
44 | g_quark_to_string(definition->name)); | |
45 | ||
720d3e65 MD |
46 | if (pos->string |
47 | && (integer_declaration->encoding == CTF_STRING_ASCII | |
48 | || integer_declaration->encoding == CTF_STRING_UTF8)) { | |
81dee1bb MD |
49 | |
50 | if (!integer_declaration->signedness) { | |
51 | g_string_append_c(pos->string, | |
52 | (int) integer_definition->value._unsigned); | |
53 | } else { | |
54 | g_string_append_c(pos->string, | |
55 | (int) integer_definition->value._signed); | |
56 | } | |
57 | return 0; | |
58 | } | |
59 | ||
164078da | 60 | switch (integer_declaration->base) { |
4d5fc303 MD |
61 | case 0: /* default */ |
62 | case 10: | |
63 | if (!integer_declaration->signedness) { | |
64 | fprintf(pos->fp, "%" PRIu64, | |
65 | integer_definition->value._unsigned); | |
66 | } else { | |
67 | fprintf(pos->fp, "%" PRId64, | |
68 | integer_definition->value._signed); | |
69 | } | |
70 | break; | |
164078da MD |
71 | case 2: |
72 | { | |
73 | int bitnr; | |
4d5fc303 MD |
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; | |
31262354 | 80 | |
db393391 | 81 | fprintf(pos->fp, "0b"); |
4d5fc303 MD |
82 | v = _bt_piecewise_lshift(v, 64 - integer_declaration->len); |
83 | for (bitnr = 0; bitnr < integer_declaration->len; bitnr++) { | |
84 | fprintf(pos->fp, "%u", (v & (1ULL << 63)) ? 1 : 0); | |
85 | v = _bt_piecewise_lshift(v, 1); | |
164078da MD |
86 | } |
87 | break; | |
88 | } | |
89 | case 8: | |
4d5fc303 MD |
90 | { |
91 | uint64_t v; | |
92 | ||
93 | if (!integer_declaration->signedness) | |
94 | v = integer_definition->value._unsigned; | |
95 | else | |
96 | v = (uint64_t) integer_definition->value._signed; | |
97 | ||
164078da MD |
98 | fprintf(pos->fp, "0%" PRIo64, |
99 | integer_definition->value._unsigned); | |
100 | break; | |
4d5fc303 | 101 | } |
164078da | 102 | case 16: |
4d5fc303 MD |
103 | { |
104 | uint64_t v; | |
105 | ||
106 | if (!integer_declaration->signedness) | |
107 | v = integer_definition->value._unsigned; | |
108 | else | |
109 | v = (uint64_t) integer_definition->value._signed; | |
110 | ||
164078da | 111 | fprintf(pos->fp, "0x%" PRIX64, |
1ae19169 | 112 | integer_definition->value._unsigned); |
164078da | 113 | break; |
4d5fc303 | 114 | } |
164078da MD |
115 | default: |
116 | return -EINVAL; | |
1ae19169 | 117 | } |
164078da | 118 | |
c5e74408 | 119 | return 0; |
1ae19169 | 120 | } |