Helper functions added to easily convert enumerated values.

This commit is contained in:
Tobias Brunner
2010-12-03 17:40:52 +01:00
parent f2649f354d
commit d657edf576
2 changed files with 92 additions and 19 deletions
+51 -18
View File
@@ -352,15 +352,11 @@ METHOD(settings_t, get_str, char*,
return def; return def;
} }
METHOD(settings_t, get_bool, bool, /**
private_settings_t *this, char *key, bool def, ...) * Described in header
*/
inline bool settings_value_as_bool(char *value, bool def)
{ {
char *value;
va_list args;
va_start(args, def);
value = find_value(this, this->top, key, args);
va_end(args);
if (value) if (value)
{ {
if (strcaseeq(value, "true") || if (strcaseeq(value, "true") ||
@@ -381,16 +377,24 @@ METHOD(settings_t, get_bool, bool,
return def; return def;
} }
METHOD(settings_t, get_int, int, METHOD(settings_t, get_bool, bool,
private_settings_t *this, char *key, int def, ...) private_settings_t *this, char *key, bool def, ...)
{ {
char *value; char *value;
int intval;
va_list args; va_list args;
va_start(args, def); va_start(args, def);
value = find_value(this, this->top, key, args); value = find_value(this, this->top, key, args);
va_end(args); va_end(args);
return settings_value_as_bool(value, def);
}
/**
* Described in header
*/
inline int settings_value_as_int(char *value, int def)
{
int intval;
if (value) if (value)
{ {
errno = 0; errno = 0;
@@ -403,16 +407,24 @@ METHOD(settings_t, get_int, int,
return def; return def;
} }
METHOD(settings_t, get_double, double, METHOD(settings_t, get_int, int,
private_settings_t *this, char *key, double def, ...) private_settings_t *this, char *key, int def, ...)
{ {
char *value; char *value;
double dval;
va_list args; va_list args;
va_start(args, def); va_start(args, def);
value = find_value(this, this->top, key, args); value = find_value(this, this->top, key, args);
va_end(args); va_end(args);
return settings_value_as_int(value, def);
}
/**
* Described in header
*/
inline double settings_value_as_double(char *value, double def)
{
double dval;
if (value) if (value)
{ {
errno = 0; errno = 0;
@@ -425,16 +437,25 @@ METHOD(settings_t, get_double, double,
return def; return def;
} }
METHOD(settings_t, get_time, u_int32_t, METHOD(settings_t, get_double, double,
private_settings_t *this, char *key, u_int32_t def, ...) private_settings_t *this, char *key, double def, ...)
{ {
char *value, *endptr; char *value;
u_int32_t timeval;
va_list args; va_list args;
va_start(args, def); va_start(args, def);
value = find_value(this, this->top, key, args); value = find_value(this, this->top, key, args);
va_end(args); va_end(args);
return settings_value_as_double(value, def);
}
/**
* Described in header
*/
inline u_int32_t settings_value_as_time(char *value, u_int32_t def)
{
char *endptr;
u_int32_t timeval;
if (value) if (value)
{ {
errno = 0; errno = 0;
@@ -462,6 +483,18 @@ METHOD(settings_t, get_time, u_int32_t,
return def; return def;
} }
METHOD(settings_t, get_time, u_int32_t,
private_settings_t *this, char *key, u_int32_t def, ...)
{
char *value;
va_list args;
va_start(args, def);
value = find_value(this, this->top, key, args);
va_end(args);
return settings_value_as_time(value, def);
}
/** /**
* Enumerate section names, not sections * Enumerate section names, not sections
*/ */
+41 -1
View File
@@ -27,6 +27,46 @@ typedef struct settings_t settings_t;
#include "utils.h" #include "utils.h"
#include "utils/enumerator.h" #include "utils/enumerator.h"
/**
* Convert a string value returned by a key/value enumerator to a boolean.
*
* @see settings_t.create_key_value_enumerator()
* @see settings_t.get_bool()
* @param value the string value
* @param def the default value, if value is NULL or invalid
*/
bool settings_value_as_bool(char *value, bool def);
/**
* Convert a string value returned by a key/value enumerator to an integer.
*
* @see settings_t.create_key_value_enumerator()
* @see settings_t.get_int()
* @param value the string value
* @param def the default value, if value is NULL or invalid
*/
int settings_value_as_int(char *value, int def);
/**
* Convert a string value returned by a key/value enumerator to a double.
*
* @see settings_t.create_key_value_enumerator()
* @see settings_t.get_double()
* @param value the string value
* @param def the default value, if value is NULL or invalid
*/
double settings_value_as_double(char *value, double def);
/**
* Convert a string value returned by a key/value enumerator to a time value.
*
* @see settings_t.create_key_value_enumerator()
* @see settings_t.get_time()
* @param value the string value
* @param def the default value, if value is NULL or invalid
*/
u_int32_t settings_value_as_time(char *value, u_int32_t def);
/** /**
* Generic configuration options read from a config file. * Generic configuration options read from a config file.
* *
@@ -167,7 +207,7 @@ struct settings_t {
* Create an enumerator over key/value pairs in a section. * Create an enumerator over key/value pairs in a section.
* *
* @param section section name to list key/value pairs of, printf style * @param section section name to list key/value pairs of, printf style
* @param ... argmuent list for section * @param ... argument list for section
* @return enumerator over (char *key, char *value) * @return enumerator over (char *key, char *value)
*/ */
enumerator_t* (*create_key_value_enumerator)(settings_t *this, enumerator_t* (*create_key_value_enumerator)(settings_t *this,