vici: Add get_bool() convenience getter for VICI messages

This commit is contained in:
Tobias Brunner
2015-08-21 18:21:13 +02:00
parent ff0abde9ed
commit b9d7319fb3
3 changed files with 94 additions and 0 deletions
@@ -1,4 +1,7 @@
/*
* Copyright (C) 2015 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* Copyright (C) 2014 Martin Willi
* Copyright (C) 2014 revosec AG
*
@@ -355,6 +358,33 @@ START_TEST(test_get_int)
}
END_TEST
START_TEST(test_get_bool)
{
vici_message_t *m;
m = build_getter_msg();
ck_assert(m->get_bool(m, TRUE, "key1"));
ck_assert(m->get_bool(m, FALSE, "key1"));
ck_assert(m->get_bool(m, TRUE, "section1.key2"));
ck_assert(m->get_bool(m, TRUE, "section1.section2.key3"));
ck_assert(m->get_bool(m, TRUE, "section1.key4"));
ck_assert(m->get_bool(m, TRUE, "key5"));
ck_assert(m->get_bool(m, TRUE, "nonexistent"));
ck_assert(m->get_bool(m, TRUE, "n.o.n.e.x.i.s.t.e.n.t"));
ck_assert(!m->get_bool(m, FALSE, "section1.key2"));
ck_assert(!m->get_bool(m, FALSE, "section1.section2.key3"));
ck_assert(!m->get_bool(m, FALSE, "section1.key4"));
ck_assert(!m->get_bool(m, FALSE, "key5"));
ck_assert(!m->get_bool(m, FALSE, "nonexistent"));
ck_assert(!m->get_bool(m, FALSE, "n.o.n.e.x.i.s.t.e.n.t"));
m->destroy(m);
}
END_TEST
START_TEST(test_get_value)
{
vici_message_t *m;
@@ -400,6 +430,7 @@ Suite *message_suite_create()
tc = tcase_create("convenience getters");
tcase_add_test(tc, test_get_str);
tcase_add_test(tc, test_get_int);
tcase_add_test(tc, test_get_bool);
tcase_add_test(tc, test_get_value);
suite_add_tcase(s, tc);
+40
View File
@@ -1,4 +1,7 @@
/*
* Copyright (C) 2015 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* Copyright (C) 2014 Martin Willi
* Copyright (C) 2014 revosec AG
*
@@ -385,6 +388,41 @@ METHOD(vici_message_t, get_int, int,
return val;
}
METHOD(vici_message_t, vget_bool, bool,
private_vici_message_t *this, bool def, char *fmt, va_list args)
{
chunk_t value;
bool found;
char buf[16];
found = find_value(this, &value, fmt, args);
if (found)
{
if (value.len == 0)
{
return def;
}
if (chunk_printable(value, NULL, 0))
{
snprintf(buf, sizeof(buf), "%.*s", (int)value.len, value.ptr);
return settings_value_as_bool(buf, def);
}
}
return def;
}
METHOD(vici_message_t, get_bool, bool,
private_vici_message_t *this, bool def, char *fmt, ...)
{
va_list args;
bool val;
va_start(args, fmt);
val = vget_bool(this, def, fmt, args);
va_end(args);
return val;
}
METHOD(vici_message_t, vget_value, chunk_t,
private_vici_message_t *this, chunk_t def, char *fmt, va_list args)
{
@@ -633,6 +671,8 @@ vici_message_t *vici_message_create_from_data(chunk_t data, bool cleanup)
.vget_str = _vget_str,
.get_int = _get_int,
.vget_int = _vget_int,
.get_bool = _get_bool,
.vget_bool = _vget_bool,
.get_value = _get_value,
.vget_value = _vget_value,
.get_encoding = _get_encoding,
+23
View File
@@ -1,4 +1,7 @@
/*
* Copyright (C) 2015 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* Copyright (C) 2014 Martin Willi
* Copyright (C) 2014 revosec AG
*
@@ -137,6 +140,26 @@ struct vici_message_t {
*/
int (*vget_int)(vici_message_t *this, int def, char *fmt, va_list args);
/**
* Get the value of a key/value pair as boolean.
*
* @param def default value if not found
* @param fmt printf style format string for key, with sections
* @param ... arguments to fmt string
* @return value
*/
bool (*get_bool)(vici_message_t *this, bool def, char *fmt, ...);
/**
* Get the value of a key/value pair as boolean, va_list variant
*
* @param def default value if not found
* @param fmt printf style format string for key, with sections
* @param args arguments to fmt string
* @return value
*/
bool (*vget_bool)(vici_message_t *this, bool def, char *fmt, va_list args);
/**
* Get the raw value of a key/value pair.
*