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"
18 struct StringInputVisitor
24 static void parse_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
27 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
28 char *endp
= (char *) siv
->string
;
33 val
= strtoll(siv
->string
, &endp
, 0);
35 if (!siv
->string
|| errno
|| endp
== siv
->string
|| *endp
) {
36 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
44 static void parse_type_bool(Visitor
*v
, bool *obj
, const char *name
,
47 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
50 if (!strcasecmp(siv
->string
, "on") ||
51 !strcasecmp(siv
->string
, "yes") ||
52 !strcasecmp(siv
->string
, "true")) {
56 if (!strcasecmp(siv
->string
, "off") ||
57 !strcasecmp(siv
->string
, "no") ||
58 !strcasecmp(siv
->string
, "false")) {
64 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
68 static void parse_type_str(Visitor
*v
, char **obj
, const char *name
,
71 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
73 *obj
= g_strdup(siv
->string
);
75 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
80 static void parse_type_number(Visitor
*v
, double *obj
, const char *name
,
83 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
84 char *endp
= (char *) siv
->string
;
89 val
= strtod(siv
->string
, &endp
);
91 if (!siv
->string
|| errno
|| endp
== siv
->string
|| *endp
) {
92 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
100 static void parse_start_optional(Visitor
*v
, bool *present
,
101 const char *name
, Error
**errp
)
103 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
113 Visitor
*string_input_get_visitor(StringInputVisitor
*v
)
118 void string_input_visitor_cleanup(StringInputVisitor
*v
)
123 StringInputVisitor
*string_input_visitor_new(const char *str
)
125 StringInputVisitor
*v
;
127 v
= g_malloc0(sizeof(*v
));
129 v
->visitor
.type_enum
= input_type_enum
;
130 v
->visitor
.type_int
= parse_type_int
;
131 v
->visitor
.type_bool
= parse_type_bool
;
132 v
->visitor
.type_str
= parse_type_str
;
133 v
->visitor
.type_number
= parse_type_number
;
134 v
->visitor
.start_optional
= parse_start_optional
;