target-arm: Implement AArch64 memory attribute registers
[qemu/qmp-unstable.git] / qapi / string-input-visitor.c
blob793548ae3a8b98ebefcd0114f62b85f44db7ae7c
1 /*
2 * String parsing 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-input-visitor.h"
15 #include "qapi/visitor-impl.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qemu/option.h"
19 struct StringInputVisitor
21 Visitor visitor;
22 const char *string;
25 static void parse_type_int(Visitor *v, int64_t *obj, const char *name,
26 Error **errp)
28 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
29 char *endp = (char *) siv->string;
30 long long val;
32 errno = 0;
33 if (siv->string) {
34 val = strtoll(siv->string, &endp, 0);
36 if (!siv->string || errno || endp == siv->string || *endp) {
37 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
38 "integer");
39 return;
42 *obj = val;
45 static void parse_type_size(Visitor *v, uint64_t *obj, const char *name,
46 Error **errp)
48 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
49 Error *err = NULL;
50 uint64_t val;
52 if (siv->string) {
53 parse_option_size(name, siv->string, &val, &err);
54 } else {
55 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
56 "size");
57 return;
59 if (err) {
60 error_propagate(errp, err);
61 return;
64 *obj = val;
67 static void parse_type_bool(Visitor *v, bool *obj, const char *name,
68 Error **errp)
70 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
72 if (siv->string) {
73 if (!strcasecmp(siv->string, "on") ||
74 !strcasecmp(siv->string, "yes") ||
75 !strcasecmp(siv->string, "true")) {
76 *obj = true;
77 return;
79 if (!strcasecmp(siv->string, "off") ||
80 !strcasecmp(siv->string, "no") ||
81 !strcasecmp(siv->string, "false")) {
82 *obj = false;
83 return;
87 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
88 "boolean");
91 static void parse_type_str(Visitor *v, char **obj, const char *name,
92 Error **errp)
94 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
95 if (siv->string) {
96 *obj = g_strdup(siv->string);
97 } else {
98 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
99 "string");
103 static void parse_type_number(Visitor *v, double *obj, const char *name,
104 Error **errp)
106 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
107 char *endp = (char *) siv->string;
108 double val;
110 errno = 0;
111 if (siv->string) {
112 val = strtod(siv->string, &endp);
114 if (!siv->string || errno || endp == siv->string || *endp) {
115 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
116 "number");
117 return;
120 *obj = val;
123 static void parse_start_optional(Visitor *v, bool *present,
124 const char *name, Error **errp)
126 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
128 if (!siv->string) {
129 *present = false;
130 return;
133 *present = true;
136 Visitor *string_input_get_visitor(StringInputVisitor *v)
138 return &v->visitor;
141 void string_input_visitor_cleanup(StringInputVisitor *v)
143 g_free(v);
146 StringInputVisitor *string_input_visitor_new(const char *str)
148 StringInputVisitor *v;
150 v = g_malloc0(sizeof(*v));
152 v->visitor.type_enum = input_type_enum;
153 v->visitor.type_int = parse_type_int;
154 v->visitor.type_size = parse_type_size;
155 v->visitor.type_bool = parse_type_bool;
156 v->visitor.type_str = parse_type_str;
157 v->visitor.type_number = parse_type_number;
158 v->visitor.start_optional = parse_start_optional;
160 v->string = str;
161 return v;