Unify style

This commit is contained in:
Petri Lehtinen 2011-01-20 21:09:14 +02:00
parent fa7c2ea070
commit 53383860e8
3 changed files with 116 additions and 139 deletions

View file

@ -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.
@ -26,8 +26,8 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
char *key = NULL; /* Current key in an object */
char *s;
int line=1;
int column=1;
int line = 1;
int column = 1;
/* Skip whitespace at the beginning of the string. */
while(size && *tok == ' ') {
@ -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,49 +54,49 @@ 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)
{
case 's': /* string */
s = va_arg(*ap, char*);
s = va_arg(*ap, char *);
if(!s)
{
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;
}
}
@ -108,7 +108,7 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
switch(*tok) {
case '\n':
line++;
column=0;
column = 0;
break;
case ' ': /* Whitespace */
@ -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,15 +184,15 @@ 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)
if(*tok == *etok)
etok_depth++;
else if(*tok=='[' && *etok==']') {
else if(*tok == '[' && *etok == ']') {
etok_depth--;
break;
} else if(*tok=='{' && *etok=='}') {
} else if(*tok == '{' && *etok == '}') {
etok_depth--;
break;
}
@ -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;
@ -216,14 +216,14 @@ static json_t *json_vnpack(json_error_t *error, ssize_t size, const char * const
case 's':
/* Handle strings specially, since they're used for both keys
* and values */
s = va_arg(*ap, char*);
s = va_arg(*ap, char *);
if(!s)
{
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,13 +270,13 @@ 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)
{
int rv=0; /* Return value */
int rv = 0; /* Return value */
int line = 1; /* Line number */
int column = 1; /* Column */
@ -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;
}
}
@ -402,7 +402,7 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
switch(*tok) {
case '\n':
line++;
column=0;
column = 0;
break;
case ' ': /* Whitespace */
@ -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,15 +467,15 @@ 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)
if(*tok == *etok)
etok_depth++;
else if(*tok=='[' && *etok==']') {
else if(*tok == '[' && *etok == ']') {
etok_depth--;
break;
} else if(*tok=='{' && *etok=='}') {
} else if(*tok == '{' && *etok == '}') {
etok_depth--;
break;
}
@ -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;
@ -511,13 +511,13 @@ static int json_vnunpack(json_t *root, json_error_t *error, ssize_t size, const
if(json_is_object(root) && !key)
{
/* It's a key */
key = va_arg(*ap, char*);
key = va_arg(*ap, char *);
if(!key)
{
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
*/

View file

@ -24,99 +24,99 @@ int main()
/* true */
value = json_pack(&error, "b", 1);
if(!json_is_true(value))
fail("json_pack boolean failed");
fail("json_pack boolean failed");
if(value->refcount != (ssize_t)-1)
fail("json_pack boolean refcount failed");
fail("json_pack boolean refcount failed");
json_decref(value);
/* false */
value = json_pack(&error, "b", 0);
if(!json_is_false(value))
fail("json_pack boolean failed");
fail("json_pack boolean failed");
if(value->refcount != (ssize_t)-1)
fail("json_pack boolean refcount failed");
fail("json_pack boolean refcount failed");
json_decref(value);
/* null */
value = json_pack(&error, "n");
if(!json_is_null(value))
fail("json_pack null failed");
fail("json_pack null failed");
if(value->refcount != (ssize_t)-1)
fail("json_pack null refcount failed");
fail("json_pack null refcount failed");
json_decref(value);
/* integer */
value = json_pack(&error, "i", 1);
if(!json_is_integer(value) || json_integer_value(value) != 1)
fail("json_pack integer failed");
fail("json_pack integer failed");
if(value->refcount != (ssize_t)1)
fail("json_pack integer refcount failed");
fail("json_pack integer refcount failed");
json_decref(value);
/* real */
value = json_pack(&error, "f", 1.0);
if(!json_is_real(value) || json_real_value(value) != 1.0)
fail("json_pack real failed");
fail("json_pack real failed");
if(value->refcount != (ssize_t)1)
fail("json_pack real refcount failed");
fail("json_pack real refcount failed");
json_decref(value);
/* string */
value = json_pack(&error, "s", "test");
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
fail("json_pack string failed");
fail("json_pack string failed");
if(value->refcount != (ssize_t)1)
fail("json_pack string refcount failed");
fail("json_pack string refcount failed");
json_decref(value);
/* empty object */
value = json_pack(&error, "{}", 1.0);
if(!json_is_object(value) || json_object_size(value) != 0)
fail("json_pack empty object failed");
fail("json_pack empty object failed");
if(value->refcount != (ssize_t)1)
fail("json_pack empty object refcount failed");
fail("json_pack empty object refcount failed");
json_decref(value);
/* empty list */
value = json_pack(&error, "[]", 1.0);
if(!json_is_array(value) || json_array_size(value) != 0)
fail("json_pack empty list failed");
fail("json_pack empty list failed");
if(value->refcount != (ssize_t)1)
fail("json_pack empty list failed");
fail("json_pack empty list failed");
json_decref(value);
/* non-incref'd object */
value = json_pack(&error, "o", json_integer(1));
if(!json_is_integer(value) || json_integer_value(value) != 1)
fail("json_pack object failed");
fail("json_pack object failed");
if(value->refcount != (ssize_t)1)
fail("json_pack integer refcount failed");
fail("json_pack integer refcount failed");
json_decref(value);
/* incref'd object */
value = json_pack(&error, "O", json_integer(1));
if(!json_is_integer(value) || json_integer_value(value) != 1)
fail("json_pack object failed");
fail("json_pack object failed");
if(value->refcount != (ssize_t)2)
fail("json_pack integer refcount failed");
fail("json_pack integer refcount failed");
json_decref(value);
json_decref(value);
/* simple object */
value = json_pack(&error, "{s:[]}", "foo");
if(!json_is_object(value) || json_object_size(value) != 1)
fail("json_pack array failed");
fail("json_pack array failed");
if(!json_is_array(json_object_get(value, "foo")))
fail("json_pack array failed");
fail("json_pack array failed");
if(json_object_get(value, "foo")->refcount != (ssize_t)1)
fail("json_pack object refcount failed");
fail("json_pack object refcount failed");
json_decref(value);
/* simple array */
value = json_pack(&error, "[i,i,i]", 0, 1, 2);
if(!json_is_array(value) || json_array_size(value) != 3)
fail("json_pack object failed");
fail("json_pack object failed");
for(i=0; i<3; i++)
{
if(!json_is_integer(json_array_get(value, i)) ||
@ -129,19 +129,19 @@ int main()
/* Whitespace; regular string */
value = json_pack(&error, " s ", "test");
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
fail("json_pack string (with whitespace) failed");
fail("json_pack string (with whitespace) failed");
json_decref(value);
/* Whitespace; empty array */
value = json_pack(&error, "[ ]");
if(!json_is_array(value) || json_array_size(value) != 0)
fail("json_pack empty array (with whitespace) failed");
fail("json_pack empty array (with whitespace) failed");
json_decref(value);
/* Whitespace; array */
value = json_pack(&error, "[ i , i, i ] ", 1, 2, 3);
if(!json_is_array(value) || json_array_size(value) != 3)
fail("json_pack array (with whitespace) failed");
fail("json_pack array (with whitespace) failed");
json_decref(value);
/*
@ -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
*/

View file

@ -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
*/