Author: stefan2
Date: Sat May 21 15:13:32 2011
New Revision: 1125725
URL: http://svn.apache.org/viewvc?rev=1125725&view=rev
Log:
Provide hash lookup functions that will return default values if either
the hash itself or the keyed entry are unavailable. This is useful where
options are stored in hashes instead of svn_config_t structures.
* subversion/include/svn_hash.h
(svn_hash_get_cstring, svn_hash_get_bool): declare new API functions
* subversion/libsvn_subr/hash.c
(svn_hash_get_cstring, svn_hash_get_bool): implement them
Modified:
subversion/trunk/subversion/include/svn_hash.h
subversion/trunk/subversion/libsvn_subr/hash.c
Modified: subversion/trunk/subversion/include/svn_hash.h
URL:
http://svn.apache.org/viewvc/subversion/trunk/subversion/include/svn_hash.h?rev=1125725&r1=1125724&r2=1125725&view=diff
==============================================================================
--- subversion/trunk/subversion/include/svn_hash.h (original)
+++ subversion/trunk/subversion/include/svn_hash.h Sat May 21 15:13:32 2011
@@ -251,6 +251,39 @@ svn_hash__clear(apr_hash_t *hash, apr_po
/** @} */
+
+/**
+ * @defgroup svn_hash_getters Specialized getter APIs for hashes
+ * @{
+ */
+
+/** Find the value of a @a key in @a hash, return the value.
+ *
+ * If @a hash is @c NULL or if the @a key cannot be found, the
+ * @a default_value will be returned.
+ *
+ * @since New in 1.7.
+ */
+const char *
+svn_hash_get_cstring(apr_hash_t *hash,
+ const char *key,
+ const char *default_value);
+
+/** Like @ref svn_hash_get_cstring, but for boolean values.
+ *
+ * Parses the value as a boolean value. The recognized representations
+ * are 'TRUE'/'FALSE', 'yes'/'no', 'on'/'off', '1'/'0'; case does not
+ * matter.
+ *
+ * @since New in 1.7.
+ */
+svn_boolean_t
+svn_hash_get_bool(apr_hash_t *hash,
+ const char *key,
+ svn_boolean_t default_value);
+
+/** @} */
+
/** @} */
#ifdef __cplusplus
Modified: subversion/trunk/subversion/libsvn_subr/hash.c
URL:
http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/hash.c?rev=1125725&r1=1125724&r2=1125725&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/hash.c (original)
+++ subversion/trunk/subversion/libsvn_subr/hash.c Sat May 21 15:13:32 2011
@@ -510,3 +510,38 @@ svn_hash__clear(apr_hash_t *hash, apr_po
#endif
return SVN_NO_ERROR;
}
+
+
+
+/*** Specialized getter APIs ***/
+
+const char*
+svn_hash_get_cstring(apr_hash_t *hash,
+ const char *key,
+ const char *default_value)
+{
+ if (hash)
+ {
+ const char *value = apr_hash_get(hash, key, APR_HASH_KEY_STRING);
+ return value ? value : default_value;
+ }
+
+ return default_value;
+}
+
+
+svn_boolean_t
+svn_hash_get_bool(apr_hash_t *hash, const char *key,
+ svn_boolean_t default_value)
+{
+ const char *tmp_value = svn_hash_get_cstring(hash, key, NULL);
+ svn_tristate_t value = svn_tristate_from_word(tmp_value);
+
+ if (value == svn_tristate_true)
+ return TRUE;
+ else if (value == svn_tristate_false)
+ return FALSE;
+
+ return default_value;
+}
+