From f73f31624869efd86b3de04d7faef9221b5eb4cb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Sun, 26 May 2013 20:50:53 -0400 Subject: [PATCH] config-parser: Add section iterator API The X backend needs to iterate through all outputs. --- shared/config-parser-test.c | 13 ++++++++++++- shared/config-parser.c | 24 ++++++++++++++++++++++++ shared/config-parser.h | 5 +++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/shared/config-parser-test.c b/shared/config-parser-test.c index 11eb9aa..86843a0 100644 --- a/shared/config-parser-test.c +++ b/shared/config-parser-test.c @@ -75,6 +75,10 @@ static const char t1[] = "color=red\n" "contents=sand\n"; +static const char *section_names[] = { + "foo", "bar", "stuff", "bucket", "bucket" +}; + static const char t2[] = "# invalid section...\n" "[this bracket isn't closed\n"; @@ -93,8 +97,9 @@ int main(int argc, char *argv[]) { struct weston_config *config; struct weston_config_section *section; + const char *name; char *s; - int r, b; + int r, b, i; int32_t n; uint32_t u; @@ -170,6 +175,12 @@ int main(int argc, char *argv[]) assert(r == -1 && errno == ENOENT && strcmp(s, "eels") == 0); free(s); + section = NULL; + i = 0; + while (weston_config_next_section(config, §ion, &name)) + assert(strcmp(section_names[i++], name) == 0); + assert(i == 5); + weston_config_destroy(config); config = run_test(t2); diff --git a/shared/config-parser.c b/shared/config-parser.c index 9772c12..7a19ea5 100644 --- a/shared/config-parser.c +++ b/shared/config-parser.c @@ -36,6 +36,10 @@ #include #include "config-parser.h" +#define container_of(ptr, type, member) ({ \ + const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + static int handle_key(const struct config_key *key, const char *value) { @@ -472,6 +476,26 @@ weston_config_parse(int fd) return config; } +int +weston_config_next_section(struct weston_config *config, + struct weston_config_section **section, + const char **name) +{ + if (*section == NULL) + *section = container_of(config->section_list.next, + struct weston_config_section, link); + else + *section = container_of((*section)->link.next, + struct weston_config_section, link); + + if (&(*section)->link == &config->section_list) + return 0; + + *name = (*section)->name; + + return 1; +} + void weston_config_destroy(struct weston_config *config) { diff --git a/shared/config-parser.h b/shared/config-parser.h index 949f4d6..794e09c 100644 --- a/shared/config-parser.h +++ b/shared/config-parser.h @@ -102,6 +102,11 @@ weston_config_parse(int fd); void weston_config_destroy(struct weston_config *config); +int weston_config_next_section(struct weston_config *config, + struct weston_config_section **section, + const char **name); + + #ifdef __cplusplus } #endif -- 2.7.4