mirror of https://github.com/akheron/jansson
Unify style
This commit is contained in:
parent
fa7c2ea070
commit
53383860e8
115
src/variadic.c
115
src/variadic.c
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
|
||||
* Copyright (c) 2010 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
|
||||
* Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
|
||||
* Copyright (c) 2011 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
|
||||
*
|
||||
* Jansson is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the MIT license. See LICENSE for details.
|
||||
|
@ -38,7 +38,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
|
||||
if(size <= 0) {
|
||||
jsonp_error_set(error, 1, 1, "Empty format string!");
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* tok must contain either a container type, or a length-1 string for a
|
||||
|
@ -54,7 +54,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
{
|
||||
jsonp_error_set(error, 1, 1,
|
||||
"Expected a single object, got %i", size);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch(*tok)
|
||||
|
@ -65,38 +65,38 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
{
|
||||
jsonp_error_set(error, 1, 1,
|
||||
"Refusing to handle a NULL string");
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
return(json_string(s));
|
||||
return json_string(s);
|
||||
|
||||
case 'n': /* null */
|
||||
return(json_null());
|
||||
return json_null();
|
||||
|
||||
case 'b': /* boolean */
|
||||
obj = va_arg(*ap, int) ?
|
||||
json_true() : json_false();
|
||||
return(obj);
|
||||
return obj;
|
||||
|
||||
case 'i': /* integer */
|
||||
return(json_integer(va_arg(*ap, int)));
|
||||
return json_integer(va_arg(*ap, int));
|
||||
|
||||
case 'f': /* double-precision float */
|
||||
return(json_real(va_arg(*ap, double)));
|
||||
return json_real(va_arg(*ap, double));
|
||||
|
||||
case 'O': /* a json_t object; increments refcount */
|
||||
obj = va_arg(*ap, json_t *);
|
||||
json_incref(obj);
|
||||
return(obj);
|
||||
return obj;
|
||||
|
||||
case 'o': /* a json_t object; doesn't increment refcount */
|
||||
obj = va_arg(*ap, json_t *);
|
||||
return(obj);
|
||||
return obj;
|
||||
|
||||
default: /* Whoops! */
|
||||
jsonp_error_set(error, 1, 1,
|
||||
"Didn't understand format character '%c'",
|
||||
*tok);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,7 +120,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Expected KEY, got COMMA!");
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -131,7 +131,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
"Got key/value separator without "
|
||||
"a key preceding it!");
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(!json_is_object(root))
|
||||
|
@ -140,7 +140,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
"Got a key/value separator "
|
||||
"(':') outside an object!");
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -153,7 +153,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Unexpected close-bracket '%c'", *tok);
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if((*tok == ']' && !json_is_array(root)) ||
|
||||
|
@ -162,9 +162,9 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Stray close-array '%c' character", *tok);
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
return(root);
|
||||
return root;
|
||||
|
||||
case '[':
|
||||
case '{':
|
||||
|
@ -184,7 +184,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
"Couldn't find matching close bracket for '%c'",
|
||||
*tok);
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(*tok == *etok)
|
||||
|
@ -207,7 +207,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
error->column += column-1;
|
||||
error->line += line-1;
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
column += etok-tok;
|
||||
tok = etok;
|
||||
|
@ -223,7 +223,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Refusing to handle a NULL string");
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(json_is_object(root) && !key)
|
||||
|
@ -240,7 +240,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
|
|||
obj = json_vnpack(error, 1, tok, ap);
|
||||
if(!obj) {
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
common:
|
||||
|
@ -251,7 +251,7 @@ common:
|
|||
jsonp_error_set(error, line, column,
|
||||
"Expected key, got identifier '%c'!", *tok);
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
json_object_set_new(root, key, obj);
|
||||
|
@ -270,7 +270,7 @@ common:
|
|||
/* Whoops -- we didn't match the close bracket! */
|
||||
jsonp_error_set(error, line, column, "Missing close array or object!");
|
||||
json_decref(root);
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const char *fmt, va_list *ap)
|
||||
|
@ -308,7 +308,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
|
||||
if(size <= 0) {
|
||||
jsonp_error_set(error, 1, 1, "Empty format string!");
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* tok must contain either a container type, or a length-1 string for a
|
||||
|
@ -320,7 +320,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
{
|
||||
jsonp_error_set(error, 1, 1,
|
||||
"Expected a single object, got %i", size);
|
||||
return(-1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch(*tok)
|
||||
|
@ -331,15 +331,15 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Type mismatch! Object (%i) wasn't a string.",
|
||||
json_typeof(root));
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
s = va_arg(*ap, const char **);
|
||||
if(!s) {
|
||||
jsonp_error_set(error, line, column, "Passed a NULL string pointer!");
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
*s = json_string_value(root);
|
||||
return(0);
|
||||
return 0;
|
||||
|
||||
case 'i':
|
||||
if(!json_is_integer(root))
|
||||
|
@ -347,10 +347,10 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Type mismatch! Object (%i) wasn't an integer.",
|
||||
json_typeof(root));
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
*va_arg(*ap, int*) = json_integer_value(root);
|
||||
return(0);
|
||||
return 0;
|
||||
|
||||
case 'b':
|
||||
if(!json_is_boolean(root))
|
||||
|
@ -358,10 +358,10 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Type mismatch! Object (%i) wasn't a boolean.",
|
||||
json_typeof(root));
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
*va_arg(*ap, int*) = json_is_true(root);
|
||||
return(0);
|
||||
return 0;
|
||||
|
||||
case 'f':
|
||||
if(!json_is_number(root))
|
||||
|
@ -369,10 +369,10 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Type mismatch! Object (%i) wasn't a real.",
|
||||
json_typeof(root));
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
*va_arg(*ap, double*) = json_number_value(root);
|
||||
return(0);
|
||||
return 0;
|
||||
|
||||
case 'O':
|
||||
json_incref(root);
|
||||
|
@ -380,18 +380,18 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
|
||||
case 'o':
|
||||
*va_arg(*ap, json_t**) = root;
|
||||
return(0);
|
||||
return 0;
|
||||
|
||||
case 'n':
|
||||
/* Don't actually assign anything; we're just happy
|
||||
* the null turned up as promised in the format
|
||||
* string. */
|
||||
return(0);
|
||||
return 0;
|
||||
|
||||
default:
|
||||
jsonp_error_set(error, line, column,
|
||||
"Unknown format character '%c'", *tok);
|
||||
return(-1);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -413,7 +413,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
{
|
||||
jsonp_error_set(error, line, column,
|
||||
"Expected KEY, got COMMA!");
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -423,7 +423,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Got key/value separator without "
|
||||
"a key preceding it!");
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
|
||||
if(!json_is_object(root))
|
||||
|
@ -431,7 +431,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Got a key/value separator "
|
||||
"(':') outside an object!");
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -443,7 +443,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
{
|
||||
jsonp_error_set(error, line, column,
|
||||
"Unexpected close-bracket '%c'", *tok);
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
|
||||
if((*tok == ']' && !json_is_array(root)) ||
|
||||
|
@ -451,9 +451,9 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
{
|
||||
jsonp_error_set(error, line, column,
|
||||
"Stray close-array '%c' character", *tok);
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
return(unvisited);
|
||||
return unvisited;
|
||||
|
||||
case '[':
|
||||
case '{':
|
||||
|
@ -467,7 +467,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
jsonp_error_set(error, line, column,
|
||||
"Couldn't find matching close bracket for '%c'",
|
||||
*tok);
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
|
||||
if(*tok == *etok)
|
||||
|
@ -496,7 +496,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
/* error should already be set */
|
||||
error->column += column-1;
|
||||
error->line += line-1;
|
||||
return(rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
unvisited += rv;
|
||||
|
@ -517,7 +517,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
{
|
||||
jsonp_error_set(error, line, column,
|
||||
"Refusing to handle a NULL key");
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -535,12 +535,12 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
if(!obj) {
|
||||
jsonp_error_set(error, line, column,
|
||||
"Array/object entry didn't exist!");
|
||||
return(-1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rv = json_vnunpack(obj, error, 1, tok, ap);
|
||||
if(rv != 0)
|
||||
return(rv);
|
||||
return rv;
|
||||
|
||||
break;
|
||||
}
|
||||
|
@ -550,7 +550,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
|
|||
|
||||
/* Whoops -- we didn't match the close bracket! */
|
||||
jsonp_error_set(error, line, column, "Missing close array or object!");
|
||||
return(-2);
|
||||
return -2;
|
||||
}
|
||||
|
||||
json_t *json_pack(json_error_t *error, const char *fmt, ...)
|
||||
|
@ -562,14 +562,14 @@ json_t *json_pack(json_error_t *error, const char *fmt, ...)
|
|||
|
||||
if(!fmt || !*fmt) {
|
||||
jsonp_error_set(error, 1, 1, "Null or empty format string!");
|
||||
return(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
va_start(ap, fmt);
|
||||
obj = json_vnpack(error, strlen(fmt), fmt, &ap);
|
||||
obj = json_vpack(error, fmt, &ap);
|
||||
va_end(ap);
|
||||
|
||||
return(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...)
|
||||
|
@ -581,15 +581,12 @@ int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...)
|
|||
|
||||
if(!fmt || !*fmt) {
|
||||
jsonp_error_set(error, 1, 1, "Null or empty format string!");
|
||||
return(-2);;
|
||||
return -2;;
|
||||
}
|
||||
|
||||
va_start(ap, fmt);
|
||||
rv = json_vnunpack(root, error, strlen(fmt), fmt, &ap);
|
||||
va_end(ap);
|
||||
|
||||
return(rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* vim: ts=4:expandtab:sw=4
|
||||
*/
|
||||
|
|
|
@ -195,14 +195,9 @@ int main()
|
|||
fail("json_pack didn't get the error coordinates right!");
|
||||
|
||||
if(json_pack(&error, "[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"))
|
||||
fail("json_pack failed to catch missing ]");
|
||||
fail("json_pack failed to catch extra }");
|
||||
if(error.line != 1 || error.column != 21)
|
||||
fail("json_pack didn't get the error coordinates right!");
|
||||
|
||||
return(0);
|
||||
|
||||
//fprintf(stderr, "%i/%i: %s %s\n", error.line, error.column, error.source, error.text);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim: ts=4:expandtab:sw=4
|
||||
*/
|
||||
|
|
|
@ -16,7 +16,6 @@ int main()
|
|||
json_t *j, *j2;
|
||||
int i1, i2, i3;
|
||||
int rv;
|
||||
//void* v;
|
||||
double f;
|
||||
char *s;
|
||||
|
||||
|
@ -37,21 +36,20 @@ int main()
|
|||
fail("json_unpack boolean failed");
|
||||
|
||||
/* null */
|
||||
rv = json_unpack(json_null(), &error, "n");
|
||||
if(rv)
|
||||
if(json_unpack(json_null(), &error, "n"))
|
||||
fail("json_unpack null failed");
|
||||
|
||||
/* integer */
|
||||
j = json_integer(1);
|
||||
j = json_integer(42);
|
||||
rv = json_unpack(j, &error, "i", &i1);
|
||||
if(rv || i1 != 1)
|
||||
if(rv || i1 != 42)
|
||||
fail("json_unpack integer failed");
|
||||
json_decref(j);
|
||||
|
||||
/* real */
|
||||
j = json_real(1.0);
|
||||
j = json_real(1.7);
|
||||
rv = json_unpack(j, &error, "f", &f);
|
||||
if(rv || f != 1.0)
|
||||
if(rv || f != 1.7)
|
||||
fail("json_unpack real failed");
|
||||
json_decref(j);
|
||||
|
||||
|
@ -64,37 +62,35 @@ int main()
|
|||
|
||||
/* empty object */
|
||||
j = json_object();
|
||||
rv = json_unpack(j, &error, "{}");
|
||||
if(rv)
|
||||
if(json_unpack(j, &error, "{}"))
|
||||
fail("json_unpack empty object failed");
|
||||
json_decref(j);
|
||||
|
||||
/* empty list */
|
||||
j = json_array();
|
||||
rv = json_unpack(j, &error, "[]");
|
||||
if(rv)
|
||||
if(json_unpack(j, &error, "[]"))
|
||||
fail("json_unpack empty list failed");
|
||||
json_decref(j);
|
||||
|
||||
/* non-incref'd object */
|
||||
j = json_object();
|
||||
rv = json_unpack(j, &error, "o", &j2);
|
||||
if(j2 != j || j->refcount != (ssize_t)1)
|
||||
if(j2 != j || j->refcount != 1)
|
||||
fail("json_unpack object failed");
|
||||
json_decref(j);
|
||||
|
||||
/* incref'd object */
|
||||
j = json_object();
|
||||
rv = json_unpack(j, &error, "O", &j2);
|
||||
if(j2 != j || j->refcount != (ssize_t)2)
|
||||
if(j2 != j || j->refcount != 2)
|
||||
fail("json_unpack object failed");
|
||||
json_decref(j);
|
||||
json_decref(j);
|
||||
|
||||
/* simple object */
|
||||
j = json_pack(&error, "{s:i}", "foo", 1);
|
||||
j = json_pack(&error, "{s:i}", "foo", 42);
|
||||
rv = json_unpack(j, &error, "{s:i}", "foo", &i1);
|
||||
if(rv || i1!=1)
|
||||
if(rv || i1 != 42)
|
||||
fail("json_unpack simple object failed");
|
||||
json_decref(j);
|
||||
|
||||
|
@ -111,49 +107,38 @@ int main()
|
|||
|
||||
/* mismatched open/close array/object */
|
||||
j = json_pack(&error, "[]");
|
||||
rv = json_unpack(j, &error, "[}");
|
||||
if(!rv)
|
||||
if(!json_unpack(j, &error, "[}"))
|
||||
fail("json_unpack failed to catch mismatched ']'");
|
||||
json_decref(j);
|
||||
|
||||
j = json_pack(&error, "{}");
|
||||
rv = json_unpack(j, &error, "{]");
|
||||
if(!rv)
|
||||
if(!json_unpack(j, &error, "{]"))
|
||||
fail("json_unpack failed to catch mismatched '}'");
|
||||
json_decref(j);
|
||||
|
||||
/* missing close array */
|
||||
j = json_pack(&error, "[]");
|
||||
rv = json_unpack(j, &error, "[");
|
||||
if(rv >= 0)
|
||||
if(!json_unpack(j, &error, "["))
|
||||
fail("json_unpack failed to catch missing ']'");
|
||||
json_decref(j);
|
||||
|
||||
/* missing close object */
|
||||
j = json_pack(&error, "{}");
|
||||
rv = json_unpack(j, &error, "{");
|
||||
if(rv >= 0)
|
||||
if(!json_unpack(j, &error, "{"))
|
||||
fail("json_unpack failed to catch missing '}'");
|
||||
json_decref(j);
|
||||
|
||||
/* NULL format string */
|
||||
j = json_pack(&error, "[]");
|
||||
rv =json_unpack(j, &error, NULL);
|
||||
if(rv >= 0)
|
||||
if(!json_unpack(j, &error, NULL))
|
||||
fail("json_unpack failed to catch null format string");
|
||||
json_decref(j);
|
||||
|
||||
/* NULL string pointer */
|
||||
j = json_string("foobie");
|
||||
rv =json_unpack(j, &error, "s", NULL);
|
||||
if(rv >= 0)
|
||||
if(!json_unpack(j, &error, "s", NULL))
|
||||
fail("json_unpack failed to catch null string pointer");
|
||||
json_decref(j);
|
||||
|
||||
return 0;
|
||||
|
||||
//fprintf(stderr, "%i/%i: %s %s\n", error.line, error.column, error.source, error.text);
|
||||
}
|
||||
|
||||
/* vim: ts=4:expandtab:sw=4
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue