summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/dict.c
diff options
context:
space:
mode:
Diffstat (limited to 'libglusterfs/src/dict.c')
-rw-r--r--libglusterfs/src/dict.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c
index 6a61e641e19..82b9236e661 100644
--- a/libglusterfs/src/dict.c
+++ b/libglusterfs/src/dict.c
@@ -1258,6 +1258,38 @@ dict_foreach (dict_t *dict,
return ret;
}
+int
+dict_foreach_with_idx (dict_t *dict,
+ int (*fn)(dict_t *this,
+ char *key,
+ data_t *value,
+ void *data, uint64_t idx),
+ void *data)
+{
+ if (!dict) {
+ gf_log_callingfn ("dict", GF_LOG_WARNING,
+ "dict is NULL");
+ return -1;
+ }
+
+ uint64_t idx = 0;
+ int ret = -1;
+ data_pair_t *pairs = NULL;
+ data_pair_t *next = NULL;
+
+ pairs = dict->members_list;
+ while (pairs) {
+ next = pairs->next;
+ ret = fn (dict, pairs->key, pairs->value, data, idx);
+ if (ret < 0)
+ return ret;
+ pairs = next;
+ idx++;
+ }
+
+ return 0;
+}
+
/* return values:
-1 = failure,
0 = no matches found,
@@ -2979,6 +3011,16 @@ dict_dump_to_str (dict_t *dict, char *dump, int dumpsize, char *format)
return 0;
}
+/* This function converts a uint32 to a (string) key for use in a dictionary.
+ * Ensure that the key string buffer is at least DICT_UINT32_KEY_SIZE in
+ * length, since that's the maximum length of a uint32's string representation
+ * plus a NULL delimiter char. */
+void
+dict_uint32_to_key (uint32_t num, char *key_buf)
+{
+ snprintf (key_buf, DICT_UINT32_KEY_SIZE, "%u", num);
+}
+
void
dict_dump_to_log (dict_t *dict)
{