hw/hppa/dino: Use the IEC binary prefix definitions
[qemu/ar7.git] / qapi / qapi-util.c
blob29a6c98b53882ee8df978feec1d608ecef1a9fa9
1 /*
2 * QAPI util functions
4 * Authors:
5 * Hu Tao <hutao@cn.fujitsu.com>
6 * Peter Lieven <pl@kamp.de>
7 *
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/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/ctype.h"
17 const char *qapi_enum_lookup(const QEnumLookup *lookup, int val)
19 assert(val >= 0 && val < lookup->size);
21 return lookup->array[val];
24 int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
25 int def, Error **errp)
27 int i;
29 if (!buf) {
30 return def;
33 for (i = 0; i < lookup->size; i++) {
34 if (!strcmp(buf, lookup->array[i])) {
35 return i;
39 error_setg(errp, "invalid parameter value: %s", buf);
40 return def;
44 * Parse a valid QAPI name from @str.
45 * A valid name consists of letters, digits, hyphen and underscore.
46 * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN
47 * may contain only letters, digits, hyphen and period.
48 * The special exception for enumeration names is not implemented.
49 * See docs/devel/qapi-code-gen.txt for more on QAPI naming rules.
50 * Keep this consistent with scripts/qapi.py!
51 * If @complete, the parse fails unless it consumes @str completely.
52 * Return its length on success, -1 on failure.
54 int parse_qapi_name(const char *str, bool complete)
56 const char *p = str;
58 if (*p == '_') { /* Downstream __RFQDN_ */
59 p++;
60 if (*p != '_') {
61 return -1;
63 while (*++p) {
64 if (!qemu_isalnum(*p) && *p != '-' && *p != '.') {
65 break;
69 if (*p != '_') {
70 return -1;
72 p++;
75 if (!qemu_isalpha(*p)) {
76 return -1;
78 while (*++p) {
79 if (!qemu_isalnum(*p) && *p != '-' && *p != '_') {
80 break;
84 if (complete && *p) {
85 return -1;
87 return p - str;