From: Petri Lehtinen Date: Thu, 4 Feb 2010 19:13:57 +0000 (+0200) Subject: Merge branch '1.2' X-Git-Tag: v1.3~15 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=jansson.git;a=commitdiff_plain;h=8d75235ff22dc4aced697e198c3c024f1f4b88fe;hp=-c Merge branch '1.2' Conflicts: LICENSE --- 8d75235ff22dc4aced697e198c3c024f1f4b88fe diff --combined src/hashtable.c index 990fd6e,1f8abf4..0d4a9f2 --- a/src/hashtable.c +++ b/src/hashtable.c @@@ -1,5 -1,5 +1,5 @@@ /* - * Copyright (c) 2009 Petri Lehtinen + * Copyright (c) 2009, 2010 Petri Lehtinen * * This library is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@@ -318,22 -318,6 +318,22 @@@ void *hashtable_iter(hashtable_t *hasht return hashtable_iter_next(hashtable, &hashtable->list); } +void *hashtable_iter_at(hashtable_t *hashtable, const void *key) +{ + pair_t *pair; + unsigned int hash; + bucket_t *bucket; + + hash = hashtable->hash_key(key); + bucket = &hashtable->buckets[hash % num_buckets(hashtable)]; + + pair = hashtable_find_pair(hashtable, bucket, key, hash); + if(!pair) + return NULL; + + return &pair->list; +} + void *hashtable_iter_next(hashtable_t *hashtable, void *iter) { list_t *list = (list_t *)iter; @@@ -353,13 -337,3 +353,13 @@@ void *hashtable_iter_value(void *iter pair_t *pair = list_to_pair((list_t *)iter); return pair->value; } + +void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value) +{ + pair_t *pair = list_to_pair((list_t *)iter); + + if(hashtable->free_value) + hashtable->free_value(pair->value); + + pair->value = value; +} diff --combined src/hashtable.h index e920c6b,e055d9f..6d3c736 --- a/src/hashtable.h +++ b/src/hashtable.h @@@ -1,5 -1,5 +1,5 @@@ /* - * Copyright (c) 2009 Petri Lehtinen + * Copyright (c) 2009, 2010 Petri Lehtinen * * This library is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@@ -161,17 -161,6 +161,17 @@@ void hashtable_clear(hashtable_t *hasht void *hashtable_iter(hashtable_t *hashtable); /** + * hashtable_iter - Return an iterator at a specific key + * + * @hashtable: The hashtable object + * @key: The key that the iterator should point to + * + * Like hashtable_iter() but returns an iterator pointing to a + * specific key. + */ +void *hashtable_iter_at(hashtable_t *hashtable, const void *key); + +/** * hashtable_iter_next - Advance an iterator * * @hashtable: The hashtable object @@@ -196,12 -185,4 +196,12 @@@ void *hashtable_iter_key(void *iter) */ void *hashtable_iter_value(void *iter); +/** + * hashtable_iter_set - Set the value pointed by an iterator + * + * @iter: The iterator + * @value: The value to set + */ +void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value); + #endif diff --combined src/jansson.h index d703c7b,78094a7..55dce0f --- a/src/jansson.h +++ b/src/jansson.h @@@ -1,5 -1,5 +1,5 @@@ /* - * Copyright (c) 2009 Petri Lehtinen + * Copyright (c) 2009, 2010 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@@ -83,11 -83,9 +83,11 @@@ int json_object_del(json_t *object, con int json_object_clear(json_t *object); int json_object_update(json_t *object, json_t *other); void *json_object_iter(json_t *object); +void *json_object_iter_at(json_t *object, const char *key); void *json_object_iter_next(json_t *object, void *iter); const char *json_object_iter_key(void *iter); json_t *json_object_iter_value(void *iter); +int json_object_iter_set_new(json_t *object, void *iter, json_t *value); static inline int json_object_set(json_t *object, const char *key, json_t *value) @@@ -101,12 -99,6 +101,12 @@@ int json_object_set_nocheck(json_t *obj return json_object_set_new_nocheck(object, key, json_incref(value)); } +static inline +int json_object_iter_set(json_t *object, void *iter, json_t *value) +{ + return json_object_iter_set_new(object, iter, json_incref(value)); +} + unsigned int json_array_size(const json_t *array); json_t *json_array_get(const json_t *array, unsigned int index); int json_array_set_new(json_t *array, unsigned int index, json_t *value); diff --combined src/value.c index 31109ff,345ff8e..35166f4 --- a/src/value.c +++ b/src/value.c @@@ -1,5 -1,5 +1,5 @@@ /* - * Copyright (c) 2009 Petri Lehtinen + * Copyright (c) 2009, 2010 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@@ -190,17 -190,6 +190,17 @@@ void *json_object_iter(json_t *json return hashtable_iter(&object->hashtable); } +void *json_object_iter_at(json_t *json, const char *key) +{ + json_object_t *object; + + if(!key || !json_is_object(json)) + return NULL; + + object = json_to_object(json); + return hashtable_iter_at(&object->hashtable, key); +} + void *json_object_iter_next(json_t *json, void *iter) { json_object_t *object; @@@ -228,19 -217,6 +228,19 @@@ json_t *json_object_iter_value(void *it return (json_t *)hashtable_iter_value(iter); } +int json_object_iter_set_new(json_t *json, void *iter, json_t *value) +{ + json_object_t *object; + + if(!json_is_object(json) || !iter || !value) + return -1; + + object = json_to_object(json); + hashtable_iter_set(&object->hashtable, iter, value); + + return 0; +} + static int json_object_equal(json_t *object1, json_t *object2) { void *iter; diff --combined test/suites/api/run index 48c3e7e,e01148d..ff53f35 --- a/test/suites/api/run +++ b/test/suites/api/run @@@ -1,34 -1,21 +1,34 @@@ #!/bin/sh # - # Copyright (c) 2009 Petri Lehtinen + # Copyright (c) 2009, 2010 Petri Lehtinen # # Jansson is free software; you can redistribute it and/or modify # it under the terms of the MIT license. See LICENSE for details. is_test() { - [ "${test_name%.c}" != "$test_name" ] && return 0 - [ -x $test_path -a ! -f $test_path.c ] && return 0 - return 1 + case "$test_name" in + *.c|*.cpp|check-exports) + return 0 + ;; + *) + return 1 + ;; + esac } run_test() { - if [ -x $test_path ]; then + if [ "$test_name" = "check-exports" ]; then test_log=$test_log $test_path >$test_log/stdout 2>$test_log/stderr else - $test_runner $suite_builddir/${test_name%.c} \ + case "$test_name" in + *.c) + test_bin=${test_name%.c} + ;; + *.cpp) + test_bin=${test_name%.cpp} + ;; + esac + $test_runner $suite_builddir/$test_bin \ >$test_log/stdout \ 2>$test_log/stderr \ || return 1 diff --combined test/suites/api/test_object.c index 99d0a56,67a93c8..4e730bb --- a/test/suites/api/test_object.c +++ b/test/suites/api/test_object.c @@@ -1,5 -1,5 +1,5 @@@ /* - * Copyright (c) 2009 Petri Lehtinen + * Copyright (c) 2009, 2010 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@@ -258,36 -258,6 +258,36 @@@ static void test_iterators( if(json_object_iter_next(object, iter) != NULL) fail("able to iterate over the end"); + if(json_object_iter_at(object, "foo")) + fail("json_object_iter_at() succeeds for non-existent key"); + + iter = json_object_iter_at(object, "b"); + if(!iter) + fail("json_object_iter_at() fails for an existing key"); + + if(strcmp(json_object_iter_key(iter), "b")) + fail("iterating failed: wrong key"); + if(json_object_iter_value(iter) != bar) + fail("iterating failed: wrong value"); + + iter = json_object_iter_next(object, iter); + if(!iter) + fail("unable to increment iterator"); + if(strcmp(json_object_iter_key(iter), "c")) + fail("iterating failed: wrong key"); + if(json_object_iter_value(iter) != baz) + fail("iterating failed: wrong value"); + + if(json_object_iter_set(object, iter, bar)) + fail("unable to set value at iterator"); + + if(strcmp(json_object_iter_key(iter), "c")) + fail("json_object_iter_key() fails after json_object_iter_set()"); + if(json_object_iter_value(iter) != bar) + fail("json_object_iter_value() fails after json_object_iter_set()"); + if(json_object_get(object, "c") != bar) + fail("json_object_get() fails after json_object_iter_set()"); + json_decref(object); json_decref(foo); json_decref(bar);