Skip to content

Commit

Permalink
json: minor style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
poettering committed May 21, 2015
1 parent 85dbc30 commit dde8bb3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
5 changes: 2 additions & 3 deletions src/import/pull-dkr.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ static void dkr_pull_job_on_finished_v2(PullJob *j) {
} else if (i->tags_job == j) {
const char *url;
_cleanup_free_ const char *buf;
_cleanup_jsonunref_ JsonVariant *doc = NULL;
_cleanup_json_variant_unref_ JsonVariant *doc = NULL;
JsonVariant *e = NULL;

assert(!i->ancestry_job);
Expand Down Expand Up @@ -851,8 +851,7 @@ static void dkr_pull_job_on_finished_v2(PullJob *j) {

} else if (i->ancestry_job == j) {

_cleanup_jsonunref_ JsonVariant *doc = NULL;
_cleanup_jsonunref_ JsonVariant *compat = NULL;
_cleanup_json_variant_unref_ JsonVariant *doc = NULL, *compat = NULL;
JsonVariant *e = NULL;
_cleanup_strv_free_ char **ancestry = NULL;
size_t allocated = 0, size = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/libsystemd-network/sd-dhcp-lease.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
if (r >= 0) {
_cleanup_free_ char *client_id_hex;

client_id_hex = hexmem (client_id, client_id_len);
client_id_hex = hexmem(client_id, client_id_len);
if (!client_id_hex) {
r = -ENOMEM;
goto finish;
Expand Down
33 changes: 21 additions & 12 deletions src/shared/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

int json_variant_new(JsonVariant **ret, JsonVariantType type) {
JsonVariant *v;

v = new0(JsonVariant, 1);
if (!v)
return -ENOMEM;
Expand All @@ -36,6 +37,8 @@ int json_variant_new(JsonVariant **ret, JsonVariantType type) {
}

static int json_variant_deep_copy(JsonVariant *ret, JsonVariant *variant) {
int r;

assert(ret);
assert(variant);

Expand All @@ -47,18 +50,18 @@ static int json_variant_deep_copy(JsonVariant *ret, JsonVariant *variant) {
if (!ret->string)
return -ENOMEM;
} else if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT) {
size_t i;

ret->objects = new0(JsonVariant, variant->size);
if (!ret->objects)
return -ENOMEM;

for (unsigned i = 0; i < variant->size; ++i) {
int r;
for (i = 0; i < variant->size; ++i) {
r = json_variant_deep_copy(&ret->objects[i], &variant->objects[i]);
if (r < 0)
return r;
}
}
else
} else
ret->value = variant->value;

return 0;
Expand All @@ -72,7 +75,6 @@ static JsonVariant *json_variant_unref_inner(JsonVariant *variant) {

if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT)
return json_object_unref(variant);

else if (variant->type == JSON_VARIANT_STRING)
free(variant->string);

Expand All @@ -91,11 +93,14 @@ static JsonVariant *json_raw_unref(JsonVariant *variant, size_t size) {
}

static JsonVariant *json_object_unref(JsonVariant *variant) {
size_t i;

assert(variant);

if (!variant->objects)
return NULL;

for (unsigned i = 0; i < variant->size; ++i)
for (i = 0; i < variant->size; ++i)
json_variant_unref_inner(&variant->objects[i]);

free(variant->objects);
Expand All @@ -119,6 +124,7 @@ static JsonVariant **json_variant_array_unref(JsonVariant **variant) {

return NULL;
}

DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant **, json_variant_array_unref);

JsonVariant *json_variant_unref(JsonVariant *variant) {
Expand All @@ -127,7 +133,6 @@ JsonVariant *json_variant_unref(JsonVariant *variant) {

if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT)
json_object_unref(variant);

else if (variant->type == JSON_VARIANT_STRING)
free(variant->string);

Expand Down Expand Up @@ -174,11 +179,13 @@ JsonVariant *json_variant_element(JsonVariant *variant, unsigned index) {
}

JsonVariant *json_variant_value(JsonVariant *variant, const char *key) {
size_t i;

assert(variant);
assert(variant->type == JSON_VARIANT_OBJECT);
assert(variant->objects);

for (unsigned i = 0; i < variant->size; i += 2) {
for (i = 0; i < variant->size; i += 2) {
JsonVariant *p = &variant->objects[i];
if (p->type == JSON_VARIANT_STRING && streq(key, p->string))
return &variant->objects[i + 1];
Expand Down Expand Up @@ -624,9 +631,11 @@ static int json_scoped_parse(JsonVariant **tokens, size_t *i, size_t n, JsonVari
assert(scope);

while((var = *i < n ? tokens[(*i)++] : NULL) != NULL) {
bool stopper = !json_is_value(var) && var->value.integer == terminator;
bool stopper;
int r;

stopper = !json_is_value(var) && var->value.integer == terminator;

if (stopper) {
if (state != STATE_COMMA && size > 0)
goto error;
Expand Down Expand Up @@ -655,7 +664,7 @@ static int json_scoped_parse(JsonVariant **tokens, size_t *i, size_t n, JsonVari
state = STATE_VALUE;
}
else if (state == STATE_VALUE) {
_cleanup_jsonunref_ JsonVariant *v = NULL;
_cleanup_json_variant_unref_ JsonVariant *v = NULL;
size_t toadd = arr ? 1 : 2;

if (!json_is_value(var)) {
Expand Down Expand Up @@ -723,7 +732,7 @@ static int json_parse_tokens(JsonVariant **tokens, size_t ntokens, JsonVariant *
size_t it = 0;
int r;
JsonVariant *e;
_cleanup_jsonunref_ JsonVariant *p = NULL;
_cleanup_json_variant_unref_ JsonVariant *p = NULL;

assert(tokens);
assert(ntokens);
Expand Down Expand Up @@ -767,8 +776,8 @@ static int json_tokens(const char *string, size_t size, JsonVariant ***tokens, s

p = buf;
for (;;) {
_cleanup_json_variant_unref_ JsonVariant *var = NULL;
_cleanup_free_ char *rstr = NULL;
_cleanup_jsonunref_ JsonVariant *var = NULL;

t = json_tokenize(&p, &rstr, &v, &json_state, NULL);

Expand Down
22 changes: 12 additions & 10 deletions src/shared/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,32 @@ union json_value {
};

typedef struct JsonVariant {
JsonVariantType type;
size_t size;
union {
char *string;
struct JsonVariant *objects;
union json_value value;
};
JsonVariantType type;
unsigned size;
} JsonVariant;

int json_variant_new(JsonVariant **ret, JsonVariantType type);
JsonVariant *json_variant_unref(JsonVariant *);
JsonVariant *json_variant_unref(JsonVariant *v);

DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref);
#define _cleanup_jsonunref_ _cleanup_(json_variant_unrefp)
#define _cleanup_json_variant_unref_ _cleanup_(json_variant_unrefp)

char *json_variant_string(JsonVariant *);
bool json_variant_bool(JsonVariant *);
intmax_t json_variant_integer(JsonVariant *);
double json_variant_real(JsonVariant *);
char *json_variant_string(JsonVariant *v);
bool json_variant_bool(JsonVariant *v);
intmax_t json_variant_integer(JsonVariant *v);
double json_variant_real(JsonVariant *v);

JsonVariant *json_variant_element(JsonVariant *, unsigned index);
JsonVariant *json_variant_value(JsonVariant *, const char *key);
JsonVariant *json_variant_element(JsonVariant *v, unsigned index);
JsonVariant *json_variant_value(JsonVariant *v, const char *key);

#define JSON_VALUE_NULL ((union json_value) {})

int json_tokenize(const char **p, char **ret_string, union json_value *ret_value, void **state, unsigned *line);

int json_parse(const char *string, JsonVariant **rv);
int json_parse_measure(const char *string, size_t *size);
5 changes: 3 additions & 2 deletions src/test/test-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ static void test_one(const char *data, ...) {
typedef void (*Test)(JsonVariant *);

static void test_file(const char *data, Test test) {
_cleanup_jsonunref_ JsonVariant *v = NULL;
int r = json_parse(data, &v);
_cleanup_json_variant_unref_ JsonVariant *v = NULL;
int r;

r = json_parse(data, &v);
assert_se(r == 0);
assert_se(v != NULL);
assert_se(v->type == JSON_VARIANT_OBJECT);
Expand Down

0 comments on commit dde8bb3

Please sign in to comment.