[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH RFC 5/9] libxl_json: introduce parser functions for builtin types
This changeset introduces following functions: * libxl_defbool_parse_json * libxl__bool_parse_json * libxl_uuid_parse_json * libxl_mac_parse_json * libxl_bitmap_parse_json * libxl_cpuid_policy_list_parse_json * libxl_string_list_parse_json * libxl_key_value_list_parse_json * libxl_hwcap_parse_json * libxl__integer_parse_json * libxl__string_parse_json They will be used in later patch to convert the libxl__json_object tree of a builtin type to libxl_FOO struct. Also remove delcaration of libxl_domid_gen_json as libxl_domid uses yajl_gen_integer to generate JSON object. Signed-off-by: Wei Liu <wei.liu2@xxxxxxxxxx> --- tools/libxl/libxl_cpuid.c | 92 +++++++++++++++++---- tools/libxl/libxl_json.c | 191 +++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_json.h | 27 +++++- tools/libxl/libxl_nocpuid.c | 7 ++ 4 files changed, 300 insertions(+), 17 deletions(-) diff --git a/tools/libxl/libxl_cpuid.c b/tools/libxl/libxl_cpuid.c index dd21b78..2c725a5 100644 --- a/tools/libxl/libxl_cpuid.c +++ b/tools/libxl/libxl_cpuid.c @@ -336,29 +336,29 @@ void libxl_cpuid_set(libxl_ctx *ctx, uint32_t domid, (const char**)(cpuid[i].policy), cpuid_res); } +static const char *input_names[2] = { "leaf", "subleaf" }; +static const char *policy_names[4] = { "eax", "ebx", "ecx", "edx" }; +/* + * Aiming for: + * [ + * { 'leaf': 'val-eax', + * 'subleaf': 'val-ecx', + * 'eax': 'filter', + * 'ebx': 'filter', + * 'ecx': 'filter', + * 'edx': 'filter' }, + * { 'leaf': 'val-eax', ..., 'eax': 'filter', ... }, + * ... etc ... + * ] + */ + yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand, libxl_cpuid_policy_list *pcpuid) { libxl_cpuid_policy_list cpuid = *pcpuid; yajl_gen_status s; - const char *input_names[2] = { "leaf", "subleaf" }; - const char *policy_names[4] = { "eax", "ebx", "ecx", "edx" }; int i, j; - /* - * Aiming for: - * [ - * { 'leaf': 'val-eax', - * 'subleaf': 'val-ecx', - * 'eax': 'filter', - * 'ebx': 'filter', - * 'ecx': 'filter', - * 'edx': 'filter' }, - * { 'leaf': 'val-eax', ..., 'eax': 'filter', ... }, - * ... etc ... - * ] - */ - s = yajl_gen_array_open(hand); if (s != yajl_gen_status_ok) goto out; @@ -396,6 +396,66 @@ out: return s; } +int libxl_cpuid_policy_list_parse_json(libxl_ctx *ctx, + const libxl__json_object *o, + libxl_cpuid_policy_list *p) +{ + int i, size; + libxl_cpuid_policy_list l; + + if (!libxl__json_object_is_array(o)) + return -1; + + if (!o->u.array->count) + return 0; + + size = o->u.array->count; + /* need one extra slot as setinel */ + l = *p = calloc(size + 1, sizeof(libxl_cpuid_policy)); + + if (!l) + return -1; + + memset(l, 0, sizeof(libxl_cpuid_policy) * (size + 1)); + l[size].input[0] = XEN_CPUID_INPUT_UNUSED; + l[size].input[1] = XEN_CPUID_INPUT_UNUSED; + + for (i = 0; i < size; i++) { + const libxl__json_object *t; + int j; + + if (flexarray_get(o->u.array, i, (void**)&t) != 0) + return -1; /* dispose_fn will clean up memory + * allocation */ + + assert(libxl__json_object_is_map(t)); + + for (j = 0; j < 2; j++) { + const libxl__json_object *r; + + r = libxl__json_map_get(input_names[j], t, JSON_INTEGER); + if (!r) + l[i].input[j] = XEN_CPUID_INPUT_UNUSED; + else + l[i].input[j] = r->u.i; + } + + for (j = 0; j < 4; j++) { + const libxl__json_object *r; + + r = libxl__json_map_get(policy_names[j], t, JSON_STRING); + if (!r) + l[i].policy[j] = NULL; + else + l[i].policy[j] = strdup(r->u.string); + } + } + + assert(i == size); + + return 0; +} + /* * Local variables: * mode: C diff --git a/tools/libxl/libxl_json.c b/tools/libxl/libxl_json.c index da315f6..34ac035 100644 --- a/tools/libxl/libxl_json.c +++ b/tools/libxl/libxl_json.c @@ -100,6 +100,35 @@ yajl_gen_status libxl_defbool_gen_json(yajl_gen hand, return libxl__yajl_gen_asciiz(hand, libxl_defbool_to_string(*db)); } +int libxl_defbool_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_defbool *p) +{ + if (!libxl__json_object_is_string(o)) + return -1; + + if (!strncmp(o->u.string, "<default>", strlen("<default>"))) + p->val = 0; + else if (!strncmp(o->u.string, "True", strlen("True"))) + p->val = 1; + else if (!strncmp(o->u.string, "False", strlen("False"))) + p->val = -1; + else + return -1; + + return 0; +} + +int libxl__bool_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + bool *p) +{ + if (!libxl__json_object_is_bool(o)) + return -1; + + *p = o->u.b; + + return 0; +} + yajl_gen_status libxl_uuid_gen_json(yajl_gen hand, libxl_uuid *uuid) { @@ -108,6 +137,15 @@ yajl_gen_status libxl_uuid_gen_json(yajl_gen hand, return yajl_gen_string(hand, (const unsigned char *)buf, LIBXL_UUID_FMTLEN); } +int libxl_uuid_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_uuid *p) +{ + if (!libxl__json_object_is_string(o)) + return -1; + + return libxl_uuid_from_string(p, o->u.string); +} + yajl_gen_status libxl_bitmap_gen_json(yajl_gen hand, libxl_bitmap *bitmap) { @@ -128,6 +166,34 @@ out: return s; } +int libxl_bitmap_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_bitmap *p) +{ + int i; + int size; + const libxl__json_object *t; + + if (!libxl__json_object_is_array(o)) + return -1; + + if (!o->u.array->count) + return 0; + + t = libxl__json_array_get(o, o->u.array->count - 1); + size = t->u.i + 1; + + libxl_bitmap_alloc(ctx, p, size); + + for (i = 0; (t = libxl__json_array_get(o, i)); i++) { + if (!libxl__json_object_is_integer(t)) + return -1; /* dispose_fn will clean up memory + * allocation */ + libxl_bitmap_set(p, t->u.i); + } + + return 0; +} + yajl_gen_status libxl_key_value_list_gen_json(yajl_gen hand, libxl_key_value_list *pkvl) { @@ -155,6 +221,43 @@ out: return s; } +int libxl_key_value_list_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_key_value_list *p) +{ + libxl__json_map_node *node = NULL; + flexarray_t *maps = NULL; + int i, size; + libxl_key_value_list kvl; + + if (!libxl__json_object_is_map(o)) + return -1; + + maps = o->u.map; + size = maps->count * 2; + kvl = *p = calloc(size, sizeof(char *)); + if (!kvl) + return -1; + + memset(kvl, 0, sizeof(char *) * size); + + for (i = 0; i < maps->count; i++) { + int idx = i * 2; + if (flexarray_get(maps, i, (void**)&node) != 0) + return -1; + assert(libxl__json_object_is_string(node->obj) || + libxl__json_object_is_null(node->obj)); + kvl[idx] = strdup(node->map_key); + if (libxl__json_object_is_string(node->obj)) + kvl[idx+1] = strdup(node->obj->u.string); + else + kvl[idx+1] = NULL; + } + + assert(i == maps->count); + + return 0; +} + yajl_gen_status libxl_string_list_gen_json(yajl_gen hand, libxl_string_list *pl) { libxl_string_list l = *pl; @@ -176,6 +279,36 @@ out: return s; } +int libxl_string_list_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_string_list *p) +{ + const libxl__json_object *t; + libxl_string_list l; + flexarray_t *array = NULL; + int i, size; + + if (!libxl__json_object_is_array(o)) + return -1; + + array = o->u.array; + size = array->count; + /* need one extra slot as sentinel */ + l = *p = calloc(size + 1, sizeof(char *)); + + if (!l) + return -1; + + memset(l, 0, sizeof(char *) * size); + l[size] = NULL; + + for (i = 0; (t = libxl__json_array_get(o, i)); i++) { + assert(libxl__json_object_is_string(t)); + l[i] = strdup(t->u.string); + } + + return 0; +} + yajl_gen_status libxl_mac_gen_json(yajl_gen hand, libxl_mac *mac) { char buf[LIBXL_MAC_FMTLEN+1]; @@ -183,6 +316,15 @@ yajl_gen_status libxl_mac_gen_json(yajl_gen hand, libxl_mac *mac) return yajl_gen_string(hand, (const unsigned char *)buf, LIBXL_MAC_FMTLEN); } +int libxl_mac_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_mac *p) +{ + if (!libxl__json_object_is_string(o)) + return -1; + + return libxl__parse_mac(o->u.string, *p); +} + yajl_gen_status libxl_hwcap_gen_json(yajl_gen hand, libxl_hwcap *p) { @@ -201,6 +343,27 @@ out: return s; } +int libxl_hwcap_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_hwcap *p) +{ + int i; + + if (!libxl__json_object_is_array(o)) + return -1; + + for (i = 0; i<4; i++) { + const libxl__json_object *t; + + t = libxl__json_array_get(o, i); + if (!t || !libxl__json_object_is_integer(t)) + return -1; + + (*p)[i] = t->u.i; + } + + return 0; +} + yajl_gen_status libxl__string_gen_json(yajl_gen hand, const char *p) { @@ -210,6 +373,23 @@ yajl_gen_status libxl__string_gen_json(yajl_gen hand, return yajl_gen_null(hand); } +int libxl__string_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + char **p) +{ + if (!libxl__json_object_is_string(o) && !libxl__json_object_is_null(o)) + return -1; + + if (libxl__json_object_is_null(o)) { + *p = NULL; + } else { + *p = strdup(o->u.string); + if (!*p) + return -1; + } + + return 0; +} + /* * libxl__json_object helper functions */ @@ -828,6 +1008,17 @@ out: return ret; } +int libxl__integer_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + void *p) +{ + if (!libxl__json_object_is_integer(o)) + return -1; + + *((long long *)p) = o->u.i; + + return 0; +} + /* * Local variables: * mode: C diff --git a/tools/libxl/libxl_json.h b/tools/libxl/libxl_json.h index a4dd8fc..d7bdeb5 100644 --- a/tools/libxl/libxl_json.h +++ b/tools/libxl/libxl_json.h @@ -22,17 +22,42 @@ # include <yajl/yajl_version.h> #endif +typedef struct libxl__json_object libxl__json_object; + yajl_gen_status libxl_defbool_gen_json(yajl_gen hand, libxl_defbool *p); -yajl_gen_status libxl_domid_gen_json(yajl_gen hand, libxl_domid *p); +int libxl_defbool_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_defbool *p); +int libxl__bool_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + bool *p); yajl_gen_status libxl_uuid_gen_json(yajl_gen hand, libxl_uuid *p); +int libxl_uuid_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_uuid *p); yajl_gen_status libxl_mac_gen_json(yajl_gen hand, libxl_mac *p); +int libxl_mac_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_mac *p); yajl_gen_status libxl_bitmap_gen_json(yajl_gen hand, libxl_bitmap *p); +int libxl_bitmap_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_bitmap *p); yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand, libxl_cpuid_policy_list *p); +int libxl_cpuid_policy_list_parse_json(libxl_ctx *ctx, + const libxl__json_object *o, + libxl_cpuid_policy_list *p); yajl_gen_status libxl_string_list_gen_json(yajl_gen hand, libxl_string_list *p); +int libxl_string_list_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_string_list *p); yajl_gen_status libxl_key_value_list_gen_json(yajl_gen hand, libxl_key_value_list *p); +int libxl_key_value_list_parse_json(libxl_ctx *ctx, + const libxl__json_object *o, + libxl_key_value_list *p); yajl_gen_status libxl_hwcap_gen_json(yajl_gen hand, libxl_hwcap *p); +int libxl_hwcap_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + libxl_hwcap *p); +int libxl__integer_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + void *p); +int libxl__string_parse_json(libxl_ctx *ctx, const libxl__json_object *o, + char **p); #include <_libxl_types_json.h> diff --git a/tools/libxl/libxl_nocpuid.c b/tools/libxl/libxl_nocpuid.c index 5f7cb6a..7364dd5 100644 --- a/tools/libxl/libxl_nocpuid.c +++ b/tools/libxl/libxl_nocpuid.c @@ -44,6 +44,13 @@ yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand, return 0; } +int libxl_cpuid_policy_list_parse_json(libxl_ctx *ctx, + const libxl__json_object *o, + libxl_cpuid_policy_list *p) +{ + return 0; +} + /* * Local variables: * mode: C -- 1.7.10.4 _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |