make style

This commit is contained in:
Philippe Teuwen 2019-03-10 00:00:59 +01:00
commit 0373696662
483 changed files with 56514 additions and 52451 deletions

View file

@ -34,7 +34,7 @@ typedef struct {
#define token(scanner) ((scanner)->token.token)
static const char * const type_names[] = {
static const char *const type_names[] = {
"object",
"array",
"string",
@ -69,7 +69,7 @@ static void next_token(scanner_t *s)
const char *t;
s->prev_token = s->token;
if(s->next_token.line) {
if (s->next_token.line) {
s->token = s->next_token;
s->next_token.line = 0;
return;
@ -83,12 +83,11 @@ static void next_token(scanner_t *s)
s->pos++;
/* skip space and ignored chars */
while(*t == ' ' || *t == '\t' || *t == '\n' || *t == ',' || *t == ':') {
if(*t == '\n') {
while (*t == ' ' || *t == '\t' || *t == '\n' || *t == ',' || *t == ':') {
if (*t == '\n') {
s->line++;
s->column = 1;
}
else
} else
s->column++;
s->pos++;
@ -142,11 +141,11 @@ static char *read_string(scanner_t *s, va_list *ap,
prev_token(s);
*ours = 0;
if(t != '#' && t != '%' && t != '+') {
if (t != '#' && t != '%' && t != '+') {
/* Optimize the simple case */
str = va_arg(*ap, const char *);
if(!str) {
if (!str) {
if (!optional) {
set_error(s, "<args>", json_error_null_value, "NULL %s", purpose);
s->has_error = 1;
@ -156,7 +155,7 @@ static char *read_string(scanner_t *s, va_list *ap,
length = strlen(str);
if(!utf8_check_string(str, length)) {
if (!utf8_check_string(str, length)) {
set_error(s, "<args>", json_error_invalid_utf8, "Invalid UTF-8 %s", purpose);
s->has_error = 1;
return NULL;
@ -171,49 +170,47 @@ static char *read_string(scanner_t *s, va_list *ap,
return NULL;
}
if(strbuffer_init(&strbuff)) {
if (strbuffer_init(&strbuff)) {
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
s->has_error = 1;
}
while(1) {
while (1) {
str = va_arg(*ap, const char *);
if(!str) {
if (!str) {
set_error(s, "<args>", json_error_null_value, "NULL %s", purpose);
s->has_error = 1;
}
next_token(s);
if(token(s) == '#') {
if (token(s) == '#') {
length = va_arg(*ap, int);
}
else if(token(s) == '%') {
} else if (token(s) == '%') {
length = va_arg(*ap, size_t);
}
else {
} else {
prev_token(s);
length = s->has_error ? 0 : strlen(str);
}
if(!s->has_error && strbuffer_append_bytes(&strbuff, str, length) == -1) {
if (!s->has_error && strbuffer_append_bytes(&strbuff, str, length) == -1) {
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
s->has_error = 1;
}
next_token(s);
if(token(s) != '+') {
if (token(s) != '+') {
prev_token(s);
break;
}
}
if(s->has_error) {
if (s->has_error) {
strbuffer_close(&strbuff);
return NULL;
}
if(!utf8_check_string(strbuff.value, strbuff.length)) {
if (!utf8_check_string(strbuff.value, strbuff.length)) {
set_error(s, "<args>", json_error_invalid_utf8, "Invalid UTF-8 %s", purpose);
strbuffer_close(&strbuff);
s->has_error = 1;
@ -230,19 +227,19 @@ static json_t *pack_object(scanner_t *s, va_list *ap)
json_t *object = json_object();
next_token(s);
while(token(s) != '}') {
while (token(s) != '}') {
char *key;
size_t len;
int ours;
json_t *value;
char valueOptional;
if(!token(s)) {
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
goto error;
}
if(token(s) != 's') {
if (token(s) != 's') {
set_error(s, "<format>", json_error_invalid_format, "Expected format 's', got '%c'", token(s));
goto error;
}
@ -256,11 +253,11 @@ static json_t *pack_object(scanner_t *s, va_list *ap)
prev_token(s);
value = pack(s, ap);
if(!value) {
if(ours)
if (!value) {
if (ours)
jsonp_free(key);
if(valueOptional != '*') {
if (valueOptional != '*') {
set_error(s, "<args>", json_error_null_value, "NULL object value");
s->has_error = 1;
}
@ -269,21 +266,21 @@ static json_t *pack_object(scanner_t *s, va_list *ap)
continue;
}
if(s->has_error)
if (s->has_error)
json_decref(value);
if(!s->has_error && json_object_set_new_nocheck(object, key, value)) {
if (!s->has_error && json_object_set_new_nocheck(object, key, value)) {
set_error(s, "<internal>", json_error_out_of_memory, "Unable to add key \"%s\"", key);
s->has_error = 1;
}
if(ours)
if (ours)
jsonp_free(key);
next_token(s);
}
if(!s->has_error)
if (!s->has_error)
return object;
error:
@ -296,11 +293,11 @@ static json_t *pack_array(scanner_t *s, va_list *ap)
json_t *array = json_array();
next_token(s);
while(token(s) != ']') {
while (token(s) != ']') {
json_t *value;
char valueOptional;
if(!token(s)) {
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
/* Format string errors are unrecoverable. */
goto error;
@ -311,8 +308,8 @@ static json_t *pack_array(scanner_t *s, va_list *ap)
prev_token(s);
value = pack(s, ap);
if(!value) {
if(valueOptional != '*') {
if (!value) {
if (valueOptional != '*') {
s->has_error = 1;
}
@ -320,10 +317,10 @@ static json_t *pack_array(scanner_t *s, va_list *ap)
continue;
}
if(s->has_error)
if (s->has_error)
json_decref(value);
if(!s->has_error && json_array_append_new(array, value)) {
if (!s->has_error && json_array_append_new(array, value)) {
set_error(s, "<internal>", json_error_out_of_memory, "Unable to append to array");
s->has_error = 1;
}
@ -331,7 +328,7 @@ static json_t *pack_array(scanner_t *s, va_list *ap)
next_token(s);
}
if(!s->has_error)
if (!s->has_error)
return array;
error:
@ -437,7 +434,7 @@ static json_t *pack_real(scanner_t *s, double value)
static json_t *pack(scanner_t *s, va_list *ap)
{
switch(token(s)) {
switch (token(s)) {
case '{':
return pack_object(s, ap);
@ -491,81 +488,80 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
*/
hashtable_t key_set;
if(hashtable_init(&key_set)) {
if (hashtable_init(&key_set)) {
set_error(s, "<internal>", json_error_out_of_memory, "Out of memory");
return -1;
}
if(root && !json_is_object(root)) {
if (root && !json_is_object(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected object, got %s",
type_name(root));
goto out;
}
next_token(s);
while(token(s) != '}') {
while (token(s) != '}') {
const char *key;
json_t *value;
int opt = 0;
if(strict != 0) {
if (strict != 0) {
set_error(s, "<format>", json_error_invalid_format, "Expected '}' after '%c', got '%c'",
(strict == 1 ? '!' : '*'), token(s));
goto out;
}
if(!token(s)) {
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
goto out;
}
if(token(s) == '!' || token(s) == '*') {
if (token(s) == '!' || token(s) == '*') {
strict = (token(s) == '!' ? 1 : -1);
next_token(s);
continue;
}
if(token(s) != 's') {
if (token(s) != 's') {
set_error(s, "<format>", json_error_invalid_format, "Expected format 's', got '%c'", token(s));
goto out;
}
key = va_arg(*ap, const char *);
if(!key) {
if (!key) {
set_error(s, "<args>", json_error_null_value, "NULL object key");
goto out;
}
next_token(s);
if(token(s) == '?') {
if (token(s) == '?') {
opt = gotopt = 1;
next_token(s);
}
if(!root) {
if (!root) {
/* skipping */
value = NULL;
}
else {
} else {
value = json_object_get(root, key);
if(!value && !opt) {
if (!value && !opt) {
set_error(s, "<validation>", json_error_item_not_found, "Object item not found: %s", key);
goto out;
}
}
if(unpack(s, value, ap))
if (unpack(s, value, ap))
goto out;
hashtable_set(&key_set, key, json_null());
next_token(s);
}
if(strict == 0 && (s->flags & JSON_STRICT))
if (strict == 0 && (s->flags & JSON_STRICT))
strict = 1;
if(root && strict == 1) {
if (root && strict == 1) {
/* We need to check that all non optional items have been parsed */
const char *key;
/* keys_res is 1 for uninitialized, 0 for success, -1 for error. */
@ -576,7 +572,7 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
if (gotopt || json_object_size(root) != key_set.size) {
json_object_foreach(root, key, value) {
if(!hashtable_get(&key_set, key)) {
if (!hashtable_get(&key_set, key)) {
unpacked++;
/* Save unrecognized keys for the error message */
@ -613,63 +609,62 @@ static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
size_t i = 0;
int strict = 0;
if(root && !json_is_array(root)) {
if (root && !json_is_array(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected array, got %s", type_name(root));
return -1;
}
next_token(s);
while(token(s) != ']') {
while (token(s) != ']') {
json_t *value;
if(strict != 0) {
if (strict != 0) {
set_error(s, "<format>", json_error_invalid_format, "Expected ']' after '%c', got '%c'",
(strict == 1 ? '!' : '*'),
token(s));
return -1;
}
if(!token(s)) {
if (!token(s)) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected end of format string");
return -1;
}
if(token(s) == '!' || token(s) == '*') {
if (token(s) == '!' || token(s) == '*') {
strict = (token(s) == '!' ? 1 : -1);
next_token(s);
continue;
}
if(!strchr(unpack_value_starters, token(s))) {
if (!strchr(unpack_value_starters, token(s))) {
set_error(s, "<format>", json_error_invalid_format, "Unexpected format character '%c'",
token(s));
return -1;
}
if(!root) {
if (!root) {
/* skipping */
value = NULL;
}
else {
} else {
value = json_array_get(root, i);
if(!value) {
if (!value) {
set_error(s, "<validation>", json_error_index_out_of_range, "Array index %lu out of range",
(unsigned long)i);
return -1;
}
}
if(unpack(s, value, ap))
if (unpack(s, value, ap))
return -1;
next_token(s);
i++;
}
if(strict == 0 && (s->flags & JSON_STRICT))
if (strict == 0 && (s->flags & JSON_STRICT))
strict = 1;
if(root && strict == 1 && i != json_array_size(root)) {
if (root && strict == 1 && i != json_array_size(root)) {
long diff = (long)json_array_size(root) - (long)i;
set_error(s, "<validation>", json_error_end_of_input_expected, "%li array item(s) left unpacked", diff);
return -1;
@ -680,8 +675,7 @@ static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
static int unpack(scanner_t *s, json_t *root, va_list *ap)
{
switch(token(s))
{
switch (token(s)) {
case '{':
return unpack_object(s, root, ap);
@ -689,126 +683,125 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
return unpack_array(s, root, ap);
case 's':
if(root && !json_is_string(root)) {
if (root && !json_is_string(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected string, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
if (!(s->flags & JSON_VALIDATE_ONLY)) {
const char **str_target;
size_t *len_target = NULL;
str_target = va_arg(*ap, const char **);
if(!str_target) {
if (!str_target) {
set_error(s, "<args>", json_error_null_value, "NULL string argument");
return -1;
}
next_token(s);
if(token(s) == '%') {
if (token(s) == '%') {
len_target = va_arg(*ap, size_t *);
if(!len_target) {
if (!len_target) {
set_error(s, "<args>", json_error_null_value, "NULL string length argument");
return -1;
}
}
else
} else
prev_token(s);
if(root) {
if (root) {
*str_target = json_string_value(root);
if(len_target)
if (len_target)
*len_target = json_string_length(root);
}
}
return 0;
case 'i':
if(root && !json_is_integer(root)) {
if (root && !json_is_integer(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected integer, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
int *target = va_arg(*ap, int*);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
int *target = va_arg(*ap, int *);
if (root)
*target = (int)json_integer_value(root);
}
return 0;
case 'I':
if(root && !json_is_integer(root)) {
if (root && !json_is_integer(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected integer, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
json_int_t *target = va_arg(*ap, json_int_t*);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
json_int_t *target = va_arg(*ap, json_int_t *);
if (root)
*target = json_integer_value(root);
}
return 0;
case 'b':
if(root && !json_is_boolean(root)) {
if (root && !json_is_boolean(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected true or false, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
int *target = va_arg(*ap, int*);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
int *target = va_arg(*ap, int *);
if (root)
*target = json_is_true(root);
}
return 0;
case 'f':
if(root && !json_is_real(root)) {
if (root && !json_is_real(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected real, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
double *target = va_arg(*ap, double*);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
double *target = va_arg(*ap, double *);
if (root)
*target = json_real_value(root);
}
return 0;
case 'F':
if(root && !json_is_number(root)) {
if (root && !json_is_number(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected real or integer, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
double *target = va_arg(*ap, double*);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
double *target = va_arg(*ap, double *);
if (root)
*target = json_number_value(root);
}
return 0;
case 'O':
if(root && !(s->flags & JSON_VALIDATE_ONLY))
if (root && !(s->flags & JSON_VALIDATE_ONLY))
json_incref(root);
/* Fall through */
/* Fall through */
case 'o':
if(!(s->flags & JSON_VALIDATE_ONLY)) {
json_t **target = va_arg(*ap, json_t**);
if(root)
if (!(s->flags & JSON_VALIDATE_ONLY)) {
json_t **target = va_arg(*ap, json_t **);
if (root)
*target = root;
}
@ -816,7 +809,7 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
case 'n':
/* Never assign, just validate */
if(root && !json_is_null(root)) {
if (root && !json_is_null(root)) {
set_error(s, "<validation>", json_error_wrong_type, "Expected null, got %s",
type_name(root));
return -1;
@ -837,7 +830,7 @@ json_t *json_vpack_ex(json_error_t *error, size_t flags,
va_list ap_copy;
json_t *value;
if(!fmt || !*fmt) {
if (!fmt || !*fmt) {
jsonp_error_init(error, "<format>");
jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument, "NULL or empty format string");
return NULL;
@ -852,11 +845,11 @@ json_t *json_vpack_ex(json_error_t *error, size_t flags,
va_end(ap_copy);
/* This will cover all situations where s.has_error is true */
if(!value)
if (!value)
return NULL;
next_token(&s);
if(token(&s)) {
if (token(&s)) {
json_decref(value);
set_error(&s, "<format>", json_error_invalid_format, "Garbage after format string");
return NULL;
@ -895,13 +888,13 @@ int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
scanner_t s;
va_list ap_copy;
if(!root) {
if (!root) {
jsonp_error_init(error, "<root>");
jsonp_error_set(error, -1, -1, 0, json_error_null_value, "NULL root value");
return -1;
}
if(!fmt || !*fmt) {
if (!fmt || !*fmt) {
jsonp_error_init(error, "<format>");
jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument, "NULL or empty format string");
return -1;
@ -912,14 +905,14 @@ int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
next_token(&s);
va_copy(ap_copy, ap);
if(unpack(&s, root, &ap_copy)) {
if (unpack(&s, root, &ap_copy)) {
va_end(ap_copy);
return -1;
}
va_end(ap_copy);
next_token(&s);
if(token(&s)) {
if (token(&s)) {
set_error(&s, "<format>", json_error_invalid_format, "Garbage after format string");
return -1;
}