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-common.h"
14 #include "qapi/string-output-visitor.h"
15 #include "qapi/visitor-impl.h"
16 #include "qemu/host-utils.h"
18 #include "qemu/range.h"
21 LM_NONE
, /* not traversing a list of repeated options */
22 LM_STARTED
, /* start_list() succeeded */
24 LM_IN_PROGRESS
, /* next_list() has been called.
26 * Generating the next list link will consume the most
27 * recently parsed QemuOpt instance of the repeated
30 * Parsing a value into the list link will examine the
31 * next QemuOpt instance of the repeated option, and
32 * possibly enter LM_SIGNED_INTERVAL or
33 * LM_UNSIGNED_INTERVAL.
36 LM_SIGNED_INTERVAL
, /* next_list() has been called.
38 * Generating the next list link will consume the most
39 * recently stored element from the signed interval,
40 * parsed from the most recent QemuOpt instance of the
41 * repeated option. This may consume QemuOpt itself
42 * and return to LM_IN_PROGRESS.
44 * Parsing a value into the list link will store the
45 * next element of the signed interval.
48 LM_UNSIGNED_INTERVAL
,/* Same as above, only for an unsigned interval. */
53 typedef enum ListMode ListMode
;
55 struct StringOutputVisitor
65 } range_start
, range_end
;
69 static void string_output_set(StringOutputVisitor
*sov
, char *string
)
72 g_string_free(sov
->string
, true);
74 sov
->string
= g_string_new(string
);
78 static void string_output_append(StringOutputVisitor
*sov
, int64_t a
)
80 Range
*r
= g_malloc0(sizeof(*r
));
83 sov
->ranges
= g_list_insert_sorted_merged(sov
->ranges
, r
, range_compare
);
86 static void string_output_append_range(StringOutputVisitor
*sov
,
89 Range
*r
= g_malloc0(sizeof(*r
));
92 sov
->ranges
= g_list_insert_sorted_merged(sov
->ranges
, r
, range_compare
);
95 static void format_string(StringOutputVisitor
*sov
, Range
*r
, bool next
,
98 if (r
->end
- r
->begin
> 1) {
100 g_string_append_printf(sov
->string
, "0x%" PRIx64
"-0x%" PRIx64
,
101 r
->begin
, r
->end
- 1);
104 g_string_append_printf(sov
->string
, "%" PRId64
"-%" PRId64
,
105 r
->begin
, r
->end
- 1);
109 g_string_append_printf(sov
->string
, "0x%" PRIx64
, r
->begin
);
111 g_string_append_printf(sov
->string
, "%" PRId64
, r
->begin
);
115 g_string_append(sov
->string
, ",");
119 static void print_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
122 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
125 switch (sov
->list_mode
) {
127 string_output_append(sov
, *obj
);
131 sov
->range_start
.s
= *obj
;
132 sov
->range_end
.s
= *obj
;
133 sov
->list_mode
= LM_IN_PROGRESS
;
137 if (sov
->range_end
.s
+ 1 == *obj
) {
140 if (sov
->range_start
.s
== sov
->range_end
.s
) {
141 string_output_append(sov
, sov
->range_end
.s
);
143 assert(sov
->range_start
.s
< sov
->range_end
.s
);
144 string_output_append_range(sov
, sov
->range_start
.s
,
148 sov
->range_start
.s
= *obj
;
149 sov
->range_end
.s
= *obj
;
154 if (sov
->range_end
.s
+ 1 == *obj
) {
156 assert(sov
->range_start
.s
< sov
->range_end
.s
);
157 string_output_append_range(sov
, sov
->range_start
.s
,
160 if (sov
->range_start
.s
== sov
->range_end
.s
) {
161 string_output_append(sov
, sov
->range_end
.s
);
163 assert(sov
->range_start
.s
< sov
->range_end
.s
);
165 string_output_append_range(sov
, sov
->range_start
.s
,
168 string_output_append(sov
, *obj
);
179 format_string(sov
, r
, l
->next
!= NULL
, false);
185 g_string_append(sov
->string
, " (");
188 format_string(sov
, r
, l
->next
!= NULL
, true);
191 g_string_append(sov
->string
, ")");
195 static void print_type_size(Visitor
*v
, uint64_t *obj
, const char *name
,
198 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
199 static const char suffixes
[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
205 out
= g_strdup_printf("%"PRIu64
, *obj
);
206 string_output_set(sov
, out
);
212 /* The exponent (returned in i) minus one gives us
213 * floor(log2(val * 1024 / 1000). The correction makes us
214 * switch to the higher power when the integer part is >= 1000.
216 frexp(val
/ (1000.0 / 1024.0), &i
);
218 assert(i
< ARRAY_SIZE(suffixes
));
219 div
= 1ULL << (i
* 10);
221 out
= g_strdup_printf("%"PRIu64
" (%0.3g %c%s)", val
,
222 (double)val
/div
, suffixes
[i
], i
? "iB" : "");
223 string_output_set(sov
, out
);
226 static void print_type_bool(Visitor
*v
, bool *obj
, const char *name
,
229 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
230 string_output_set(sov
, g_strdup(*obj
? "true" : "false"));
233 static void print_type_str(Visitor
*v
, char **obj
, const char *name
,
236 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
240 out
= *obj
? g_strdup_printf("\"%s\"", *obj
) : g_strdup("<null>");
242 out
= g_strdup(*obj
? *obj
: "");
244 string_output_set(sov
, out
);
247 static void print_type_number(Visitor
*v
, double *obj
, const char *name
,
250 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
251 string_output_set(sov
, g_strdup_printf("%f", *obj
));
255 start_list(Visitor
*v
, const char *name
, Error
**errp
)
257 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
259 /* we can't traverse a list in a list */
260 assert(sov
->list_mode
== LM_NONE
);
261 sov
->list_mode
= LM_STARTED
;
266 next_list(Visitor
*v
, GenericList
**list
, Error
**errp
)
268 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
269 GenericList
*ret
= NULL
;
278 if (ret
&& ret
->next
== NULL
) {
279 sov
->list_mode
= LM_NONE
;
283 if (ret
&& ret
->next
== NULL
) {
284 sov
->list_mode
= LM_END
;
293 end_list(Visitor
*v
, Error
**errp
)
295 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
297 assert(sov
->list_mode
== LM_STARTED
||
298 sov
->list_mode
== LM_END
||
299 sov
->list_mode
== LM_NONE
||
300 sov
->list_mode
== LM_IN_PROGRESS
);
301 sov
->list_mode
= LM_NONE
;
306 char *string_output_get_string(StringOutputVisitor
*sov
)
308 char *string
= g_string_free(sov
->string
, false);
313 Visitor
*string_output_get_visitor(StringOutputVisitor
*sov
)
315 return &sov
->visitor
;
318 static void free_range(void *range
, void *dummy
)
323 void string_output_visitor_cleanup(StringOutputVisitor
*sov
)
326 g_string_free(sov
->string
, true);
329 g_list_foreach(sov
->ranges
, free_range
, NULL
);
330 g_list_free(sov
->ranges
);
334 StringOutputVisitor
*string_output_visitor_new(bool human
)
336 StringOutputVisitor
*v
;
338 v
= g_malloc0(sizeof(*v
));
340 v
->string
= g_string_new(NULL
);
342 v
->visitor
.type_enum
= output_type_enum
;
343 v
->visitor
.type_int
= print_type_int
;
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
;