/*
- * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
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;
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;
+}
/*
- * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
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
*/
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
/*
- * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
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)
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);
/*
- * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
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;
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;
#!/bin/sh
#
- # Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ # Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
#
# 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
/*
- * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
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);