diff options
| author | David Bremner <david@tethera.net> | 2020-08-08 11:16:49 -0300 |
|---|---|---|
| committer | David Bremner <david@tethera.net> | 2021-02-06 19:08:12 -0400 |
| commit | fd6f8e6c30e0443d1ead248047ab572120df85e9 (patch) | |
| tree | 26e07a0890b9184fcff6a90f71dfbb91d9a87e8d /lib/config.cc | |
| parent | 3fb123f215668a54cd6084b2a520f767d2be6712 (diff) | |
lib/config: add config values iterator
This is intended to avoid duplicating the string splitting and
traversal code for all clients of the config API.
Diffstat (limited to 'lib/config.cc')
| -rw-r--r-- | lib/config.cc | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/config.cc b/lib/config.cc index a6b75913..0fe9a268 100644 --- a/lib/config.cc +++ b/lib/config.cc @@ -31,6 +31,11 @@ struct _notmuch_config_list { char *current_val; }; +struct _notmuch_config_values { + const char *iterator; + size_t tok_len; +}; + static const char * _notmuch_config_key_to_string (notmuch_config_key_t key); static int @@ -248,6 +253,53 @@ _notmuch_config_load_from_database (notmuch_database_t *notmuch) return status; } +notmuch_config_values_t * +notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key) +{ + notmuch_config_values_t *values; + + 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; + + values = talloc (notmuch, notmuch_config_values_t); + if (unlikely(! values)) + return NULL; + + values->iterator = strsplit_len (str, ';', &(values->tok_len)); + return values; +} + +notmuch_bool_t +notmuch_config_values_valid (notmuch_config_values_t *values) { + if (! values) + return false; + + return (values->iterator != NULL); +} + +const char * +notmuch_config_values_get (notmuch_config_values_t *values) { + return talloc_strndup (values, values->iterator, 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)); +} + +void +notmuch_config_values_destroy (notmuch_config_values_t *values) { + talloc_free (values); +} + notmuch_status_t _notmuch_config_load_from_file (notmuch_database_t *notmuch, GKeyFile *file) |
