In using the apr_hash datastructure in Subversion, we've found that we
often only want the key or value from a hash. Furthermore, casting
the various return parameters has proven cumbersome. To solve this
problem, we've introduced three helper functions to return the key,
key length, and value from a hash iterator.
We've found these functions quite useful, so I'm including a patch to
add them to APR proper. The patch is against trunk, but if possible,
I'd like to see these APIs backported to 1.4.x and 1.5.x.
Thanks,
-Hyrum
Index: tables/apr_hash.c
===================================================================
--- tables/apr_hash.c (revision 920903)
+++ tables/apr_hash.c (working copy)
@@ -156,7 +156,31 @@ APR_DECLARE(void) apr_hash_this(apr_hash_index_t *
if (val) *val = (void *)hi->this->val;
}
+APR_DECLARE(const void *) apr_hash_this_key(apr_hash_index_t *hi)
+{
+ const void *key;
+ apr_hash_this(hi, &key, NULL, NULL);
+ return key;
+}
+
+APR_DECLARE(apr_ssize_t) apr_hash_this_klen(apr_hash_index_t *hi)
+{
+ apr_ssize_t klen;
+
+ apr_hash_this(hi, NULL, &klen, NULL);
+ return klen;
+}
+
+APR_DECLARE(void *) apr_hash_this_val(apr_hash_index_t *hi)
+{
+ void *val;
+
+ apr_hash_this(hi, NULL, NULL, &val);
+ return val;
+}
+
+
/*
* Expanding a hash table
*/
Index: test/testhash.c
===================================================================
--- test/testhash.c (revision 920903)
+++ test/testhash.c (working copy)
@@ -32,12 +32,13 @@ static int comp_string(const void *str1, const voi
static void dump_hash(apr_pool_t *p, apr_hash_t *h, char str[][MAX_LTH])
{
apr_hash_index_t *hi;
- char *val, *key;
- apr_ssize_t len;
int i = 0;
for (hi = apr_hash_first(p, h); hi; hi = apr_hash_next(hi)) {
- apr_hash_this(hi,(void*) &key, &len, (void*) &val);
+ char *key = apr_hash_this_key(hi);
+ apr_ssize_t len = apr_hash_this_klen(hi);
+ char *val = apr_hash_this_val(hi);
+
str[i][0]='\0';
apr_snprintf(str[i], MAX_LTH, "%sKey %s (%" APR_SSIZE_T_FMT ") Value
%s\n",
str[i], key, len, val);
Index: include/apr_hash.h
===================================================================
--- include/apr_hash.h (revision 920903)
+++ include/apr_hash.h (working copy)
@@ -167,6 +167,27 @@ APR_DECLARE(void) apr_hash_this(apr_hash_index_t *
apr_ssize_t *klen, void **val);
/**
+ * Get the current entry's key from the iteration state.
+ * @param hi The iteration state
+ * @return The pointer to the key
+ */
+APR_DECLARE(const void*) apr_hash_this_key(apr_hash_index_t *hi);
+
+/**
+ * Get the current entry's key length from the iteration state.
+ * @param hi The iteration state
+ * @return The key length
+ */
+APR_DECLARE(apr_ssize_t) apr_hash_this_klen(apr_hash_index_t *hi);
+
+/**
+ * Get the current entry's value from the iteration state.
+ * @param hi The iteration state
+ * @return The pointer to the value
+ */
+APR_DECLARE(void*) apr_hash_this_val(apr_hash_index_t *hi);
+
+/**
* Get the number of key/value pairs in the hash table.
* @param ht The hash table
* @return The number of key/value pairs in the hash table.