summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorDaniel Carl <danielcarl@gmx.de>2012-11-01 17:01:40 +0100
committerDaniel Carl <danielcarl@gmx.de>2012-11-10 14:43:03 +0100
commit26f9b8e6d7b3ac4a93da2a9b15ccb39ba437990e (patch)
tree74fee6f936cb6ecd32f9cf70cc3812af09e186c9 /src/util.c
parentc24ae0407026c0c76a6b7f88780c013ffc0d3788 (diff)
Added util function to generate arg from string.
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c39
1 files changed, 34 insertions, 5 deletions
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;
}