dev  

Re: svn commit: r676807 - in /apr/apr/trunk: CHANGES include/apr_hash.h tables/apr_hash.c

Davi Arnaut
Tue, 15 Jul 2008 10:36:55 -0700

[EMAIL PROTECTED] wrote:
Author: mturk
Date: Mon Jul 14 23:29:29 2008
New Revision: 676807


<snip>


  /**
+ * Declaration prototype for the iterator callback function of apr_hash_do().
+ *
+ * @param rec The data passed as the first argument to apr_hash_[v]do()
+ * @param key The key from this iteration of the hash table
+ * @param klen The key length from this iteration of the hash table
+ * @param value The value from this iteration of the hash table
+ * @remark Iteration continues while this callback function returns non-zero.
+ * To export the callback function for apr_hash_do() it must be declared
+ * in the _NONSTD convention.
+ */
+typedef int (apr_hash_do_callback_fn_t)(void *rec, const void *key,
+                                                   apr_ssize_t klen,
+                                                   const void *value);
+
+/**
+ * Iterate over a hash table running the provided function once for every
+ * element in the hash table. The @param comp function will be invoked for
+ * every element in the hash table.
+ *
+ * @param comp The function to run
+ * @param rec The data to pass as the first argument to the function
+ * @param ht The hash table to iterate over
+ * @return FALSE if one of the comp() iterations returned zero; TRUE if all
+ *            iterations returned non-zero
+ * @see apr_hash_do_callback_fn_t
+ */
+APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
+                             void *rec, const apr_hash_t *ht);
+
+/**
   * Get a pointer to the pool which the hash table was created in
   */
  APR_POOL_DECLARE_ACCESSOR(hash);

Modified: apr/apr/trunk/tables/apr_hash.c
URL: 
http://svn.apache.org/viewvc/apr/apr/trunk/tables/apr_hash.c?rev=676807&r1=676806&r2=676807&view=diff
==============================================================================
--- apr/apr/trunk/tables/apr_hash.c (original)
+++ apr/apr/trunk/tables/apr_hash.c Mon Jul 14 23:29:29 2008
@@ -474,4 +474,37 @@
      return res;
  }

+/* This is basically the following...
+ * for every element in hash table {
+ *    comp elemeny.key, element.value
+ * }
+ *
+ * Like with apr_table_do, the comp callback is called for each and every
+ * element of the hash table.
+ */
+APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
+                             void *rec, const apr_hash_t *ht)
+{
+    apr_hash_index_t  hix;
+    apr_hash_index_t *hi;
+    int rv, dorv  = 1;
+
+    hix.ht    = (apr_hash_t *)ht;
+    hix.index = 0;
+    hix.this  = NULL;
+    hix.next  = NULL;
+
+    if ((hi = apr_hash_next(&hix))) {
+        /* Scan the entire table */
+        do {
+            rv = (*comp)(rec, hi->this->key, hi->this->klen, hi->this->val);
+        } while ((hi = apr_hash_next(hi)));

Iteration does not stop if one of the comp() iterations returned zero...

+
+        if (rv == 0) {
+            dorv = 0;
+        }
+    }

         ^ Spurious space here

+    return dorv;
+}
+

Regards,

-- Davi Arnaut