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 | ||
33 | if (pos->dummy) | |
c5e74408 | 34 | return 0; |
31262354 | 35 | |
fd3382e8 | 36 | if (pos->field_nr++ != 0) |
31262354 | 37 | fprintf(pos->fp, ","); |
fd3382e8 | 38 | fprintf(pos->fp, " "); |
31262354 MD |
39 | if (pos->print_names) |
40 | fprintf(pos->fp, "%s = ", | |
41 | g_quark_to_string(definition->name)); | |
42 | ||
164078da | 43 | switch (integer_declaration->base) { |
4d5fc303 MD |
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; | |
164078da MD |
54 | case 2: |
55 | { | |
56 | int bitnr; | |
4d5fc303 MD |
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; | |
31262354 | 63 | |
164078da | 64 | fprintf(pos->fp, "b"); |
4d5fc303 MD |
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); | |
164078da MD |
69 | } |
70 | break; | |
71 | } | |
72 | case 8: | |
4d5fc303 MD |
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 | ||
164078da MD |
81 | fprintf(pos->fp, "0%" PRIo64, |
82 | integer_definition->value._unsigned); | |
83 | break; | |
4d5fc303 | 84 | } |
164078da | 85 | case 16: |
4d5fc303 MD |
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 | ||
164078da | 94 | fprintf(pos->fp, "0x%" PRIX64, |
1ae19169 | 95 | integer_definition->value._unsigned); |
164078da | 96 | break; |
4d5fc303 | 97 | } |
164078da MD |
98 | default: |
99 | return -EINVAL; | |
1ae19169 | 100 | } |
164078da | 101 | |
c5e74408 | 102 | return 0; |
1ae19169 | 103 | } |