From 26f9b8e6d7b3ac4a93da2a9b15ccb39ba437990e Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Thu, 1 Nov 2012 17:01:40 +0100 Subject: [PATCH] Added util function to generate arg from string. --- src/setting.c | 33 ++++++++++++--------------------- src/util.c | 39 ++++++++++++++++++++++++++++++++++----- src/util.h | 5 +++-- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/setting.c b/src/setting.c index e701bf5..5b713f1 100644 --- a/src/setting.c +++ b/src/setting.c @@ -102,6 +102,7 @@ void setting_cleanup(void) gboolean setting_run(const gchar* name, const gchar* param) { + Arg* a = NULL; gboolean result = FALSE; Setting* s = g_hash_table_lookup(settings, name); if (!s) { @@ -115,29 +116,18 @@ gboolean setting_run(const gchar* name, const gchar* param) /* if string param is given convert it to the required data type and set * it to the arg of the setting */ - switch (s->type) { - case TYPE_BOOLEAN: - s->arg.i = util_atob(param) ? 1 : 0; - result = s->func(s); - break; - - case TYPE_INTEGER: - s->arg.i = g_ascii_strtoull(param, (gchar**)NULL, 10); - result = s->func(s); - break; - - case TYPE_DOUBLE: - s->arg.i = (1000 * g_ascii_strtod(param, (gchar**)NULL)); - result = s->func(s); - break; + a = util_char_to_arg(param, s->type); + if (a == NULL) { + vp_echo(VP_MSG_ERROR, "No valid value"); + return FALSE; + } - case TYPE_CHAR: - case TYPE_COLOR: - s->arg.s = g_strdup(param); - result = s->func(s); - g_free(s->arg.s); - break; + s->arg = *a; + result = s->func(s); + if (a->s) { + g_free(a->s); } + g_free(a); return result; } @@ -176,6 +166,7 @@ static gboolean setting_cookie_timeout(const Setting* s) static gboolean setting_scrollstep(const Setting* s) { + PRINT_DEBUG("Set scrollstep to %d", s->arg.i); vp.config.scrollstep = s->arg.i; return TRUE; diff --git a/src/util.c b/src/util.c index 0401b2f..ddc4485 100644 --- a/src/util.c +++ b/src/util.c @@ -50,12 +50,41 @@ void util_create_file_if_not_exists(const char* filename) { } } -gboolean util_atob(const gchar* str) +Arg* util_char_to_arg(const gchar* str, Type type) { - if (str == NULL) { - return FALSE; + Arg* arg = util_new_arg(); + + if (!str) { + return NULL; + } + switch (type) { + case TYPE_BOOLEAN: + arg->i = g_ascii_strncasecmp(str, "true", 4) == 0 + || g_ascii_strncasecmp(str, "on", 2) == 0 ? 1 : 0; + break; + + case TYPE_INTEGER: + arg->i = g_ascii_strtoull(str, (gchar**)NULL, 10); + break; + + case TYPE_DOUBLE: + arg->i = (1000 * g_ascii_strtod(str, (gchar**)NULL)); + break; + + case TYPE_CHAR: + case TYPE_COLOR: + arg->s = g_strdup(str); + break; } - return g_ascii_strncasecmp(str, "true", 4) == 0 - || g_ascii_strncasecmp(str, "on", 2) == 0; + return arg; +} + +Arg* util_new_arg(void) +{ + Arg* arg = g_new0(Arg, 1); + arg->i = 0; + arg->s = NULL; + + return arg; } diff --git a/src/util.h b/src/util.h index 3d14843..2433d9e 100644 --- a/src/util.h +++ b/src/util.h @@ -20,12 +20,13 @@ #ifndef UTIL_H #define UTIL_H -#include +#include "main.h" gchar* util_get_config_dir(void); gchar* util_get_cache_dir(void); void util_create_dir_if_not_exists(const gchar* dirpath); void util_create_file_if_not_exists(const gchar* filename); -gboolean util_atob(const gchar* str); +Arg* util_char_to_arg(const gchar* str, Type type); +Arg* util_new_arg(void); #endif /* end of include guard: UTIL_H */ -- 2.20.1