CTF: fix packet indexing
[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 (!print_field(definition))
34 return 0;
35
36 if (pos->dummy)
37 return 0;
38
39 if (pos->field_nr++ != 0)
40 fprintf(pos->fp, ",");
41 fprintf(pos->fp, " ");
42 if (pos->print_names)
43 fprintf(pos->fp, "%s = ",
44 g_quark_to_string(definition->name));
45
46 if (pos->string
47 && (integer_declaration->encoding == CTF_STRING_ASCII
48 || integer_declaration->encoding == CTF_STRING_UTF8)) {
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
60 switch (integer_declaration->base) {
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;
71 case 2:
72 {
73 int bitnr;
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, "0b");
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);
86 }
87 break;
88 }
89 case 8:
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
98 fprintf(pos->fp, "0%" PRIo64,
99 integer_definition->value._unsigned);
100 break;
101 }
102 case 16:
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
111 fprintf(pos->fp, "0x%" PRIX64,
112 integer_definition->value._unsigned);
113 break;
114 }
115 default:
116 return -EINVAL;
117 }
118
119 return 0;
120 }
This page took 0.031026 seconds and 4 git commands to generate.