qapi: Prefer type_int64 over type_int in visitors
[qemu/kevin.git] / qapi / string-output-visitor.c
blobcf8dfc8b48402d6e1b6baad41ad63fb028309d3a
1 /*
2 * String printing Visitor
4 * Copyright Red Hat, Inc. 2012
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
8 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu-common.h"
15 #include "qapi/string-output-visitor.h"
16 #include "qapi/visitor-impl.h"
17 #include "qemu/host-utils.h"
18 #include <math.h>
19 #include "qemu/range.h"
21 enum ListMode {
22 LM_NONE, /* not traversing a list of repeated options */
23 LM_STARTED, /* start_list() succeeded */
25 LM_IN_PROGRESS, /* next_list() has been called.
27 * Generating the next list link will consume the most
28 * recently parsed QemuOpt instance of the repeated
29 * option.
31 * Parsing a value into the list link will examine the
32 * next QemuOpt instance of the repeated option, and
33 * possibly enter LM_SIGNED_INTERVAL or
34 * LM_UNSIGNED_INTERVAL.
37 LM_SIGNED_INTERVAL, /* next_list() has been called.
39 * Generating the next list link will consume the most
40 * recently stored element from the signed interval,
41 * parsed from the most recent QemuOpt instance of the
42 * repeated option. This may consume QemuOpt itself
43 * and return to LM_IN_PROGRESS.
45 * Parsing a value into the list link will store the
46 * next element of the signed interval.
49 LM_UNSIGNED_INTERVAL,/* Same as above, only for an unsigned interval. */
51 LM_END
54 typedef enum ListMode ListMode;
56 struct StringOutputVisitor
58 Visitor visitor;
59 bool human;
60 GString *string;
61 bool head;
62 ListMode list_mode;
63 union {
64 int64_t s;
65 uint64_t u;
66 } range_start, range_end;
67 GList *ranges;
70 static StringOutputVisitor *to_sov(Visitor *v)
72 return container_of(v, StringOutputVisitor, visitor);
75 static void string_output_set(StringOutputVisitor *sov, char *string)
77 if (sov->string) {
78 g_string_free(sov->string, true);
80 sov->string = g_string_new(string);
81 g_free(string);
84 static void string_output_append(StringOutputVisitor *sov, int64_t a)
86 Range *r = g_malloc0(sizeof(*r));
87 r->begin = a;
88 r->end = a + 1;
89 sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare);
92 static void string_output_append_range(StringOutputVisitor *sov,
93 int64_t s, int64_t e)
95 Range *r = g_malloc0(sizeof(*r));
96 r->begin = s;
97 r->end = e + 1;
98 sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare);
101 static void format_string(StringOutputVisitor *sov, Range *r, bool next,
102 bool human)
104 if (r->end - r->begin > 1) {
105 if (human) {
106 g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64,
107 r->begin, r->end - 1);
109 } else {
110 g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64,
111 r->begin, r->end - 1);
113 } else {
114 if (human) {
115 g_string_append_printf(sov->string, "0x%" PRIx64, r->begin);
116 } else {
117 g_string_append_printf(sov->string, "%" PRId64, r->begin);
120 if (next) {
121 g_string_append(sov->string, ",");
125 static void print_type_int64(Visitor *v, int64_t *obj, const char *name,
126 Error **errp)
128 StringOutputVisitor *sov = to_sov(v);
129 GList *l;
131 switch (sov->list_mode) {
132 case LM_NONE:
133 string_output_append(sov, *obj);
134 break;
136 case LM_STARTED:
137 sov->range_start.s = *obj;
138 sov->range_end.s = *obj;
139 sov->list_mode = LM_IN_PROGRESS;
140 return;
142 case LM_IN_PROGRESS:
143 if (sov->range_end.s + 1 == *obj) {
144 sov->range_end.s++;
145 } else {
146 if (sov->range_start.s == sov->range_end.s) {
147 string_output_append(sov, sov->range_end.s);
148 } else {
149 assert(sov->range_start.s < sov->range_end.s);
150 string_output_append_range(sov, sov->range_start.s,
151 sov->range_end.s);
154 sov->range_start.s = *obj;
155 sov->range_end.s = *obj;
157 return;
159 case LM_END:
160 if (sov->range_end.s + 1 == *obj) {
161 sov->range_end.s++;
162 assert(sov->range_start.s < sov->range_end.s);
163 string_output_append_range(sov, sov->range_start.s,
164 sov->range_end.s);
165 } else {
166 if (sov->range_start.s == sov->range_end.s) {
167 string_output_append(sov, sov->range_end.s);
168 } else {
169 assert(sov->range_start.s < sov->range_end.s);
171 string_output_append_range(sov, sov->range_start.s,
172 sov->range_end.s);
174 string_output_append(sov, *obj);
176 break;
178 default:
179 abort();
182 l = sov->ranges;
183 while (l) {
184 Range *r = l->data;
185 format_string(sov, r, l->next != NULL, false);
186 l = l->next;
189 if (sov->human) {
190 l = sov->ranges;
191 g_string_append(sov->string, " (");
192 while (l) {
193 Range *r = l->data;
194 format_string(sov, r, l->next != NULL, true);
195 l = l->next;
197 g_string_append(sov->string, ")");
201 static void print_type_size(Visitor *v, uint64_t *obj, const char *name,
202 Error **errp)
204 StringOutputVisitor *sov = to_sov(v);
205 static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
206 uint64_t div, val;
207 char *out;
208 int i;
210 if (!sov->human) {
211 out = g_strdup_printf("%"PRIu64, *obj);
212 string_output_set(sov, out);
213 return;
216 val = *obj;
218 /* The exponent (returned in i) minus one gives us
219 * floor(log2(val * 1024 / 1000). The correction makes us
220 * switch to the higher power when the integer part is >= 1000.
222 frexp(val / (1000.0 / 1024.0), &i);
223 i = (i - 1) / 10;
224 assert(i < ARRAY_SIZE(suffixes));
225 div = 1ULL << (i * 10);
227 out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val,
228 (double)val/div, suffixes[i], i ? "iB" : "");
229 string_output_set(sov, out);
232 static void print_type_bool(Visitor *v, bool *obj, const char *name,
233 Error **errp)
235 StringOutputVisitor *sov = to_sov(v);
236 string_output_set(sov, g_strdup(*obj ? "true" : "false"));
239 static void print_type_str(Visitor *v, char **obj, const char *name,
240 Error **errp)
242 StringOutputVisitor *sov = to_sov(v);
243 char *out;
245 if (sov->human) {
246 out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
247 } else {
248 out = g_strdup(*obj ? *obj : "");
250 string_output_set(sov, out);
253 static void print_type_number(Visitor *v, double *obj, const char *name,
254 Error **errp)
256 StringOutputVisitor *sov = to_sov(v);
257 string_output_set(sov, g_strdup_printf("%f", *obj));
260 static void
261 start_list(Visitor *v, const char *name, Error **errp)
263 StringOutputVisitor *sov = to_sov(v);
265 /* we can't traverse a list in a list */
266 assert(sov->list_mode == LM_NONE);
267 sov->list_mode = LM_STARTED;
268 sov->head = true;
271 static GenericList *
272 next_list(Visitor *v, GenericList **list, Error **errp)
274 StringOutputVisitor *sov = to_sov(v);
275 GenericList *ret = NULL;
276 if (*list) {
277 if (sov->head) {
278 ret = *list;
279 } else {
280 ret = (*list)->next;
283 if (sov->head) {
284 if (ret && ret->next == NULL) {
285 sov->list_mode = LM_NONE;
287 sov->head = false;
288 } else {
289 if (ret && ret->next == NULL) {
290 sov->list_mode = LM_END;
295 return ret;
298 static void
299 end_list(Visitor *v, Error **errp)
301 StringOutputVisitor *sov = to_sov(v);
303 assert(sov->list_mode == LM_STARTED ||
304 sov->list_mode == LM_END ||
305 sov->list_mode == LM_NONE ||
306 sov->list_mode == LM_IN_PROGRESS);
307 sov->list_mode = LM_NONE;
308 sov->head = true;
312 char *string_output_get_string(StringOutputVisitor *sov)
314 char *string = g_string_free(sov->string, false);
315 sov->string = NULL;
316 return string;
319 Visitor *string_output_get_visitor(StringOutputVisitor *sov)
321 return &sov->visitor;
324 static void free_range(void *range, void *dummy)
326 g_free(range);
329 void string_output_visitor_cleanup(StringOutputVisitor *sov)
331 if (sov->string) {
332 g_string_free(sov->string, true);
335 g_list_foreach(sov->ranges, free_range, NULL);
336 g_list_free(sov->ranges);
337 g_free(sov);
340 StringOutputVisitor *string_output_visitor_new(bool human)
342 StringOutputVisitor *v;
344 v = g_malloc0(sizeof(*v));
346 v->string = g_string_new(NULL);
347 v->human = human;
348 v->visitor.type_enum = output_type_enum;
349 v->visitor.type_int64 = print_type_int64;
350 v->visitor.type_size = print_type_size;
351 v->visitor.type_bool = print_type_bool;
352 v->visitor.type_str = print_type_str;
353 v->visitor.type_number = print_type_number;
354 v->visitor.start_list = start_list;
355 v->visitor.next_list = next_list;
356 v->visitor.end_list = end_list;
358 return v;