From 16fc3ea68e0aef5c2a977ab6f65d3e000a6da663 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Fri, 22 Oct 2010 10:55:40 +0200 Subject: [PATCH] Clean up argument parsing for plugins. --- src/plugins/mod_auth_sqlite.c | 52 +++++++++++++++--------- src/util/config_token.c | 74 +++++++++++++++++++++++++++++++++++ src/util/config_token.h | 7 ++++ 3 files changed, 114 insertions(+), 19 deletions(-) diff --git a/src/plugins/mod_auth_sqlite.c b/src/plugins/mod_auth_sqlite.c index 08d1d95..8d04f65 100644 --- a/src/plugins/mod_auth_sqlite.c +++ b/src/plugins/mod_auth_sqlite.c @@ -37,31 +37,45 @@ static struct sql_data* parse_config(const char* line, struct plugin_handle* plu while (token) { - char* split = strchr(token, '='); - size_t len = strlen(token); - size_t key = split ? (split - token) : len; - if (key == 4 && strncmp(token, "file", 4) == 0 && data->db == 0) - { - if (sqlite3_open(split + 1, &data->db)) - { - cfg_tokens_free(tokens); - hub_free(data); - set_error_message(plugin, "Unable to open database file"); - return 0; - } - } - else if (key == 9 && strncmp(token, "exclusive", 9) == 0) - { - if (!string_to_boolean(split + 1, &data->exclusive)) - data->exclusive = 1; - } - else + struct cfg_settings* setting = cfg_settings_split(token); + + if (!setting) { set_error_message(plugin, "Unable to parse startup parameters"); cfg_tokens_free(tokens); hub_free(data); return 0; } + + if (strcmp(cfg_settings_get_key(setting), "file") == 0) + { + if (!data->db) + { + if (sqlite3_open(cfg_settings_get_value(setting), &data->db)) + { + cfg_tokens_free(tokens); + cfg_settings_free(setting); + hub_free(data); + set_error_message(plugin, "Unable to open database file"); + return 0; + } + } + } + else if (strcmp(cfg_settings_get_key(setting), "exclusive") == 0) + { + if (!string_to_boolean(cfg_settings_get_value(setting), &data->exclusive)) + data->exclusive = 1; + } + else + { + set_error_message(plugin, "Unknown startup parameters given"); + cfg_tokens_free(tokens); + cfg_settings_free(setting); + hub_free(data); + return 0; + } + + cfg_settings_free(setting); token = cfg_token_get_next(tokens); } cfg_tokens_free(tokens); diff --git a/src/util/config_token.c b/src/util/config_token.c index e647ef8..df63163 100644 --- a/src/util/config_token.c +++ b/src/util/config_token.c @@ -154,3 +154,77 @@ char* cfg_token_get_next(struct cfg_tokens* tokens) { return list_get_next(tokens->list); } + +struct cfg_settings +{ + char* key; + char* value; +}; + +struct cfg_settings* cfg_settings_split(const char* line) +{ + struct cfg_settings* s = NULL; + struct cfg_tokens* tok = NULL; + char* pos = NULL; + + if ( !line + || !*line + || ((pos = (char*) strchr(line, '=')) == NULL) + || ((s = hub_malloc_zero(sizeof(struct cfg_settings))) == NULL) + || ((tok = cfg_tokenize(line)) == NULL) + || (cfg_token_count(tok) < 2) + || (cfg_token_count(tok) > 3) + || (cfg_token_count(tok) == 3 && strcmp(cfg_token_get(tok, 1), "=")) + ) + { + cfg_tokens_free(tok); + cfg_settings_free(s); + return NULL; + } + + if (cfg_token_count(tok) == 2) + { + char* key = cfg_token_get_first(tok); + pos = strchr(key, '='); + pos[0] = 0; + key = strip_white_space(key); + + if (!*key) + { + cfg_tokens_free(tok); + cfg_settings_free(s); + return NULL; + } + + s->key = strdup(key); + s->value = strdup(strip_white_space(cfg_token_get_next(tok))); + } + else + { + s->key = strdup(strip_white_space(cfg_token_get(tok, 0))); + s->value = strdup(strip_white_space(cfg_token_get(tok, 2))); + } + cfg_tokens_free(tok); + return s; +} + +const char* cfg_settings_get_key(struct cfg_settings* s) +{ + return s->key; +} + +const char* cfg_settings_get_value(struct cfg_settings* s) +{ + return s->value; +} + +void cfg_settings_free(struct cfg_settings* s) +{ + if (s) + { + hub_free(s->key); + hub_free(s->value); + hub_free(s); + } +} + diff --git a/src/util/config_token.h b/src/util/config_token.h index f8ad081..1b40f23 100644 --- a/src/util/config_token.h +++ b/src/util/config_token.h @@ -33,5 +33,12 @@ char* cfg_token_get(struct cfg_tokens*, size_t offset); char* cfg_token_get_first(struct cfg_tokens*); char* cfg_token_get_next(struct cfg_tokens*); + +struct cfg_settings; +struct cfg_settings* cfg_settings_split(const char* line); +const char* cfg_settings_get_key(struct cfg_settings*); +const char* cfg_settings_get_value(struct cfg_settings*); +void cfg_settings_free(struct cfg_settings*); + #endif /* HAVE_UHUB_CONFIG_TOKEN_H */