summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCsaba Henk <csaba@gluster.com>2010-09-23 08:29:14 +0000
committerVijay Bellur <vijay@dev.gluster.com>2010-09-24 03:53:36 -0700
commit7e1b8bb370b4082e66ebe6e458fc45aa653ab3f1 (patch)
treed345ed507ef6d813fd6989da51eb6ee5e7956efc
parent406bcbb443aaca1655be9dcb9101a717eafdc061 (diff)
dict: add dict_get_str_boolean() function handily query string-boolean values
Signed-off-by: Csaba Henk <csaba@gluster.com> Signed-off-by: Vijay Bellur <vijay@dev.gluster.com> BUG: 1670 (Volume set enable disable support) URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=1670
-rw-r--r--libglusterfs/src/dict.c61
-rw-r--r--libglusterfs/src/dict.h1
2 files changed, 62 insertions, 0 deletions
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c
index b1c1a5d94..bff17444c 100644
--- a/libglusterfs/src/dict.c
+++ b/libglusterfs/src/dict.c
@@ -2170,6 +2170,67 @@ err:
return ret;
}
+
+/**
+ * dict_get_str_boolean - get a boolean value based on string representation.
+ *
+ * @this : dictionary
+ * @key : dictionary key queried
+ * @default_val : default value if key not found
+ *
+ * @return : @default_val if key not found
+ * : boolean interpretation of @this[@key] if it makes sense
+ * (ie., "on", "true", "enable" ...)
+ * : -1 if error occurs or @this[@key] doesn't make sens as
+ * boolean
+ *
+ * So if you query a boolean option, then via @default_val you can choose
+ * between following patterns:
+ *
+ * - fall back to _gf_false if @key is not set [@default_val = 0]
+ * - fall back to _gf_true if @key is not set [@default_val = 1]
+ * - regard as failure if @key is not set [@default_val = -1]
+ * - handle specially (not as error) if @key is not set
+ * [@default_val = anything else]
+ */
+
+int
+dict_get_str_boolean (dict_t *this, char *key, int default_val)
+{
+ data_t *data = NULL;
+ gf_boolean_t boo = _gf_false;
+ int ret = 0;
+
+ ret = dict_get_with_ref (this, key, &data);
+ if (ret < 0) {
+ if (ret == -ENOENT)
+ ret = default_val;
+ else
+ ret = -1;
+ goto err;
+ }
+
+ GF_ASSERT (data);
+
+ if (!data->data) {
+ ret = -1;
+ goto err;
+ }
+
+ ret = gf_string2boolean (data->data, &boo);
+ if (ret == -1)
+ goto err;
+
+ ret = boo;
+
+err:
+ if (data)
+ data_unref (data);
+
+ return ret;
+}
+
+
/**
* Serialization format:
* -------- -------- -------- ----------- -------------
diff --git a/libglusterfs/src/dict.h b/libglusterfs/src/dict.h
index 41b6b6780..f5c4abd0b 100644
--- a/libglusterfs/src/dict.h
+++ b/libglusterfs/src/dict.h
@@ -185,4 +185,5 @@ GF_MUST_CHECK int dict_set_dynmstr (dict_t *this, char *key, char *str);
GF_MUST_CHECK int dict_set_dynstr (dict_t *this, char *key, char *str);
GF_MUST_CHECK int dict_get_str (dict_t *this, char *key, char **str);
+GF_MUST_CHECK int dict_get_str_boolean (dict_t *this, char *key, int default_val);
#endif