2 * String printing Visitor
4 * Copyright Red Hat, Inc. 2012
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"
19 #include "qemu/range.h"
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
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. */
54 typedef enum ListMode ListMode;
56 struct StringOutputVisitor
66 } range_start, range_end;
70 static StringOutputVisitor *to_sov(Visitor *v)
72 return container_of(v, StringOutputVisitor, visitor);
75 static void string_output_set(StringOutputVisitor *sov, char *string)
78 g_string_free(sov->string, true);
80 sov->string = g_string_new(string);
84 static void string_output_append(StringOutputVisitor *sov, int64_t a)
86 Range *r = g_malloc0(sizeof(*r));
89 sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare);
92 static void string_output_append_range(StringOutputVisitor *sov,
95 Range *r = g_malloc0(sizeof(*r));
98 sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare);
101 static void format_string(StringOutputVisitor *sov, Range *r, bool next,
104 if (r->end - r->begin > 1) {
106 g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64,
107 r->begin, r->end - 1);
110 g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64,
111 r->begin, r->end - 1);
115 g_string_append_printf(sov->string, "0x%" PRIx64, r->begin);
117 g_string_append_printf(sov->string, "%" PRId64, r->begin);
121 g_string_append(sov->string, ",");
125 static void print_type_int64(Visitor *v, int64_t *obj, const char *name,
128 StringOutputVisitor *sov = to_sov(v);
131 switch (sov->list_mode) {
133 string_output_append(sov, *obj);
137 sov->range_start.s = *obj;
138 sov->range_end.s = *obj;
139 sov->list_mode = LM_IN_PROGRESS;
143 if (sov->range_end.s + 1 == *obj) {
146 if (sov->range_start.s == sov->range_end.s) {
147 string_output_append(sov, sov->range_end.s);
149 assert(sov->range_start.s < sov->range_end.s);
150 string_output_append_range(sov, sov->range_start.s,
154 sov->range_start.s = *obj;
155 sov->range_end.s = *obj;
160 if (sov->range_end.s + 1 == *obj) {
162 assert(sov->range_start.s < sov->range_end.s);
163 string_output_append_range(sov, sov->range_start.s,
166 if (sov->range_start.s == sov->range_end.s) {
167 string_output_append(sov, sov->range_end.s);
169 assert(sov->range_start.s < sov->range_end.s);
171 string_output_append_range(sov, sov->range_start.s,
174 string_output_append(sov, *obj);
185 format_string(sov, r, l->next != NULL, false);
191 g_string_append(sov->string, " (");
194 format_string(sov, r, l->next != NULL, true);
197 g_string_append(sov->string, ")");
201 static void print_type_uint64(Visitor *v, uint64_t *obj, const char *name,
204 /* FIXME: print_type_int64 mishandles values over INT64_MAX */
206 print_type_int64(v, &i, name, errp);
209 static void print_type_size(Visitor *v, uint64_t *obj, const char *name,
212 StringOutputVisitor *sov = to_sov(v);
213 static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
219 out = g_strdup_printf("%"PRIu64, *obj);
220 string_output_set(sov, out);
226 /* The exponent (returned in i) minus one gives us
227 * floor(log2(val * 1024 / 1000). The correction makes us
228 * switch to the higher power when the integer part is >= 1000.
230 frexp(val / (1000.0 / 1024.0), &i);
232 assert(i < ARRAY_SIZE(suffixes));
233 div = 1ULL << (i * 10);
235 out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val,
236 (double)val/div, suffixes[i], i ? "iB" : "");
237 string_output_set(sov, out);
240 static void print_type_bool(Visitor *v, bool *obj, const char *name,
243 StringOutputVisitor *sov = to_sov(v);
244 string_output_set(sov, g_strdup(*obj ? "true" : "false"));
247 static void print_type_str(Visitor *v, char **obj, const char *name,
250 StringOutputVisitor *sov = to_sov(v);
254 out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
256 out = g_strdup(*obj ? *obj : "");
258 string_output_set(sov, out);
261 static void print_type_number(Visitor *v, double *obj, const char *name,
264 StringOutputVisitor *sov = to_sov(v);
265 string_output_set(sov, g_strdup_printf("%f", *obj));
269 start_list(Visitor *v, const char *name, Error **errp)
271 StringOutputVisitor *sov = to_sov(v);
273 /* we can't traverse a list in a list */
274 assert(sov->list_mode == LM_NONE);
275 sov->list_mode = LM_STARTED;
280 next_list(Visitor *v, GenericList **list, Error **errp)
282 StringOutputVisitor *sov = to_sov(v);
283 GenericList *ret = NULL;
292 if (ret && ret->next == NULL) {
293 sov->list_mode = LM_NONE;
297 if (ret && ret->next == NULL) {
298 sov->list_mode = LM_END;
307 end_list(Visitor *v, Error **errp)
309 StringOutputVisitor *sov = to_sov(v);
311 assert(sov->list_mode == LM_STARTED ||
312 sov->list_mode == LM_END ||
313 sov->list_mode == LM_NONE ||
314 sov->list_mode == LM_IN_PROGRESS);
315 sov->list_mode = LM_NONE;
320 char *string_output_get_string(StringOutputVisitor *sov)
322 char *string = g_string_free(sov->string, false);
327 Visitor *string_output_get_visitor(StringOutputVisitor *sov)
329 return &sov->visitor;
332 static void free_range(void *range, void *dummy)
337 void string_output_visitor_cleanup(StringOutputVisitor *sov)
340 g_string_free(sov->string, true);
343 g_list_foreach(sov->ranges, free_range, NULL);
344 g_list_free(sov->ranges);
348 StringOutputVisitor *string_output_visitor_new(bool human)
350 StringOutputVisitor *v;
352 v = g_malloc0(sizeof(*v));
354 v->string = g_string_new(NULL);
356 v->visitor.type_enum = output_type_enum;
357 v->visitor.type_int64 = print_type_int64;
358 v->visitor.type_uint64 = print_type_uint64;
359 v->visitor.type_size = print_type_size;
360 v->visitor.type_bool = print_type_bool;
361 v->visitor.type_str = print_type_str;
362 v->visitor.type_number = print_type_number;
363 v->visitor.start_list = start_list;
364 v->visitor.next_list = next_list;
365 v->visitor.end_list = end_list;