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
25 static void parse_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
28 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
29 char *endp
= (char *) 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",
45 static void parse_type_size(Visitor
*v
, uint64_t *obj
, const char *name
,
48 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
53 parse_option_size(name
, siv
->string
, &val
, &err
);
55 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
60 error_propagate(errp
, err
);
67 static void parse_type_bool(Visitor
*v
, bool *obj
, const char *name
,
70 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
73 if (!strcasecmp(siv
->string
, "on") ||
74 !strcasecmp(siv
->string
, "yes") ||
75 !strcasecmp(siv
->string
, "true")) {
79 if (!strcasecmp(siv
->string
, "off") ||
80 !strcasecmp(siv
->string
, "no") ||
81 !strcasecmp(siv
->string
, "false")) {
87 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
91 static void parse_type_str(Visitor
*v
, char **obj
, const char *name
,
94 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
96 *obj
= g_strdup(siv
->string
);
98 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
103 static void parse_type_number(Visitor
*v
, double *obj
, const char *name
,
106 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
107 char *endp
= (char *) 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",
123 static void parse_start_optional(Visitor
*v
, bool *present
,
124 const char *name
, Error
**errp
)
126 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
136 Visitor
*string_input_get_visitor(StringInputVisitor
*v
)
141 void string_input_visitor_cleanup(StringInputVisitor
*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
;