glib: Add compat wrapper for g_poll on old glib
[qemu/pbrook.git] / qapi / string-output-visitor.c
blob921653d4256ed28f1e822c77127a67797c1e89ee
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"
18 struct StringOutputVisitor
20 Visitor visitor;
21 char *string;
24 static void string_output_set(StringOutputVisitor *sov, char *string)
26 g_free(sov->string);
27 sov->string = string;
30 static void print_type_int(Visitor *v, int64_t *obj, const char *name,
31 Error **errp)
33 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
34 string_output_set(sov, g_strdup_printf("%lld", (long long) *obj));
37 static void print_type_bool(Visitor *v, bool *obj, const char *name,
38 Error **errp)
40 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
41 string_output_set(sov, g_strdup(*obj ? "true" : "false"));
44 static void print_type_str(Visitor *v, char **obj, const char *name,
45 Error **errp)
47 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
48 string_output_set(sov, g_strdup(*obj ? *obj : ""));
51 static void print_type_number(Visitor *v, double *obj, const char *name,
52 Error **errp)
54 StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
55 string_output_set(sov, g_strdup_printf("%f", *obj));
58 char *string_output_get_string(StringOutputVisitor *sov)
60 char *string = sov->string;
61 sov->string = NULL;
62 return string;
65 Visitor *string_output_get_visitor(StringOutputVisitor *sov)
67 return &sov->visitor;
70 void string_output_visitor_cleanup(StringOutputVisitor *sov)
72 g_free(sov->string);
73 g_free(sov);
76 StringOutputVisitor *string_output_visitor_new(void)
78 StringOutputVisitor *v;
80 v = g_malloc0(sizeof(*v));
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;
88 return v;