Minor changes in C source code

This commit is contained in:
Nicola Spanti (RyDroid)
2016-08-08 15:20:41 +02:00
parent 9688f343a5
commit f40b08d8f0
2 changed files with 69 additions and 54 deletions

View File

@@ -84,8 +84,10 @@ static void json_object_fini(void)
json_object_table->count); json_object_table->count);
lh_foreach(json_object_table, ent) lh_foreach(json_object_table, ent)
{ {
struct json_object* obj = (struct json_object*)lh_entry_v(ent); struct json_object* obj =
MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj); (struct json_object*) lh_entry_v(ent);
MC_DEBUG("\t%s:%p\n",
json_type_to_name(obj->o_type), obj);
} }
} }
} }
@@ -147,7 +149,9 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl
if(c < ' ') if(c < ' ')
{ {
if(pos - start_offset > 0) if(pos - start_offset > 0)
printbuf_memappend(pb, str + start_offset, pos - start_offset); printbuf_memappend(pb,
str + start_offset,
pos - start_offset);
sprintbuf(pb, "\\u00%c%c", sprintbuf(pb, "\\u00%c%c",
json_hex_chars[c >> 4], json_hex_chars[c >> 4],
json_hex_chars[c & 0xf]); json_hex_chars[c & 0xf]);
@@ -168,7 +172,7 @@ extern struct json_object* json_object_get(struct json_object *jso)
{ {
if (jso) if (jso)
jso->_ref_count++; jso->_ref_count++;
return jso; return NULL;
} }
int json_object_put(struct json_object *jso) int json_object_put(struct json_object *jso)
@@ -437,7 +441,8 @@ void json_object_object_add_ex(struct json_object* jso,
struct lh_entry *existing_entry; struct lh_entry *existing_entry;
const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key); const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key);
existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL : existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
lh_table_lookup_entry_w_hash(jso->o.c_object, (const void *)key, hash); lh_table_lookup_entry_w_hash(jso->o.c_object,
(const void *)key, hash);
if (!existing_entry) if (!existing_entry)
{ {
const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ? const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
@@ -445,7 +450,7 @@ void json_object_object_add_ex(struct json_object* jso,
lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts); lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
return; return;
} }
existing_value = (json_object *)lh_entry_v(existing_entry); existing_value = (json_object *) lh_entry_v(existing_entry);
if (existing_value) if (existing_value)
json_object_put(existing_value); json_object_put(existing_value);
existing_entry->v = val; existing_entry->v = val;
@@ -459,7 +464,8 @@ int json_object_object_add(struct json_object* jso, const char *key,
json_object *existing_value = NULL; json_object *existing_value = NULL;
struct lh_entry *existing_entry; struct lh_entry *existing_entry;
const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key); const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key);
existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object, (const void *)key, hash); existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object,
(const void *)key, hash);
if (!existing_entry) if (!existing_entry)
{ {
char *keydup = strdup(key); char *keydup = strdup(key);
@@ -482,14 +488,16 @@ int json_object_object_length(const struct json_object *jso)
return lh_table_length(jso->o.c_object); return lh_table_length(jso->o.c_object);
} }
struct json_object* json_object_object_get(const struct json_object* jso, const char *key) struct json_object* json_object_object_get(const struct json_object* jso,
const char *key)
{ {
struct json_object *result = NULL; struct json_object *result = NULL;
json_object_object_get_ex(jso, key, &result); json_object_object_get_ex(jso, key, &result);
return result; return result;
} }
json_bool json_object_object_get_ex(const struct json_object* jso, const char *key, struct json_object **value) json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
struct json_object **value)
{ {
if (value != NULL) if (value != NULL)
*value = NULL; *value = NULL;
@@ -500,7 +508,8 @@ json_bool json_object_object_get_ex(const struct json_object* jso, const char *k
switch(jso->o_type) switch(jso->o_type)
{ {
case json_type_object: case json_type_object:
return lh_table_lookup_ex(jso->o.c_object, (const void *)key, (void**)value); return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
(void**) value);
default: default:
if (value != NULL) if (value != NULL)
*value = NULL; *value = NULL;
@@ -523,7 +532,6 @@ static int json_object_boolean_to_json_string(struct json_object* jso,
{ {
if (jso->o.c_boolean) if (jso->o.c_boolean)
return sprintbuf(pb, "true"); return sprintbuf(pb, "true");
else
return sprintbuf(pb, "false"); return sprintbuf(pb, "false");
} }
@@ -603,10 +611,9 @@ int32_t json_object_get_int(const struct json_object *jso)
/* Make sure we return the correct values for out of range numbers. */ /* Make sure we return the correct values for out of range numbers. */
if (cint64 <= INT32_MIN) if (cint64 <= INT32_MIN)
return INT32_MIN; return INT32_MIN;
else if (cint64 >= INT32_MAX) if (cint64 >= INT32_MAX)
return INT32_MAX; return INT32_MAX;
else return (int32_t) cint64;
return (int32_t)cint64;
case json_type_double: case json_type_double:
return (int32_t)jso->o.c_double; return (int32_t)jso->o.c_double;
case json_type_boolean: case json_type_boolean:
@@ -938,7 +945,6 @@ static int json_object_array_to_json_string(struct json_object* jso,
if (flags & JSON_C_TO_STRING_SPACED) if (flags & JSON_C_TO_STRING_SPACED)
return sprintbuf(pb, " ]"); return sprintbuf(pb, " ]");
else
return sprintbuf(pb, "]"); return sprintbuf(pb, "]");
} }
@@ -982,7 +988,8 @@ struct array_list* json_object_get_array(const struct json_object *jso)
} }
} }
void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *)) void json_object_array_sort(struct json_object *jso,
int(*sort_fn)(const void *, const void *))
{ {
array_list_sort(jso->o.c_array, sort_fn); array_list_sort(jso->o.c_array, sort_fn);
} }

View File

@@ -91,9 +91,11 @@ static const char* json_tokener_errors[] = {
const char *json_tokener_error_desc(enum json_tokener_error jerr) const char *json_tokener_error_desc(enum json_tokener_error jerr)
{ {
int jerr_int = (int)jerr; int jerr_int = (int) jerr;
if (jerr_int < 0 || jerr_int >= (int)(sizeof(json_tokener_errors) / sizeof(json_tokener_errors[0]))) if (jerr_int < 0 ||
return "Unknown error, invalid json_tokener_error value passed to json_tokener_error_desc()"; jerr_int >= (int)(sizeof(json_tokener_errors) / sizeof(json_tokener_errors[0])))
return "Unknown error, "
"invalid json_tokener_error value passed to json_tokener_error_desc()";
return json_tokener_errors[jerr]; return json_tokener_errors[jerr];
} }
@@ -114,7 +116,8 @@ struct json_tokener* json_tokener_new_ex(int depth)
tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener)); tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener));
if (!tok) return NULL; if (!tok) return NULL;
tok->stack = (struct json_tokener_srec *)calloc(depth, sizeof(struct json_tokener_srec)); tok->stack = (struct json_tokener_srec *) calloc(depth,
sizeof(struct json_tokener_srec));
if (!tok->stack) { if (!tok->stack) {
free(tok); free(tok);
return NULL; return NULL;
@@ -168,7 +171,8 @@ struct json_object* json_tokener_parse(const char *str)
return obj; return obj;
} }
struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokener_error *error) struct json_object* json_tokener_parse_verbose(const char *str,
enum json_tokener_error *error)
{ {
struct json_tokener* tok; struct json_tokener* tok;
struct json_object* obj; struct json_object* obj;
@@ -213,7 +217,10 @@ struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokene
*/ */
#define PEEK_CHAR(dest, tok) \ #define PEEK_CHAR(dest, tok) \
(((tok)->char_offset == len) ? \ (((tok)->char_offset == len) ? \
(((tok)->depth == 0 && state == json_tokener_state_eatws && saved_state == json_tokener_state_finish) ? \ (((tok)->depth == 0 && \
state == json_tokener_state_eatws && \
saved_state == json_tokener_state_finish \
) ? \
(((tok)->err = json_tokener_success), 0) \ (((tok)->err = json_tokener_success), 0) \
: \ : \
(((tok)->err = json_tokener_continue), 0) \ (((tok)->err = json_tokener_continue), 0) \
@@ -353,9 +360,6 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
printbuf_reset(tok->pb); printbuf_reset(tok->pb);
tok->st_pos = 0; tok->st_pos = 0;
goto redo_char; goto redo_char;
#if defined(__GNUC__)
case '0' ... '9':
#else
case '0': case '0':
case '1': case '1':
case '2': case '2':
@@ -366,7 +370,6 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
case '7': case '7':
case '8': case '8':
case '9': case '9':
#endif
case '-': case '-':
state = json_tokener_state_number; state = json_tokener_state_number;
printbuf_reset(tok->pb); printbuf_reset(tok->pb);
@@ -405,7 +408,8 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
{ {
if (tok->st_pos == json_inf_str_len) if (tok->st_pos == json_inf_str_len)
{ {
current = json_object_new_double(is_negative ? -INFINITY : INFINITY); current = json_object_new_double(is_negative
? -INFINITY : INFINITY);
if(current == NULL) if(current == NULL)
goto out; goto out;
saved_state = json_tokener_state_finish; saved_state = json_tokener_state_finish;
@@ -617,13 +621,15 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
* characters. * characters.
*/ */
if( !ADVANCE_CHAR(str, tok) || !ADVANCE_CHAR(str, tok) ) { if( !ADVANCE_CHAR(str, tok) || !ADVANCE_CHAR(str, tok) ) {
printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); printbuf_memappend_fast(tok->pb,
(char*) utf8_replacement_char, 3);
} }
/* Advance to the first char of the next sequence and /* Advance to the first char of the next sequence and
* continue processing with the next sequence. * continue processing with the next sequence.
*/ */
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) {
printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); printbuf_memappend_fast(tok->pb,
(char*) utf8_replacement_char, 3);
goto out; goto out;
} }
tok->ucs_char = 0; tok->ucs_char = 0;
@@ -634,7 +640,8 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
* it. Put a replacement char in for the hi surrogate * it. Put a replacement char in for the hi surrogate
* and pretend we finished. * and pretend we finished.
*/ */
printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); printbuf_memappend_fast(tok->pb,
(char*) utf8_replacement_char, 3);
} }
} else if (IS_LOW_SURROGATE(tok->ucs_char)) { } else if (IS_LOW_SURROGATE(tok->ucs_char)) {
/* Got a low surrogate not preceded by a high */ /* Got a low surrogate not preceded by a high */
@@ -771,7 +778,8 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
int64_t num64; int64_t num64;
double numd; double numd;
if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) { if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) {
if (num64 && tok->pb->buf[0]=='0' && (tok->flags & JSON_TOKENER_STRICT)) { if (num64 && tok->pb->buf[0]=='0' &&
(tok->flags & JSON_TOKENER_STRICT)) {
/* in strict mode, number must not start with 0 */ /* in strict mode, number must not start with 0 */
tok->err = json_tokener_error_parse_number; tok->err = json_tokener_error_parse_number;
goto out; goto out;
@@ -971,5 +979,5 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
void json_tokener_set_flags(struct json_tokener *tok, int flags) void json_tokener_set_flags(struct json_tokener *tok, int flags)
{ {
tok->flags = flags; if(tok) tok->flags = flags;
} }