xref: /openbmc/qemu/qapi/string-output-visitor.c (revision 173bbb75)
1a020f980SPaolo Bonzini /*
2a020f980SPaolo Bonzini  * String printing Visitor
3a020f980SPaolo Bonzini  *
4a020f980SPaolo Bonzini  * Copyright Red Hat, Inc. 2012
5a020f980SPaolo Bonzini  *
6a020f980SPaolo Bonzini  * Author: Paolo Bonzini <pbonzini@redhat.com>
7a020f980SPaolo Bonzini  *
8a020f980SPaolo Bonzini  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9a020f980SPaolo Bonzini  * See the COPYING.LIB file in the top-level directory.
10a020f980SPaolo Bonzini  *
11a020f980SPaolo Bonzini  */
12a020f980SPaolo Bonzini 
13a020f980SPaolo Bonzini #include "qemu-common.h"
14a020f980SPaolo Bonzini #include "string-output-visitor.h"
15a020f980SPaolo Bonzini #include "qapi/qapi-visit-impl.h"
16a020f980SPaolo Bonzini #include "qerror.h"
17a020f980SPaolo Bonzini 
18a020f980SPaolo Bonzini struct StringOutputVisitor
19a020f980SPaolo Bonzini {
20a020f980SPaolo Bonzini     Visitor visitor;
21a020f980SPaolo Bonzini     char *string;
22a020f980SPaolo Bonzini };
23a020f980SPaolo Bonzini 
24a020f980SPaolo Bonzini static void string_output_set(StringOutputVisitor *sov, char *string)
25a020f980SPaolo Bonzini {
26a020f980SPaolo Bonzini     g_free(sov->string);
27a020f980SPaolo Bonzini     sov->string = string;
28a020f980SPaolo Bonzini }
29a020f980SPaolo Bonzini 
30a020f980SPaolo Bonzini static void print_type_int(Visitor *v, int64_t *obj, const char *name,
31a020f980SPaolo Bonzini                            Error **errp)
32a020f980SPaolo Bonzini {
33a020f980SPaolo Bonzini     StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
34a020f980SPaolo Bonzini     string_output_set(sov, g_strdup_printf("%lld", (long long) *obj));
35a020f980SPaolo Bonzini }
36a020f980SPaolo Bonzini 
37a020f980SPaolo Bonzini static void print_type_bool(Visitor *v, bool *obj, const char *name,
38a020f980SPaolo Bonzini                             Error **errp)
39a020f980SPaolo Bonzini {
40a020f980SPaolo Bonzini     StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
41a020f980SPaolo Bonzini     string_output_set(sov, g_strdup(*obj ? "true" : "false"));
42a020f980SPaolo Bonzini }
43a020f980SPaolo Bonzini 
44a020f980SPaolo Bonzini static void print_type_str(Visitor *v, char **obj, const char *name,
45a020f980SPaolo Bonzini                            Error **errp)
46a020f980SPaolo Bonzini {
47a020f980SPaolo Bonzini     StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
48a020f980SPaolo Bonzini     string_output_set(sov, g_strdup(*obj ? *obj : ""));
49a020f980SPaolo Bonzini }
50a020f980SPaolo Bonzini 
51a020f980SPaolo Bonzini static void print_type_number(Visitor *v, double *obj, const char *name,
52a020f980SPaolo Bonzini                               Error **errp)
53a020f980SPaolo Bonzini {
54a020f980SPaolo Bonzini     StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
55*173bbb75SMichael Roth     string_output_set(sov, g_strdup_printf("%f", *obj));
56a020f980SPaolo Bonzini }
57a020f980SPaolo Bonzini 
58a020f980SPaolo Bonzini char *string_output_get_string(StringOutputVisitor *sov)
59a020f980SPaolo Bonzini {
60a020f980SPaolo Bonzini     char *string = sov->string;
61a020f980SPaolo Bonzini     sov->string = NULL;
62a020f980SPaolo Bonzini     return string;
63a020f980SPaolo Bonzini }
64a020f980SPaolo Bonzini 
65a020f980SPaolo Bonzini Visitor *string_output_get_visitor(StringOutputVisitor *sov)
66a020f980SPaolo Bonzini {
67a020f980SPaolo Bonzini     return &sov->visitor;
68a020f980SPaolo Bonzini }
69a020f980SPaolo Bonzini 
70a020f980SPaolo Bonzini void string_output_visitor_cleanup(StringOutputVisitor *sov)
71a020f980SPaolo Bonzini {
72a020f980SPaolo Bonzini     g_free(sov->string);
73a020f980SPaolo Bonzini     g_free(sov);
74a020f980SPaolo Bonzini }
75a020f980SPaolo Bonzini 
76a020f980SPaolo Bonzini StringOutputVisitor *string_output_visitor_new(void)
77a020f980SPaolo Bonzini {
78a020f980SPaolo Bonzini     StringOutputVisitor *v;
79a020f980SPaolo Bonzini 
80a020f980SPaolo Bonzini     v = g_malloc0(sizeof(*v));
81a020f980SPaolo Bonzini 
82a020f980SPaolo Bonzini     v->visitor.type_enum = output_type_enum;
83a020f980SPaolo Bonzini     v->visitor.type_int = print_type_int;
84a020f980SPaolo Bonzini     v->visitor.type_bool = print_type_bool;
85a020f980SPaolo Bonzini     v->visitor.type_str = print_type_str;
86a020f980SPaolo Bonzini     v->visitor.type_number = print_type_number;
87a020f980SPaolo Bonzini 
88a020f980SPaolo Bonzini     return v;
89a020f980SPaolo Bonzini }
90