summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPranith Kumar K <pkarampu@redhat.com>2014-11-13 22:39:44 +0530
committerVijay Bellur <vbellur@redhat.com>2014-11-25 09:50:30 -0800
commitff9ffab7b4e991de6191f10909c32a7a08f20ada (patch)
treee1d3848f977fb5cbaa6606c22d19bafd8abc5c0e
parent4ea5b8d2046b9e0bc7f24cdf1b2e72ab8b462c9e (diff)
dict: Write dict_foreach_* in form of dict_foreach_match
Change-Id: Iaa3454f7f3b6516660b1976bea63e39ea7795f8f BUG: 1164051 Signed-off-by: Pranith Kumar K <pkarampu@redhat.com> Reviewed-on: http://review.gluster.org/9121 Reviewed-by: Jeff Darcy <jdarcy@redhat.com> Reviewed-by: Krishnan Parthasarathi <kparthas@redhat.com> Tested-by: Gluster Build System <jenkins@build.gluster.com>
-rw-r--r--libglusterfs/src/dict.c71
1 files changed, 18 insertions, 53 deletions
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c
index 4b35fa8dd3c..b8cb23fe700 100644
--- a/libglusterfs/src/dict.c
+++ b/libglusterfs/src/dict.c
@@ -1104,6 +1104,12 @@ dict_remove_foreach_fn (dict_t *d, char *k,
return 0;
}
+static inline gf_boolean_t
+match_everything (dict_t *d, char *k, data_t *v, void *data)
+{
+ return _gf_true;
+}
+
int
dict_foreach (dict_t *dict,
int (*fn)(dict_t *this,
@@ -1112,26 +1118,14 @@ dict_foreach (dict_t *dict,
void *data),
void *data)
{
- if (!dict) {
- gf_log_callingfn ("dict", GF_LOG_WARNING,
- "dict is NULL");
- return -1;
- }
+ int ret = 0;
- int ret = -1;
- data_pair_t *pairs = NULL;
- data_pair_t *next = NULL;
+ ret = dict_foreach_match (dict, match_everything, NULL, fn, data);
- pairs = dict->members_list;
- while (pairs) {
- next = pairs->next;
- ret = fn (dict, pairs->key, pairs->value, data);
- if (ret < 0)
- return ret;
- pairs = next;
- }
+ if (ret > 0)
+ ret = 0;
- return 0;
+ return ret;
}
/* return values:
@@ -1179,6 +1173,11 @@ dict_foreach_match (dict_t *dict,
return count;
}
+static gf_boolean_t
+dict_fnmatch (dict_t *d, char *k, data_t *val, void *match_data)
+{
+ return (fnmatch (match_data, k, 0) == 0);
+}
/* return values:
-1 = failure,
0 = no matches found,
@@ -1192,30 +1191,7 @@ dict_foreach_fnmatch (dict_t *dict, char *pattern,
void *data),
void *data)
{
- if (!dict) {
- gf_log_callingfn ("dict", GF_LOG_WARNING,
- "dict is NULL");
- return 0;
- }
-
- int ret = -1;
- int count = 0;
- data_pair_t *pairs = NULL;
- data_pair_t *next = NULL;
-
- pairs = dict->members_list;
- while (pairs) {
- next = pairs->next;
- if (!fnmatch (pattern, pairs->key, 0)) {
- ret = fn (dict, pairs->key, pairs->value, data);
- if (ret == -1)
- return -1;
- count++;
- }
- pairs = next;
- }
-
- return count;
+ return dict_foreach_match (dict, dict_fnmatch, pattern, fn, data);
}
@@ -1269,17 +1245,6 @@ _copy (dict_t *unused,
return dict_set ((dict_t *)newdict, key, (value));
}
-static int
-_remove (dict_t *dict,
- char *key,
- data_t *value,
- void *unused)
-{
- dict_del ((dict_t *)dict, key);
- return 0;
-}
-
-
dict_t *
dict_copy (dict_t *dict,
dict_t *new)
@@ -1305,7 +1270,7 @@ dict_reset (dict_t *dict)
gf_log_callingfn ("dict", GF_LOG_WARNING, "dict is NULL");
goto out;
}
- dict_foreach (dict, _remove, NULL);
+ dict_foreach (dict, dict_remove_foreach_fn, NULL);
ret = 0;
out:
return ret;