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