1
2
3
4
5
6
7
8
9
10
11
12
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
18struct StringOutputVisitor
19{
20 Visitor visitor;
21 char *string;
22};
23
24static void string_output_set(StringOutputVisitor *sov, char *string)
25{
26 g_free(sov->string);
27 sov->string = string;
28}
29
30static void print_type_int(Visitor *v, int64_t *obj, const char *name,
31 Error **errp)
32{
33 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
34 string_output_set(sov, g_strdup_printf("%lld", (long long) *obj));
35}
36
37static void print_type_bool(Visitor *v, bool *obj, const char *name,
38 Error **errp)
39{
40 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
41 string_output_set(sov, g_strdup(*obj ? "true" : "false"));
42}
43
44static void print_type_str(Visitor *v, char **obj, const char *name,
45 Error **errp)
46{
47 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
48 string_output_set(sov, g_strdup(*obj ? *obj : ""));
49}
50
51static void print_type_number(Visitor *v, double *obj, const char *name,
52 Error **errp)
53{
54 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
55 string_output_set(sov, g_strdup_printf("%f", *obj));
56}
57
58char *string_output_get_string(StringOutputVisitor *sov)
59{
60 char *string = sov->string;
61 sov->string = NULL;
62 return string;
63}
64
65Visitor *string_output_get_visitor(StringOutputVisitor *sov)
66{
67 return &sov->visitor;
68}
69
70void string_output_visitor_cleanup(StringOutputVisitor *sov)
71{
72 g_free(sov->string);
73 g_free(sov);
74}
75
76StringOutputVisitor *string_output_visitor_new(void)
77{
78 StringOutputVisitor *v;
79
80 v = g_malloc0(sizeof(*v));
81
82 v->visitor.type_enum = output_type_enum;
83 v->visitor.type_int = print_type_int;
84 v->visitor.type_bool = print_type_bool;
85 v->visitor.type_str = print_type_str;
86 v->visitor.type_number = print_type_number;
87
88 return v;
89}
90