]> Git Repo - qemu.git/blob - qapi/string-output-visitor.c
qapi: Avoid use of misnamed DO_UPCAST()
[qemu.git] / qapi / string-output-visitor.c
1 /*
2  * String printing Visitor
3  *
4  * Copyright Red Hat, Inc. 2012
5  *
6  * Author: Paolo Bonzini <[email protected]>
7  *
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.
10  *
11  */
12
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"
18 #include <math.h>
19 #include "qemu/range.h"
20
21 enum ListMode {
22     LM_NONE,             /* not traversing a list of repeated options */
23     LM_STARTED,          /* start_list() succeeded */
24
25     LM_IN_PROGRESS,      /* next_list() has been called.
26                           *
27                           * Generating the next list link will consume the most
28                           * recently parsed QemuOpt instance of the repeated
29                           * option.
30                           *
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.
35                           */
36
37     LM_SIGNED_INTERVAL,  /* next_list() has been called.
38                           *
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.
44                           *
45                           * Parsing a value into the list link will store the
46                           * next element of the signed interval.
47                           */
48
49     LM_UNSIGNED_INTERVAL,/* Same as above, only for an unsigned interval. */
50
51     LM_END
52 };
53
54 typedef enum ListMode ListMode;
55
56 struct StringOutputVisitor
57 {
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;
68 };
69
70 static StringOutputVisitor *to_sov(Visitor *v)
71 {
72     return container_of(v, StringOutputVisitor, visitor);
73 }
74
75 static void string_output_set(StringOutputVisitor *sov, char *string)
76 {
77     if (sov->string) {
78         g_string_free(sov->string, true);
79     }
80     sov->string = g_string_new(string);
81     g_free(string);
82 }
83
84 static void string_output_append(StringOutputVisitor *sov, int64_t a)
85 {
86     Range *r = g_malloc0(sizeof(*r));
87     r->begin = a;
88     r->end = a + 1;
89     sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare);
90 }
91
92 static void string_output_append_range(StringOutputVisitor *sov,
93                                        int64_t s, int64_t e)
94 {
95     Range *r = g_malloc0(sizeof(*r));
96     r->begin = s;
97     r->end = e + 1;
98     sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare);
99 }
100
101 static void format_string(StringOutputVisitor *sov, Range *r, bool next,
102                           bool human)
103 {
104     if (r->end - r->begin > 1) {
105         if (human) {
106             g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64,
107                                    r->begin, r->end - 1);
108
109         } else {
110             g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64,
111                                    r->begin, r->end - 1);
112         }
113     } else {
114         if (human) {
115             g_string_append_printf(sov->string, "0x%" PRIx64, r->begin);
116         } else {
117             g_string_append_printf(sov->string, "%" PRId64, r->begin);
118         }
119     }
120     if (next) {
121         g_string_append(sov->string, ",");
122     }
123 }
124
125 static void print_type_int(Visitor *v, int64_t *obj, const char *name,
126                            Error **errp)
127 {
128     StringOutputVisitor *sov = to_sov(v);
129     GList *l;
130
131     switch (sov->list_mode) {
132     case LM_NONE:
133         string_output_append(sov, *obj);
134         break;
135
136     case LM_STARTED:
137         sov->range_start.s = *obj;
138         sov->range_end.s = *obj;
139         sov->list_mode = LM_IN_PROGRESS;
140         return;
141
142     case LM_IN_PROGRESS:
143         if (sov->range_end.s + 1 == *obj) {
144             sov->range_end.s++;
145         } else {
146             if (sov->range_start.s == sov->range_end.s) {
147                 string_output_append(sov, sov->range_end.s);
148             } else {
149                 assert(sov->range_start.s < sov->range_end.s);
150                 string_output_append_range(sov, sov->range_start.s,
151                                            sov->range_end.s);
152             }
153
154             sov->range_start.s = *obj;
155             sov->range_end.s = *obj;
156         }
157         return;
158
159     case LM_END:
160         if (sov->range_end.s + 1 == *obj) {
161             sov->range_end.s++;
162             assert(sov->range_start.s < sov->range_end.s);
163             string_output_append_range(sov, sov->range_start.s,
164                                        sov->range_end.s);
165         } else {
166             if (sov->range_start.s == sov->range_end.s) {
167                 string_output_append(sov, sov->range_end.s);
168             } else {
169                 assert(sov->range_start.s < sov->range_end.s);
170
171                 string_output_append_range(sov, sov->range_start.s,
172                                            sov->range_end.s);
173             }
174             string_output_append(sov, *obj);
175         }
176         break;
177
178     default:
179         abort();
180     }
181
182     l = sov->ranges;
183     while (l) {
184         Range *r = l->data;
185         format_string(sov, r, l->next != NULL, false);
186         l = l->next;
187     }
188
189     if (sov->human) {
190         l = sov->ranges;
191         g_string_append(sov->string, " (");
192         while (l) {
193             Range *r = l->data;
194             format_string(sov, r, l->next != NULL, true);
195             l = l->next;
196         }
197         g_string_append(sov->string, ")");
198     }
199 }
200
201 static void print_type_size(Visitor *v, uint64_t *obj, const char *name,
202                            Error **errp)
203 {
204     StringOutputVisitor *sov = to_sov(v);
205     static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
206     uint64_t div, val;
207     char *out;
208     int i;
209
210     if (!sov->human) {
211         out = g_strdup_printf("%"PRIu64, *obj);
212         string_output_set(sov, out);
213         return;
214     }
215
216     val = *obj;
217
218     /* The exponent (returned in i) minus one gives us
219      * floor(log2(val * 1024 / 1000).  The correction makes us
220      * switch to the higher power when the integer part is >= 1000.
221      */
222     frexp(val / (1000.0 / 1024.0), &i);
223     i = (i - 1) / 10;
224     assert(i < ARRAY_SIZE(suffixes));
225     div = 1ULL << (i * 10);
226
227     out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val,
228                           (double)val/div, suffixes[i], i ? "iB" : "");
229     string_output_set(sov, out);
230 }
231
232 static void print_type_bool(Visitor *v, bool *obj, const char *name,
233                             Error **errp)
234 {
235     StringOutputVisitor *sov = to_sov(v);
236     string_output_set(sov, g_strdup(*obj ? "true" : "false"));
237 }
238
239 static void print_type_str(Visitor *v, char **obj, const char *name,
240                            Error **errp)
241 {
242     StringOutputVisitor *sov = to_sov(v);
243     char *out;
244
245     if (sov->human) {
246         out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
247     } else {
248         out = g_strdup(*obj ? *obj : "");
249     }
250     string_output_set(sov, out);
251 }
252
253 static void print_type_number(Visitor *v, double *obj, const char *name,
254                               Error **errp)
255 {
256     StringOutputVisitor *sov = to_sov(v);
257     string_output_set(sov, g_strdup_printf("%f", *obj));
258 }
259
260 static void
261 start_list(Visitor *v, const char *name, Error **errp)
262 {
263     StringOutputVisitor *sov = to_sov(v);
264
265     /* we can't traverse a list in a list */
266     assert(sov->list_mode == LM_NONE);
267     sov->list_mode = LM_STARTED;
268     sov->head = true;
269 }
270
271 static GenericList *
272 next_list(Visitor *v, GenericList **list, Error **errp)
273 {
274     StringOutputVisitor *sov = to_sov(v);
275     GenericList *ret = NULL;
276     if (*list) {
277         if (sov->head) {
278             ret = *list;
279         } else {
280             ret = (*list)->next;
281         }
282
283         if (sov->head) {
284             if (ret && ret->next == NULL) {
285                 sov->list_mode = LM_NONE;
286             }
287             sov->head = false;
288         } else {
289             if (ret && ret->next == NULL) {
290                 sov->list_mode = LM_END;
291             }
292         }
293     }
294
295     return ret;
296 }
297
298 static void
299 end_list(Visitor *v, Error **errp)
300 {
301     StringOutputVisitor *sov = to_sov(v);
302
303     assert(sov->list_mode == LM_STARTED ||
304            sov->list_mode == LM_END ||
305            sov->list_mode == LM_NONE ||
306            sov->list_mode == LM_IN_PROGRESS);
307     sov->list_mode = LM_NONE;
308     sov->head = true;
309
310 }
311
312 char *string_output_get_string(StringOutputVisitor *sov)
313 {
314     char *string = g_string_free(sov->string, false);
315     sov->string = NULL;
316     return string;
317 }
318
319 Visitor *string_output_get_visitor(StringOutputVisitor *sov)
320 {
321     return &sov->visitor;
322 }
323
324 static void free_range(void *range, void *dummy)
325 {
326     g_free(range);
327 }
328
329 void string_output_visitor_cleanup(StringOutputVisitor *sov)
330 {
331     if (sov->string) {
332         g_string_free(sov->string, true);
333     }
334
335     g_list_foreach(sov->ranges, free_range, NULL);
336     g_list_free(sov->ranges);
337     g_free(sov);
338 }
339
340 StringOutputVisitor *string_output_visitor_new(bool human)
341 {
342     StringOutputVisitor *v;
343
344     v = g_malloc0(sizeof(*v));
345
346     v->string = g_string_new(NULL);
347     v->human = human;
348     v->visitor.type_enum = output_type_enum;
349     v->visitor.type_int = print_type_int;
350     v->visitor.type_size = print_type_size;
351     v->visitor.type_bool = print_type_bool;
352     v->visitor.type_str = print_type_str;
353     v->visitor.type_number = print_type_number;
354     v->visitor.start_list = start_list;
355     v->visitor.next_list = next_list;
356     v->visitor.end_list = end_list;
357
358     return v;
359 }
This page took 0.044142 seconds and 4 git commands to generate.