Merge pull request #255 from RyDroid/minor-c2

Minor changes in C source code
This commit is contained in:
Eric Haszlakiewicz
2016-08-10 15:06:30 -04:00
committed by GitHub
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);
lh_foreach(json_object_table, ent)
{
struct json_object* obj = (struct json_object*)lh_entry_v(ent);
MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj);
struct json_object* 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(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",
json_hex_chars[c >> 4],
json_hex_chars[c & 0xf]);
@@ -168,7 +172,7 @@ extern struct json_object* json_object_get(struct json_object *jso)
{
if (jso)
jso->_ref_count++;
return jso;
return NULL;
}
int json_object_put(struct json_object *jso)
@@ -353,7 +357,7 @@ static void indent(struct printbuf *pb, int level, int flags)
static int json_object_object_to_json_string(struct json_object* jso,
struct printbuf *pb,
int level,
int flags)
int flags)
{
int had_children = 0;
struct json_object_iter iter;
@@ -418,7 +422,7 @@ struct json_object* json_object_new_object(void)
jso->_delete = &json_object_object_delete;
jso->_to_json_string = &json_object_object_to_json_string;
jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
&json_object_lh_entry_free);
&json_object_lh_entry_free);
if (!jso->o.c_object)
{
json_object_generic_delete(jso);
@@ -452,7 +456,8 @@ void json_object_object_add_ex(struct json_object* jso,
struct lh_entry *existing_entry;
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 :
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)
{
const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
@@ -460,21 +465,22 @@ void json_object_object_add_ex(struct json_object* jso,
lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
return;
}
existing_value = (json_object *)lh_entry_v(existing_entry);
existing_value = (json_object *) lh_entry_v(existing_entry);
if (existing_value)
json_object_put(existing_value);
existing_entry->v = val;
}
int json_object_object_add(struct json_object* jso, const char *key,
struct json_object *val)
struct json_object *val)
{
// We lookup the entry and replace the value, rather than just deleting
// and re-adding it, so the existing key remains valid.
json_object *existing_value = NULL;
struct lh_entry *existing_entry;
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)
{
char *keydup = strdup(key);
@@ -497,14 +503,16 @@ int json_object_object_length(const struct json_object *jso)
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;
json_object_object_get_ex(jso, key, &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)
*value = NULL;
@@ -515,7 +523,8 @@ json_bool json_object_object_get_ex(const struct json_object* jso, const char *k
switch(jso->o_type)
{
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:
if (value != NULL)
*value = NULL;
@@ -534,12 +543,11 @@ void json_object_object_del(struct json_object* jso, const char *key)
static int json_object_boolean_to_json_string(struct json_object* jso,
struct printbuf *pb,
int level,
int flags)
int flags)
{
if (jso->o.c_boolean)
return sprintbuf(pb, "true");
else
return sprintbuf(pb, "false");
return sprintbuf(pb, "false");
}
struct json_object* json_object_new_boolean(json_bool b)
@@ -618,10 +626,9 @@ int32_t json_object_get_int(const struct json_object *jso)
/* Make sure we return the correct values for out of range numbers. */
if (cint64 <= INT32_MIN)
return INT32_MIN;
else if (cint64 >= INT32_MAX)
if (cint64 >= INT32_MAX)
return INT32_MAX;
else
return (int32_t)cint64;
return (int32_t) cint64;
case json_type_double:
return (int32_t)jso->o.c_double;
case json_type_boolean:
@@ -823,7 +830,7 @@ double json_object_get_double(const struct json_object *jso)
static int json_object_string_to_json_string(struct json_object* jso,
struct printbuf *pb,
int level,
int flags)
int flags)
{
sprintbuf(pb, "\"");
json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
@@ -953,8 +960,7 @@ static int json_object_array_to_json_string(struct json_object* jso,
if (flags & JSON_C_TO_STRING_SPACED)
return sprintbuf(pb, " ]");
else
return sprintbuf(pb, "]");
return sprintbuf(pb, "]");
}
static void json_object_array_entry_free(void *data)
@@ -997,7 +1003,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);
}