diff options
Diffstat (limited to 'cfg')
-rw-r--r-- | cfg/cfgparse.c | 54 | ||||
-rw-r--r-- | cfg/cfgparse.h | 17 |
2 files changed, 68 insertions, 3 deletions
diff --git a/cfg/cfgparse.c b/cfg/cfgparse.c index 4dc6dbb..9ac8ec7 100644 --- a/cfg/cfgparse.c +++ b/cfg/cfgparse.c @@ -74,9 +74,18 @@ static void fill_prop_number( const char* start, int len ) { - (void)len; - p->type = cfg_type_int; - p->as.num = (int)strtol(start, 0, 10); + int is_f = 0; + int i; + for (i = 0; i < len; i++) + if (start[i] == '.') + is_f = 1; + if (is_f) { + p->type = cfg_type_float; + p->as.num = (float)strtod(start, 0); + } else { + p->type = cfg_type_int; + p->as.num = (int)strtol(start, 0, 10); + } } static void fill_prop_data( @@ -222,6 +231,36 @@ int find_int_default( return def; } +float find_float_default( + const cfg_Object* obj, + const char* name, + float def +) { + const cfg_Prop* prop; + prop = find_prop_of(obj, name, cfg_type_float); + if (prop) { + return prop->as.flt; + } + return def; +} + +float find_num_default( + const cfg_Object* obj, + const char* name, + float def +) { + const cfg_Prop* prop; + prop = find_prop_of(obj, name, cfg_type_float); + if (prop) { + return prop->as.flt; + } + prop = find_prop_of(obj, name, cfg_type_int); + if (prop) { + return prop->as.num; + } + return def; +} + const char* find_string_default( const cfg_Object* obj, const char* name, @@ -234,3 +273,12 @@ const char* find_string_default( } return def; } + +unsigned find_colour_default( + const cfg_Object* obj, + const char* name, + const char* def +) { + const char* s = find_string_default(obj, name, def); + return (unsigned)strtol(s, 0, 16); +} diff --git a/cfg/cfgparse.h b/cfg/cfgparse.h index 4865b48..a585de6 100644 --- a/cfg/cfgparse.h +++ b/cfg/cfgparse.h @@ -5,6 +5,7 @@ typedef enum { cfg_type_int, + cfg_type_float, cfg_type_string, cfg_type_data, cfg_type_none @@ -19,6 +20,7 @@ typedef struct cfg_Prop { char name[28]; cfg_Type type; union { + float flt; int num; cfg_Data data; char* str; @@ -53,10 +55,25 @@ int find_int_default( const char* name, int def ); +float find_float_default( + const cfg_Object* obj, + const char* name, + float def +); +float find_num_default( + const cfg_Object* obj, + const char* name, + float def +); const char* find_string_default( const cfg_Object* obj, const char* name, const char* def ); +unsigned find_colour_default( + const cfg_Object* obj, + const char* name, + const char* def +); #endif |