Fix gcc 4.6 warnings
[babeltrace.git] / formats / ctf-text / types / integer.c
CommitLineData
1ae19169
MD
1/*
2 * Common Trace Format
3 *
4 * Integers read/write functions.
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
1ae19169
MD
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>
4d5fc303 25#include <babeltrace/bitfield.h>
1ae19169 26
c5e74408 27int ctf_text_integer_write(struct stream_pos *ppos, struct definition *definition)
1ae19169
MD
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
d335f0f7
MD
35 if (!print_field(definition))
36 return 0;
37
1ae19169 38 if (pos->dummy)
c5e74408 39 return 0;
31262354 40
fd3382e8 41 if (pos->field_nr++ != 0)
31262354 42 fprintf(pos->fp, ",");
fd3382e8 43 fprintf(pos->fp, " ");
31262354
MD
44 if (pos->print_names)
45 fprintf(pos->fp, "%s = ",
46 g_quark_to_string(definition->name));
47
720d3e65
MD
48 if (pos->string
49 && (integer_declaration->encoding == CTF_STRING_ASCII
50 || integer_declaration->encoding == CTF_STRING_UTF8)) {
81dee1bb
MD
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
164078da 62 switch (integer_declaration->base) {
4d5fc303
MD
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;
164078da
MD
73 case 2:
74 {
75 int bitnr;
4d5fc303
MD
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;
31262354 82
db393391 83 fprintf(pos->fp, "0b");
4d5fc303
MD
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);
164078da
MD
88 }
89 break;
90 }
91 case 8:
4d5fc303
MD
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
5c551b40 100 fprintf(pos->fp, "0%" PRIo64, v);
164078da 101 break;
4d5fc303 102 }
164078da 103 case 16:
4d5fc303
MD
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
5c551b40 112 fprintf(pos->fp, "0x%" PRIX64, v);
164078da 113 break;
4d5fc303 114 }
164078da
MD
115 default:
116 return -EINVAL;
1ae19169 117 }
164078da 118
c5e74408 119 return 0;
1ae19169 120}
This page took 0.027716 seconds and 4 git commands to generate.