summaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
authorNico von Geyso <Nico.Geyso@FU-Berlin.de>2013-03-18 21:02:36 +0100
committerCarlos Martín Nieto <cmn@dwim.me>2013-08-08 11:14:53 +0200
commita603c191578f7b33720e36e95421fcd58bc7abe4 (patch)
tree4de06cb51a580e85e5be38c91bbdfe980b4a55b5 /src/config.c
parent6385fc5ff5d669d3ec99d89f19c5860cf53011ba (diff)
downloadlibgit2-a603c191578f7b33720e36e95421fcd58bc7abe4.tar.gz
replaced foreach() with non callback based iterations in git_config_backend
new functions in struct git_config_backend: * iterator_new(...) * iterator_free(...) * next(...) The old callback based foreach style can still be used with `git_config_backend_foreach_match`
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c46
1 files changed, 45 insertions, 1 deletions
diff --git a/src/config.c b/src/config.c
index 2a058549f..b421b3be1 100644
--- a/src/config.c
+++ b/src/config.c
@@ -321,6 +321,50 @@ int git_config_foreach(
return git_config_foreach_match(cfg, NULL, cb, payload);
}
+int git_config_backend_foreach_match(
+ git_config_backend *backend,
+ const char *regexp,
+ int (*fn)(const git_config_entry *, void *),
+ void *data)
+{
+ git_config_entry entry;
+ git_config_backend_iter iter;
+ regex_t regex;
+ int result = 0;
+
+ if (regexp != NULL) {
+ if ((result = regcomp(&regex, regexp, REG_EXTENDED)) < 0) {
+ giterr_set_regex(&regex, result);
+ regfree(&regex);
+ return -1;
+ }
+ }
+
+ if (backend->iterator_new(&iter, backend) < 0)
+ return 0;
+
+ while(!(backend->next(&iter, &entry, backend) < 0)) {
+ /* skip non-matching keys if regexp was provided */
+ if (regexp && regexec(&regex, entry.name, 0, NULL, 0) != 0)
+ continue;
+
+ /* abort iterator on non-zero return value */
+ if (fn(&entry, data)) {
+ giterr_clear();
+ result = GIT_EUSER;
+ goto cleanup;
+ }
+ }
+
+cleanup:
+ if (regexp != NULL)
+ regfree(&regex);
+
+ backend->iterator_free(iter);
+
+ return result;
+}
+
int git_config_foreach_match(
const git_config *cfg,
const char *regexp,
@@ -335,7 +379,7 @@ int git_config_foreach_match(
for (i = 0; i < cfg->files.length && ret == 0; ++i) {
internal = git_vector_get(&cfg->files, i);
file = internal->file;
- ret = file->foreach(file, regexp, cb, payload);
+ ret = git_config_backend_foreach_match(file, regexp, cb, payload);
}
return ret;