ahci: Update byte count after DMA completion
[qemu/ar7.git] / qapi / string-output-visitor.c
blob1ab8574585c3d3038dd08908ad6fed853a486cd4
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-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"
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 void string_output_set(StringOutputVisitor *sov, char *string)
72 if (sov->string) {
73 g_string_free(sov->string, true);
75 sov->string = g_string_new(string);
76 g_free(string);
79 static void string_output_append(StringOutputVisitor *sov, int64_t a)
81 Range *r = g_malloc0(sizeof(*r));
82 r->begin = a;
83 r->end = a + 1;
84 sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare);
87 static void string_output_append_range(StringOutputVisitor *sov,
88 int64_t s, int64_t e)
90 Range *r = g_malloc0(sizeof(*r));
91 r->begin = s;
92 r->end = e + 1;
93 sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare);
96 static void format_string(StringOutputVisitor *sov, Range *r, bool next,
97 bool human)
99 if (r->end - r->begin > 1) {
100 if (human) {
101 g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64,
102 r->begin, r->end - 1);
104 } else {
105 g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64,
106 r->begin, r->end - 1);
108 } else {
109 if (human) {
110 g_string_append_printf(sov->string, "0x%" PRIx64, r->begin);
111 } else {
112 g_string_append_printf(sov->string, "%" PRId64, r->begin);
115 if (next) {
116 g_string_append(sov->string, ",");
120 static void print_type_int(Visitor *v, int64_t *obj, const char *name,
121 Error **errp)
123 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
124 GList *l;
126 switch (sov->list_mode) {
127 case LM_NONE:
128 string_output_append(sov, *obj);
129 break;
131 case LM_STARTED:
132 sov->range_start.s = *obj;
133 sov->range_end.s = *obj;
134 sov->list_mode = LM_IN_PROGRESS;
135 return;
137 case LM_IN_PROGRESS:
138 if (sov->range_end.s + 1 == *obj) {
139 sov->range_end.s++;
140 } else {
141 if (sov->range_start.s == sov->range_end.s) {
142 string_output_append(sov, sov->range_end.s);
143 } else {
144 assert(sov->range_start.s < sov->range_end.s);
145 string_output_append_range(sov, sov->range_start.s,
146 sov->range_end.s);
149 sov->range_start.s = *obj;
150 sov->range_end.s = *obj;
152 return;
154 case LM_END:
155 if (sov->range_end.s + 1 == *obj) {
156 sov->range_end.s++;
157 assert(sov->range_start.s < sov->range_end.s);
158 string_output_append_range(sov, sov->range_start.s,
159 sov->range_end.s);
160 } else {
161 if (sov->range_start.s == sov->range_end.s) {
162 string_output_append(sov, sov->range_end.s);
163 } else {
164 assert(sov->range_start.s < sov->range_end.s);
166 string_output_append_range(sov, sov->range_start.s,
167 sov->range_end.s);
169 string_output_append(sov, *obj);
171 break;
173 default:
174 abort();
177 l = sov->ranges;
178 while (l) {
179 Range *r = l->data;
180 format_string(sov, r, l->next != NULL, false);
181 l = l->next;
184 if (sov->human) {
185 l = sov->ranges;
186 g_string_append(sov->string, " (");
187 while (l) {
188 Range *r = l->data;
189 format_string(sov, r, l->next != NULL, true);
190 l = l->next;
192 g_string_append(sov->string, ")");
196 static void print_type_size(Visitor *v, uint64_t *obj, const char *name,
197 Error **errp)
199 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
200 static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
201 uint64_t div, val;
202 char *out;
203 int i;
205 if (!sov->human) {
206 out = g_strdup_printf("%"PRIu64, *obj);
207 string_output_set(sov, out);
208 return;
211 val = *obj;
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);
218 i = (i - 1) / 10;
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,
228 Error **errp)
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,
235 Error **errp)
237 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
238 char *out;
240 if (sov->human) {
241 out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
242 } else {
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,
249 Error **errp)
251 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
252 string_output_set(sov, g_strdup_printf("%f", *obj));
255 static void
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;
263 sov->head = true;
266 static GenericList *
267 next_list(Visitor *v, GenericList **list, Error **errp)
269 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
270 GenericList *ret = NULL;
271 if (*list) {
272 if (sov->head) {
273 ret = *list;
274 } else {
275 ret = (*list)->next;
278 if (sov->head) {
279 if (ret && ret->next == NULL) {
280 sov->list_mode = LM_NONE;
282 sov->head = false;
283 } else {
284 if (ret && ret->next == NULL) {
285 sov->list_mode = LM_END;
290 return ret;
293 static void
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;
303 sov->head = true;
307 char *string_output_get_string(StringOutputVisitor *sov)
309 char *string = g_string_free(sov->string, false);
310 sov->string = NULL;
311 return string;
314 Visitor *string_output_get_visitor(StringOutputVisitor *sov)
316 return &sov->visitor;
319 static void free_range(void *range, void *dummy)
321 g_free(range);
324 void string_output_visitor_cleanup(StringOutputVisitor *sov)
326 if (sov->string) {
327 g_string_free(sov->string, true);
330 g_list_foreach(sov->ranges, free_range, NULL);
331 g_list_free(sov->ranges);
332 g_free(sov);
335 StringOutputVisitor *string_output_visitor_new(bool human)
337 StringOutputVisitor *v;
339 v = g_malloc0(sizeof(*v));
341 v->string = g_string_new(NULL);
342 v->human = human;
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;
353 return v;