[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [Xen-devel] [PATCH V2 09/11] libxl_json, Handle number abrove LONG_MAX.
On Mon, Oct 24, 2011 at 10:57, Ian Campbell <Ian.Campbell@xxxxxxxxxx> wrote: > On Thu, 2011-10-20 at 18:59 +0100, Anthony PERARD wrote: >> The integers are now "long long" in the json_object. If strtoll failed to >> convert a string into a number, the number is stored as it (a char*). >> >> Signed-off-by: Anthony PERARD <anthony.perard@xxxxxxxxxx> >> --- >> Âtools/libxl/libxl_internal.h |  Â7 +++-- >> Âtools/libxl/libxl_json.c   |  52 >> +++++++++++++++++++++++------------------ >> Â2 files changed, 33 insertions(+), 26 deletions(-) >> >> diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h >> index 5720b31..849b251 100644 >> --- a/tools/libxl/libxl_internal.h >> +++ b/tools/libxl/libxl_internal.h >> @@ -465,7 +465,8 @@ typedef enum { >>   ÂJSON_TRUE, >>   ÂJSON_FALSE, >>   ÂJSON_INTEGER, >> -  ÂJSON_DOUBLE, > > Did you accidentally remove this ... > >> +  Â/* number is store in string, it's too big to be a long long */ >> +  ÂJSON_NUMBER, >>   ÂJSON_STRING, >>   ÂJSON_MAP, >>   ÂJSON_ARRAY, >> @@ -475,7 +476,7 @@ typedef enum { >> Âtypedef struct libxl__json_object { >>   Âlibxl__json_node_type type; >>   Âunion { >> -    Âlong i; >> +    Âlong long i; >>     Âdouble d; > > ... or accidentally leave this? I've accidentally leave this double, because I do not handle float number as I do'nt need them yet. But I probably should parse them as well, and keep double in the structure. >>     Âchar *string; >>     Â/* List of libxl__json_object */ >> @@ -534,7 +535,7 @@ flexarray_t *libxl__json_object_get_array(const >> libxl__json_object *o) >>   Âelse >>     Âreturn NULL; >> Â} >> -static inline long libxl__json_object_get_integer(const libxl__json_object >> *o) >> +static inline long long libxl__json_object_get_integer(const >> libxl__json_object *o) >> Â{ >>   Âif (libxl__json_object_is_integer(o)) >>     Âreturn o->u.i; >> diff --git a/tools/libxl/libxl_json.c b/tools/libxl/libxl_json.c >> index c743114..2d8f61e 100644 >> --- a/tools/libxl/libxl_json.c >> +++ b/tools/libxl/libxl_json.c >> @@ -44,6 +44,7 @@ struct libxl__yajl_ctx { >> Â# Âdefine DEBUG_GEN(ctx, type)       Âyajl_gen_##type(ctx->g) >> Â# Âdefine DEBUG_GEN_VALUE(ctx, type, value) yajl_gen_##type(ctx->g, value) >> Â# Âdefine DEBUG_GEN_STRING(ctx, str, n)   yajl_gen_string(ctx->g, str, n) >> +# Âdefine DEBUG_GEN_NUMBER(ctx, str, n)   yajl_gen_number(ctx->g, str, n) >> Â# Âdefine DEBUG_GEN_REPORT(yajl_ctx) \ >>   Âdo { \ >>     Âconst unsigned char *buf = NULL; \ >> @@ -60,6 +61,7 @@ struct libxl__yajl_ctx { >> Â# Âdefine DEBUG_GEN(ctx, type)         Â((void)0) >> Â# Âdefine DEBUG_GEN_VALUE(ctx, type, value)   ((void)0) >> Â# Âdefine DEBUG_GEN_STRING(ctx, value, lenght) Â((void)0) >> +# Âdefine DEBUG_GEN_NUMBER(ctx, value, lenght) Â((void)0) > > that typo got propagated... > >> Â# Âdefine DEBUG_GEN_REPORT(ctx)         ((void)0) >> Â#endif >> >> @@ -363,6 +365,7 @@ void libxl__json_object_free(libxl__gc *gc, >> libxl__json_object *obj) >>     Âreturn; >>   Âswitch (obj->type) { >>   Âcase JSON_STRING: >> +  Âcase JSON_NUMBER: >>     Âfree(obj->u.string); >>     Âbreak; >>   Âcase JSON_MAP: { >> @@ -504,35 +507,38 @@ static int json_callback_boolean(void *opaque, int >> boolean) >>   Âreturn 1; >> Â} >> >> -static int json_callback_integer(void *opaque, long value) >> +static int json_callback_number(void *opaque, const char *s, unsigned int >> len) >> Â{ >>   Âlibxl__yajl_ctx *ctx = opaque; >> -  Âlibxl__json_object *obj; >> - >> -  ÂDEBUG_GEN_VALUE(ctx, integer, value); >> +  Âlibxl__json_object *obj = NULL; >> +  Âlong long i; >> >> -  Âif ((obj = json_object_alloc(ctx->gc, JSON_INTEGER)) == NULL) >> -    Âreturn 0; >> -  Âobj->u.i = value; >> +  Â/* should be replace by number */ >> +  ÂDEBUG_GEN_NUMBER(ctx, s, len); >> >> -  Âif (json_object_append_to(ctx->gc, obj, ctx->current) == -1) { >> -    Âlibxl__json_object_free(ctx->gc, obj); >> -    Âreturn 0; >> -  Â} >> +  Âi = strtoll(s, NULL, 10); >> >> -  Âreturn 1; >> -} >> +  Âif ((i == LLONG_MIN || i == LLONG_MAX) && errno == ERANGE) { >> +    Âchar *t = NULL; >> >> -static int json_callback_double(void *opaque, double value) >> -{ >> -  Âlibxl__yajl_ctx *ctx = opaque; >> -  Âlibxl__json_object *obj; >> +    Âif ((obj = json_object_alloc(ctx->gc, JSON_NUMBER)) == NULL) >> +      Âreturn 0; >> >> -  ÂDEBUG_GEN_VALUE(ctx, double, value); >> +    Ât = malloc(len + 1); >> +    Âif (t == NULL) { >> +      ÂLIBXL__LOG_ERRNO(libxl__gc_owner(ctx->gc), LIBXL__LOG_ERROR, >> +               "Failed to allocate"); >> +      Âreturn 0; >> +    Â} >> +    Âstrncpy(t, s, len); >> +    Ât[len] = 0; >> >> -  Âif ((obj = json_object_alloc(ctx->gc, JSON_DOUBLE)) == NULL) >> -    Âreturn 0; >> -  Âobj->u.d = value; >> +    Âobj->u.string = t; >> +  Â} else { >> +    Âif ((obj = json_object_alloc(ctx->gc, JSON_INTEGER)) == NULL) >> +      Âreturn 0; >> +    Âobj->u.i = i; >> +  Â} >> >>   Âif (json_object_append_to(ctx->gc, obj, ctx->current) == -1) { >>     Âlibxl__json_object_free(ctx->gc, obj); >> @@ -706,9 +712,9 @@ static int json_callback_end_array(void *opaque) >> Âstatic yajl_callbacks callbacks = { >>   Âjson_callback_null, >>   Âjson_callback_boolean, >> -  Âjson_callback_integer, >> -  Âjson_callback_double, >>   ÂNULL, >> +  ÂNULL, >> +  Âjson_callback_number, >>   Âjson_callback_string, >>   Âjson_callback_start_map, >>   Âjson_callback_map_key, > > > > _______________________________________________ > Xen-devel mailing list > Xen-devel@xxxxxxxxxxxxxxxxxxx > http://lists.xensource.com/xen-devel > -- Anthony PERARD _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxxxxxxxx http://lists.xensource.com/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |