settings: Add settings_value_as_uint64() helper function
This commit is contained in:
@@ -537,6 +537,31 @@ METHOD(settings_t, get_int, int,
|
|||||||
return settings_value_as_int(value, def);
|
return settings_value_as_int(value, def);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Described in header
|
||||||
|
*/
|
||||||
|
inline u_int64_t settings_value_as_uint64(char *value, u_int64_t def)
|
||||||
|
{
|
||||||
|
u_int64_t intval;
|
||||||
|
char *end;
|
||||||
|
int base = 10;
|
||||||
|
|
||||||
|
if (value)
|
||||||
|
{
|
||||||
|
errno = 0;
|
||||||
|
if (value[0] == '0' && value[1] == 'x')
|
||||||
|
{ /* manually detect 0x prefix as we want to avoid octal encoding */
|
||||||
|
base = 16;
|
||||||
|
}
|
||||||
|
intval = strtoull(value, &end, base);
|
||||||
|
if (errno == 0 && *end == 0 && end != value)
|
||||||
|
{
|
||||||
|
return intval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Described in header
|
* Described in header
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -50,6 +50,15 @@ bool settings_value_as_bool(char *value, bool def);
|
|||||||
*/
|
*/
|
||||||
int settings_value_as_int(char *value, int def);
|
int settings_value_as_int(char *value, int def);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a string value returned by a key/value enumerator to an u_int64_t.
|
||||||
|
*
|
||||||
|
* @see settings_t.create_key_value_enumerator()
|
||||||
|
* @param value the string value
|
||||||
|
* @param def the default value, if value is NULL or invalid
|
||||||
|
*/
|
||||||
|
u_int64_t settings_value_as_uint64(char *value, u_int64_t def);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a string value returned by a key/value enumerator to a double.
|
* Convert a string value returned by a key/value enumerator to a double.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -317,6 +317,26 @@ START_TEST(test_set_int)
|
|||||||
}
|
}
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
START_TEST(test_value_as_unit64)
|
||||||
|
{
|
||||||
|
test_int_eq(1, settings_value_as_uint64(NULL, 1));
|
||||||
|
test_int_eq(1, settings_value_as_uint64("", 1));
|
||||||
|
test_int_eq(1, settings_value_as_uint64("2a", 1));
|
||||||
|
test_int_eq(1, settings_value_as_uint64("a2", 1));
|
||||||
|
test_int_eq(1, settings_value_as_uint64("2.0", 1));
|
||||||
|
|
||||||
|
test_int_eq(10, settings_value_as_uint64("10", 0));
|
||||||
|
test_int_eq(10, settings_value_as_uint64("010", 0));
|
||||||
|
test_int_eq(16, settings_value_as_uint64("0x010", 0));
|
||||||
|
test_int_eq(0x2a, settings_value_as_uint64("0x2a", 0));
|
||||||
|
|
||||||
|
test_int_eq(0xffffffffffffffffLL, settings_value_as_uint64("0xffffffffffffffff", 0));
|
||||||
|
test_int_eq(0xffffffff00000000LL, settings_value_as_uint64("0xffffffff00000000", 0));
|
||||||
|
test_int_eq(0xffffffff00000000LL, settings_value_as_uint64("18446744069414584320", 0));
|
||||||
|
test_int_eq(0xffffffff00000001LL, settings_value_as_uint64("18446744069414584321", 0));
|
||||||
|
}
|
||||||
|
END_TEST
|
||||||
|
|
||||||
START_SETUP(setup_double_config)
|
START_SETUP(setup_double_config)
|
||||||
{
|
{
|
||||||
create_settings(chunk_from_str(
|
create_settings(chunk_from_str(
|
||||||
@@ -1158,6 +1178,10 @@ Suite *settings_suite_create()
|
|||||||
tcase_add_test(tc, test_set_int);
|
tcase_add_test(tc, test_set_int);
|
||||||
suite_add_tcase(s, tc);
|
suite_add_tcase(s, tc);
|
||||||
|
|
||||||
|
tc = tcase_create("settings_value_as_uint64");
|
||||||
|
tcase_add_test(tc, test_value_as_unit64);
|
||||||
|
suite_add_tcase(s, tc);
|
||||||
|
|
||||||
tc = tcase_create("get/set_double");
|
tc = tcase_create("get/set_double");
|
||||||
tcase_add_checked_fixture(tc, setup_double_config, teardown_config);
|
tcase_add_checked_fixture(tc, setup_double_config, teardown_config);
|
||||||
tcase_add_test(tc, test_get_double);
|
tcase_add_test(tc, test_get_double);
|
||||||
|
|||||||
Reference in New Issue
Block a user