2 * String printing Visitor
4 * Copyright Red Hat, Inc. 2012-2016
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"
19 #include "qemu/range.h"
22 LM_NONE
, /* not traversing a list of repeated options */
23 LM_STARTED
, /* next_list() ready to be called */
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. */
51 LM_END
, /* next_list() called, about to see last element. */
54 typedef enum ListMode ListMode
;
56 struct StringOutputVisitor
65 } range_start
, range_end
;
69 static StringOutputVisitor
*to_sov(Visitor
*v
)
71 return container_of(v
, StringOutputVisitor
, visitor
);
74 static void string_output_set(StringOutputVisitor
*sov
, char *string
)
77 g_string_free(sov
->string
, true);
79 sov
->string
= g_string_new(string
);
83 static void string_output_append(StringOutputVisitor
*sov
, int64_t a
)
85 Range
*r
= g_malloc0(sizeof(*r
));
88 sov
->ranges
= range_list_insert(sov
->ranges
, r
);
91 static void string_output_append_range(StringOutputVisitor
*sov
,
94 Range
*r
= g_malloc0(sizeof(*r
));
97 sov
->ranges
= range_list_insert(sov
->ranges
, r
);
100 static void format_string(StringOutputVisitor
*sov
, Range
*r
, bool next
,
103 if (r
->end
- r
->begin
> 1) {
105 g_string_append_printf(sov
->string
, "0x%" PRIx64
"-0x%" PRIx64
,
106 r
->begin
, r
->end
- 1);
109 g_string_append_printf(sov
->string
, "%" PRId64
"-%" PRId64
,
110 r
->begin
, r
->end
- 1);
114 g_string_append_printf(sov
->string
, "0x%" PRIx64
, r
->begin
);
116 g_string_append_printf(sov
->string
, "%" PRId64
, r
->begin
);
120 g_string_append(sov
->string
, ",");
124 static void print_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
127 StringOutputVisitor
*sov
= to_sov(v
);
130 switch (sov
->list_mode
) {
132 string_output_append(sov
, *obj
);
136 sov
->range_start
.s
= *obj
;
137 sov
->range_end
.s
= *obj
;
138 sov
->list_mode
= LM_IN_PROGRESS
;
142 if (sov
->range_end
.s
+ 1 == *obj
) {
145 if (sov
->range_start
.s
== sov
->range_end
.s
) {
146 string_output_append(sov
, sov
->range_end
.s
);
148 assert(sov
->range_start
.s
< sov
->range_end
.s
);
149 string_output_append_range(sov
, sov
->range_start
.s
,
153 sov
->range_start
.s
= *obj
;
154 sov
->range_end
.s
= *obj
;
159 if (sov
->range_end
.s
+ 1 == *obj
) {
161 assert(sov
->range_start
.s
< sov
->range_end
.s
);
162 string_output_append_range(sov
, sov
->range_start
.s
,
165 if (sov
->range_start
.s
== sov
->range_end
.s
) {
166 string_output_append(sov
, sov
->range_end
.s
);
168 assert(sov
->range_start
.s
< sov
->range_end
.s
);
170 string_output_append_range(sov
, sov
->range_start
.s
,
173 string_output_append(sov
, *obj
);
184 format_string(sov
, r
, l
->next
!= NULL
, false);
190 g_string_append(sov
->string
, " (");
193 format_string(sov
, r
, l
->next
!= NULL
, true);
196 g_string_append(sov
->string
, ")");
200 static void print_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
203 /* FIXME: print_type_int64 mishandles values over INT64_MAX */
205 print_type_int64(v
, name
, &i
, errp
);
208 static void print_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
211 StringOutputVisitor
*sov
= to_sov(v
);
212 static const char suffixes
[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
218 out
= g_strdup_printf("%"PRIu64
, *obj
);
219 string_output_set(sov
, out
);
225 /* The exponent (returned in i) minus one gives us
226 * floor(log2(val * 1024 / 1000). The correction makes us
227 * switch to the higher power when the integer part is >= 1000.
229 frexp(val
/ (1000.0 / 1024.0), &i
);
231 assert(i
< ARRAY_SIZE(suffixes
));
232 div
= 1ULL << (i
* 10);
234 out
= g_strdup_printf("%"PRIu64
" (%0.3g %c%s)", val
,
235 (double)val
/div
, suffixes
[i
], i
? "iB" : "");
236 string_output_set(sov
, out
);
239 static void print_type_bool(Visitor
*v
, const char *name
, bool *obj
,
242 StringOutputVisitor
*sov
= to_sov(v
);
243 string_output_set(sov
, g_strdup(*obj
? "true" : "false"));
246 static void print_type_str(Visitor
*v
, const char *name
, char **obj
,
249 StringOutputVisitor
*sov
= to_sov(v
);
253 out
= *obj
? g_strdup_printf("\"%s\"", *obj
) : g_strdup("<null>");
255 out
= g_strdup(*obj
? *obj
: "");
257 string_output_set(sov
, out
);
260 static void print_type_number(Visitor
*v
, const char *name
, double *obj
,
263 StringOutputVisitor
*sov
= to_sov(v
);
264 string_output_set(sov
, g_strdup_printf("%f", *obj
));
268 start_list(Visitor
*v
, const char *name
, GenericList
**list
, size_t size
,
271 StringOutputVisitor
*sov
= to_sov(v
);
273 /* we can't traverse a list in a list */
274 assert(sov
->list_mode
== LM_NONE
);
275 /* We don't support visits without a list */
277 /* List handling is only needed if there are at least two elements */
278 if (*list
&& (*list
)->next
) {
279 sov
->list_mode
= LM_STARTED
;
283 static GenericList
*next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
285 StringOutputVisitor
*sov
= to_sov(v
);
286 GenericList
*ret
= tail
->next
;
288 if (ret
&& !ret
->next
) {
289 sov
->list_mode
= LM_END
;
294 static void end_list(Visitor
*v
)
296 StringOutputVisitor
*sov
= to_sov(v
);
298 assert(sov
->list_mode
== LM_STARTED
||
299 sov
->list_mode
== LM_END
||
300 sov
->list_mode
== LM_NONE
||
301 sov
->list_mode
== LM_IN_PROGRESS
);
302 sov
->list_mode
= LM_NONE
;
305 char *string_output_get_string(StringOutputVisitor
*sov
)
307 char *string
= g_string_free(sov
->string
, false);
312 Visitor
*string_output_get_visitor(StringOutputVisitor
*sov
)
314 return &sov
->visitor
;
317 static void free_range(void *range
, void *dummy
)
322 void string_output_visitor_cleanup(StringOutputVisitor
*sov
)
325 g_string_free(sov
->string
, true);
328 g_list_foreach(sov
->ranges
, free_range
, NULL
);
329 g_list_free(sov
->ranges
);
333 StringOutputVisitor
*string_output_visitor_new(bool human
)
335 StringOutputVisitor
*v
;
337 v
= g_malloc0(sizeof(*v
));
339 v
->string
= g_string_new(NULL
);
341 v
->visitor
.type
= VISITOR_OUTPUT
;
342 v
->visitor
.type_int64
= print_type_int64
;
343 v
->visitor
.type_uint64
= print_type_uint64
;
344 v
->visitor
.type_size
= print_type_size
;
345 v
->visitor
.type_bool
= print_type_bool
;
346 v
->visitor
.type_str
= print_type_str
;
347 v
->visitor
.type_number
= print_type_number
;
348 v
->visitor
.start_list
= start_list
;
349 v
->visitor
.next_list
= next_list
;
350 v
->visitor
.end_list
= end_list
;