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 "qapi/qmp/qerror.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 void string_output_set(StringOutputVisitor
*sov
, char *string
)
73 g_string_free(sov
->string
, true);
75 sov
->string
= g_string_new(string
);
79 static void string_output_append(StringOutputVisitor
*sov
, int64_t a
)
81 Range
*r
= g_malloc0(sizeof(*r
));
84 sov
->ranges
= g_list_insert_sorted_merged(sov
->ranges
, r
, range_compare
);
87 static void string_output_append_range(StringOutputVisitor
*sov
,
90 Range
*r
= g_malloc0(sizeof(*r
));
93 sov
->ranges
= g_list_insert_sorted_merged(sov
->ranges
, r
, range_compare
);
96 static void format_string(StringOutputVisitor
*sov
, Range
*r
, bool next
,
99 if (r
->end
- r
->begin
> 1) {
101 g_string_append_printf(sov
->string
, "%" PRIx64
"-%" PRIx64
,
102 r
->begin
, r
->end
- 1);
105 g_string_append_printf(sov
->string
, "%" PRId64
"-%" PRId64
,
106 r
->begin
, r
->end
- 1);
110 g_string_append_printf(sov
->string
, "%" PRIx64
, r
->begin
);
112 g_string_append_printf(sov
->string
, "%" PRId64
, r
->begin
);
116 g_string_append(sov
->string
, ",");
120 static void print_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
123 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
126 switch (sov
->list_mode
) {
128 string_output_append(sov
, *obj
);
132 sov
->range_start
.s
= *obj
;
133 sov
->range_end
.s
= *obj
;
134 sov
->list_mode
= LM_IN_PROGRESS
;
138 if (sov
->range_end
.s
+ 1 == *obj
) {
141 if (sov
->range_start
.s
== sov
->range_end
.s
) {
142 string_output_append(sov
, sov
->range_end
.s
);
144 assert(sov
->range_start
.s
< sov
->range_end
.s
);
145 string_output_append_range(sov
, sov
->range_start
.s
,
149 sov
->range_start
.s
= *obj
;
150 sov
->range_end
.s
= *obj
;
155 if (sov
->range_end
.s
+ 1 == *obj
) {
157 assert(sov
->range_start
.s
< sov
->range_end
.s
);
158 string_output_append_range(sov
, sov
->range_start
.s
,
161 if (sov
->range_start
.s
== sov
->range_end
.s
) {
162 string_output_append(sov
, sov
->range_end
.s
);
164 assert(sov
->range_start
.s
< sov
->range_end
.s
);
166 string_output_append_range(sov
, sov
->range_start
.s
,
169 string_output_append(sov
, *obj
);
180 format_string(sov
, r
, l
->next
!= NULL
, false);
186 g_string_append(sov
->string
, " (");
189 format_string(sov
, r
, l
->next
!= NULL
, false);
192 g_string_append(sov
->string
, ")");
196 static void print_type_size(Visitor
*v
, uint64_t *obj
, const char *name
,
199 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
200 static const char suffixes
[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
206 out
= g_strdup_printf("%"PRIu64
, *obj
);
207 string_output_set(sov
, out
);
213 /* The exponent (returned in i) minus one gives us
214 * floor(log2(val * 1024 / 1000). The correction makes us
215 * switch to the higher power when the integer part is >= 1000.
217 frexp(val
/ (1000.0 / 1024.0), &i
);
219 assert(i
< ARRAY_SIZE(suffixes
));
220 div
= 1ULL << (i
* 10);
222 out
= g_strdup_printf("%"PRIu64
" (%0.3g %c%s)", val
,
223 (double)val
/div
, suffixes
[i
], i
? "iB" : "");
224 string_output_set(sov
, out
);
227 static void print_type_bool(Visitor
*v
, bool *obj
, const char *name
,
230 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
231 string_output_set(sov
, g_strdup(*obj
? "true" : "false"));
234 static void print_type_str(Visitor
*v
, char **obj
, const char *name
,
237 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
241 out
= *obj
? g_strdup_printf("\"%s\"", *obj
) : g_strdup("<null>");
243 out
= g_strdup(*obj
? *obj
: "");
245 string_output_set(sov
, out
);
248 static void print_type_number(Visitor
*v
, double *obj
, const char *name
,
251 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
252 string_output_set(sov
, g_strdup_printf("%f", *obj
));
256 start_list(Visitor
*v
, const char *name
, Error
**errp
)
258 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
260 /* we can't traverse a list in a list */
261 assert(sov
->list_mode
== LM_NONE
);
262 sov
->list_mode
= LM_STARTED
;
267 next_list(Visitor
*v
, GenericList
**list
, Error
**errp
)
269 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, v
);
270 GenericList
*ret
= NULL
;
279 if (ret
&& ret
->next
== NULL
) {
280 sov
->list_mode
= LM_NONE
;
284 if (ret
&& ret
->next
== NULL
) {
285 sov
->list_mode
= LM_END
;
294 end_list(Visitor
*v
, Error
**errp
)
296 StringOutputVisitor
*sov
= DO_UPCAST(StringOutputVisitor
, visitor
, 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
;
307 char *string_output_get_string(StringOutputVisitor
*sov
)
309 char *string
= g_string_free(sov
->string
, false);
314 Visitor
*string_output_get_visitor(StringOutputVisitor
*sov
)
316 return &sov
->visitor
;
319 static void free_range(void *range
, void *dummy
)
324 void string_output_visitor_cleanup(StringOutputVisitor
*sov
)
327 g_string_free(sov
->string
, true);
330 g_list_foreach(sov
->ranges
, free_range
, NULL
);
331 g_list_free(sov
->ranges
);
335 StringOutputVisitor
*string_output_visitor_new(bool human
)
337 StringOutputVisitor
*v
;
339 v
= g_malloc0(sizeof(*v
));
341 v
->string
= g_string_new(NULL
);
343 v
->visitor
.type_enum
= output_type_enum
;
344 v
->visitor
.type_int
= print_type_int
;
345 v
->visitor
.type_size
= print_type_size
;
346 v
->visitor
.type_bool
= print_type_bool
;
347 v
->visitor
.type_str
= print_type_str
;
348 v
->visitor
.type_number
= print_type_number
;
349 v
->visitor
.start_list
= start_list
;
350 v
->visitor
.next_list
= next_list
;
351 v
->visitor
.end_list
= end_list
;