aboutsummaryrefslogtreecommitdiff
path: root/lib/config.cc
diff options
context:
space:
mode:
authorDavid Bremner <david@tethera.net>2021-01-01 09:28:24 -0400
committerDavid Bremner <david@tethera.net>2021-02-06 19:09:39 -0400
commitd071828bd5f8aee0437aeb4993ffeeaa803c367b (patch)
tree0a739d38749fa206cff386f22e71e7c65c1b07d5 /lib/config.cc
parentfd6f8e6c30e0443d1ead248047ab572120df85e9 (diff)
lib/config: make values iterators restartable
This is relatively cheap, and makes it easier to transform existing code which uses arrays of pointers to store configuration lists.
Diffstat (limited to 'lib/config.cc')
-rw-r--r--lib/config.cc43
1 files changed, 34 insertions, 9 deletions
diff --git a/lib/config.cc b/lib/config.cc
index 0fe9a268..b2957f0c 100644
--- a/lib/config.cc
+++ b/lib/config.cc
@@ -34,6 +34,8 @@ struct _notmuch_config_list {
struct _notmuch_config_values {
const char *iterator;
size_t tok_len;
+ const char *string;
+ void *children; /* talloc_context */
};
static const char * _notmuch_config_key_to_string (notmuch_config_key_t key);
@@ -256,23 +258,33 @@ _notmuch_config_load_from_database (notmuch_database_t *notmuch)
notmuch_config_values_t *
notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key)
{
- notmuch_config_values_t *values;
+ notmuch_config_values_t *values = NULL;
+ bool ok = false;
- const char *str;
const char *key_str = _notmuch_config_key_to_string (key);
if (! key_str)
- return NULL;
-
- str = _notmuch_string_map_get (notmuch->config, key_str);
- if (! str)
- return NULL;
+ goto DONE;
values = talloc (notmuch, notmuch_config_values_t);
if (unlikely(! values))
- return NULL;
+ goto DONE;
+
+ values->children = talloc_new (values);
+
+ values->string = _notmuch_string_map_get (notmuch->config, key_str);
+ if (! values->string)
+ goto DONE;
+
+ values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
+ ok = true;
- values->iterator = strsplit_len (str, ';', &(values->tok_len));
+ DONE:
+ if (!ok) {
+ if (values)
+ talloc_free(values);
+ return NULL;
+ }
return values;
}
@@ -290,6 +302,19 @@ notmuch_config_values_get (notmuch_config_values_t *values) {
}
void
+notmuch_config_values_start (notmuch_config_values_t *values) {
+ if (values == NULL)
+ return;
+ if (values->children) {
+ talloc_free (values->children);
+ }
+
+ values->children = talloc_new (values);
+
+ values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
+}
+
+void
notmuch_config_values_move_to_next (notmuch_config_values_t *values) {
values->iterator += values->tok_len;
values->iterator = strsplit_len (values->iterator, ';', &(values->tok_len));