summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Brej <cbrej@cs.man.ac.uk>2010-04-20 21:07:58 +0100
committerCharlie Brej <cbrej@cs.man.ac.uk>2010-04-20 21:07:58 +0100
commitdd69e80ff9239cde8ff1cc7afd2f3c236e1c3256 (patch)
treec404ddf3a4f628e0dd4c0b3e34f507e89986428c
parentd163fb2a3dcfe64ef1e984cedaeade8c8d0fc4bc (diff)
[key-file] Add foreach function to access all stored values
Supplying a callback function, you can get each group/key/value triple. Allows accessing values in the key file for which you may not know the keys.
-rw-r--r--src/libply/ply-key-file.c55
-rw-r--r--src/libply/ply-key-file.h7
2 files changed, 62 insertions, 0 deletions
diff --git a/src/libply/ply-key-file.c b/src/libply/ply-key-file.c
index f36b229d..3b7ee383 100644
--- a/src/libply/ply-key-file.c
+++ b/src/libply/ply-key-file.c
@@ -60,6 +60,13 @@ struct _ply_key_file
ply_hashtable_t *groups;
};
+typedef struct
+{
+ ply_key_file_foreach_func_t *func;
+ void *user_data;
+ char *group_name;
+} ply_key_file_foreach_func_data_t;
+
static bool ply_key_file_open_file (ply_key_file_t *key_file);
static void ply_key_file_close_file (ply_key_file_t *key_file);
@@ -357,4 +364,52 @@ ply_key_file_get_value (ply_key_file_t *key_file,
return strdup (entry->value);
}
+static void
+ply_key_file_foreach_entry_entries (void *key,
+ void *data,
+ void *user_data)
+{
+ ply_key_file_entry_t *entry;
+ ply_key_file_foreach_func_data_t *func_data;
+
+ func_data = user_data;
+ entry = data;
+
+ func_data->func(func_data->group_name,
+ entry->key,
+ entry->value,
+ func_data->user_data);
+}
+
+static void
+ply_key_file_foreach_entry_groups (void *key,
+ void *data,
+ void *user_data)
+{
+ ply_key_file_group_t *group;
+ ply_key_file_foreach_func_data_t *func_data;
+
+ func_data = user_data;
+ group = data;
+ func_data->group_name = group->name;
+
+ ply_hashtable_foreach (group->entries,
+ ply_key_file_foreach_entry_entries,
+ func_data);
+}
+
+void
+ply_key_file_foreach_entry (ply_key_file_t *key_file,
+ ply_key_file_foreach_func_t func,
+ void *user_data)
+{
+ ply_key_file_foreach_func_data_t func_data;
+
+ func_data.func = func;
+ func_data.user_data = user_data;
+ ply_hashtable_foreach (key_file->groups,
+ ply_key_file_foreach_entry_groups,
+ &func_data);
+}
+
/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */
diff --git a/src/libply/ply-key-file.h b/src/libply/ply-key-file.h
index 3d35e120..8f6c8918 100644
--- a/src/libply/ply-key-file.h
+++ b/src/libply/ply-key-file.h
@@ -27,6 +27,10 @@
#include <unistd.h>
typedef struct _ply_key_file ply_key_file_t;
+typedef void (ply_key_file_foreach_func_t) (const char *group_name,
+ const char *key,
+ const char *value,
+ void *user_data);
#ifndef PLY_HIDE_FUNCTION_DECLARATIONS
ply_key_file_t *ply_key_file_new (const char *filename);
@@ -38,6 +42,9 @@ bool ply_key_file_has_key (ply_key_file_t *key_file,
char *ply_key_file_get_value (ply_key_file_t *key_file,
const char *group_name,
const char *key);
+void ply_key_file_foreach_entry (ply_key_file_t *key_file,
+ ply_key_file_foreach_func_t func,
+ void *user_data);
#endif
#endif /* PLY_KEY_FILE_H */