From: "Bryce Harrington" <[email protected]> Adds a setter, weston_config_section_set(), for changing key values in the loaded copy of the weston.ini configuration. This functionality is not exposed to clients. Changes are not saved to disk.
Signed-off-by: Bryce Harrington (http://osg.samsung.com) <[email protected]> Signed-off-by: Bryce Harrington <[email protected]> --- shared/config-parser.c | 88 ++++++++++++++++++++++++++++++++-------------- shared/config-parser.h | 5 +++ tests/config-parser-test.c | 22 ++++++++++++ 3 files changed, 88 insertions(+), 27 deletions(-) diff --git a/shared/config-parser.c b/shared/config-parser.c index b72cb77..78039ba 100644 --- a/shared/config-parser.c +++ b/shared/config-parser.c @@ -115,6 +115,33 @@ open_config_file(struct weston_config *c, const char *name) return open(c->path, O_RDONLY | O_CLOEXEC); } +static struct weston_config_section * +config_add_section(struct weston_config *config, const char *name) +{ + struct weston_config_section *section; + + section = malloc(sizeof *section); + section->name = strdup(name); + wl_list_init(§ion->entry_list); + wl_list_insert(config->section_list.prev, §ion->link); + + return section; +} + +static struct weston_config_entry * +section_add_entry(struct weston_config_section *section, + const char *key, const char *value) +{ + struct weston_config_entry *entry; + + entry = malloc(sizeof *entry); + entry->key = strdup(key); + entry->value = strdup(value); + wl_list_insert(section->entry_list.prev, &entry->link); + + return entry; +} + static struct weston_config_entry * config_section_get_entry(struct weston_config_section *section, const char *key) @@ -174,6 +201,40 @@ weston_config_get_section(struct weston_config *config, const char *section, return NULL; } +/** Set a value in the configuration + * + * \param section Data structure containing \c key to be updated + * \param key Name of parameter to change + * \param value String value to set \c key to + * + * \return true if the value could be set, false otherwise. + * + * This routine allows changing of specific entries in the config + * data loaded from a configuration file. This does not change the + * config file on disk, only the values in memory. + */ +WL_EXPORT +bool +weston_config_section_set(struct weston_config_section *section, + const char *key, const char *value) +{ + struct weston_config_entry *e; + + if (section == NULL || key == NULL || value == NULL) + return false; + + e = config_section_get_entry(section, key); + if (e == NULL) { + // If no entry, create a new one + e = section_add_entry(section, key, value); + } else { + // Change existing value + free(e->value); + e->value = strdup(value); + } + return (e != NULL) && (e->value != NULL); +} + WL_EXPORT int weston_config_section_get_int(struct weston_config_section *section, @@ -315,33 +376,6 @@ weston_config_get_libexec_dir(void) return LIBEXECDIR; } -static struct weston_config_section * -config_add_section(struct weston_config *config, const char *name) -{ - struct weston_config_section *section; - - section = malloc(sizeof *section); - section->name = strdup(name); - wl_list_init(§ion->entry_list); - wl_list_insert(config->section_list.prev, §ion->link); - - return section; -} - -static struct weston_config_entry * -section_add_entry(struct weston_config_section *section, - const char *key, const char *value) -{ - struct weston_config_entry *entry; - - entry = malloc(sizeof *entry); - entry->key = strdup(key); - entry->value = strdup(value); - wl_list_insert(section->entry_list.prev, &entry->link); - - return entry; -} - struct weston_config * weston_config_parse(const char *name) { diff --git a/shared/config-parser.h b/shared/config-parser.h index 1ecc8cc..e47c54a 100644 --- a/shared/config-parser.h +++ b/shared/config-parser.h @@ -27,6 +27,8 @@ extern "C" { #endif +#include <stdbool.h> + enum config_key_type { CONFIG_KEY_INTEGER, /* typeof data = int */ CONFIG_KEY_UNSIGNED_INTEGER, /* typeof data = unsigned int */ @@ -71,6 +73,9 @@ struct weston_config; struct weston_config_section * weston_config_get_section(struct weston_config *config, const char *section, const char *key, const char *value); +bool +weston_config_section_set(struct weston_config_section *section, + const char *key, const char *value); int weston_config_section_get_int(struct weston_config_section *section, const char *key, diff --git a/tests/config-parser-test.c b/tests/config-parser-test.c index 4b255b7..fa61ffd 100644 --- a/tests/config-parser-test.c +++ b/tests/config-parser-test.c @@ -96,6 +96,10 @@ static const char t4[] = "[bambam]\n" "=not valid at all\n"; +static const char t5[] = + "[foo]\n" + "a=b\n"; + int main(int argc, char *argv[]) { struct weston_config *config; @@ -200,6 +204,24 @@ int main(int argc, char *argv[]) section = weston_config_get_section(NULL, "bucket", NULL, NULL); assert(section == NULL); + weston_config_destroy(config); + + /* Setting values */ + config = run_test(t5); + + section = weston_config_get_section(config, "foo", NULL, NULL); + assert(weston_config_section_set(section, "a", "c")); + + section = weston_config_get_section(config, "foo", NULL, NULL); + r = weston_config_section_get_string(section, "a", &s, NULL); + assert(r == 0 && strcmp(s, "c") == 0); + free(s); + + assert(!weston_config_section_set(NULL, "a", "c")); + assert(!weston_config_section_set(section, NULL, "c")); + assert(!weston_config_section_set(section, "a", NULL)); + + weston_config_destroy(config); return 0; } -- 1.9.1 _______________________________________________ wayland-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/wayland-devel
