Clean up argument parsing for plugins.

This commit is contained in:
Jan Vidar Krey 2010-10-22 10:55:40 +02:00
parent a934dfaa70
commit 16fc3ea68e
3 changed files with 114 additions and 19 deletions

View File

@ -37,31 +37,45 @@ static struct sql_data* parse_config(const char* line, struct plugin_handle* plu
while (token) while (token)
{ {
char* split = strchr(token, '='); struct cfg_settings* setting = cfg_settings_split(token);
size_t len = strlen(token);
size_t key = split ? (split - token) : len; if (!setting)
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
{ {
set_error_message(plugin, "Unable to parse startup parameters"); set_error_message(plugin, "Unable to parse startup parameters");
cfg_tokens_free(tokens); cfg_tokens_free(tokens);
hub_free(data); hub_free(data);
return 0; 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); token = cfg_token_get_next(tokens);
} }
cfg_tokens_free(tokens); cfg_tokens_free(tokens);

View File

@ -154,3 +154,77 @@ char* cfg_token_get_next(struct cfg_tokens* tokens)
{ {
return list_get_next(tokens->list); 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);
}
}

View File

@ -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_first(struct cfg_tokens*);
char* cfg_token_get_next(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 */ #endif /* HAVE_UHUB_CONFIG_TOKEN_H */