xml_generator: support bool and integer types

The Xml_generator is extended by overloads for bool as well as signed
and unsigned int, long, and long long.

Fixes #1819
This commit is contained in:
Christian Helmuth 2015-12-16 12:36:27 +01:00
parent 98a4cb27b2
commit 14c8380507
3 changed files with 56 additions and 5 deletions

View File

@ -318,13 +318,45 @@ class Genode::Xml_generator
_curr_node->insert_attribute(name, str.string());
}
void attribute(char const *name, long value)
void attribute(char const *name, bool value)
{
_curr_node->insert_attribute(name, value ? "true" : "false");
}
void attribute(char const *name, long long value)
{
char buf[64];
Genode::snprintf(buf, sizeof(buf), "%ld", value);
Genode::snprintf(buf, sizeof(buf), "%lld", value);
_curr_node->insert_attribute(name, buf);
}
void attribute(char const *name, long value)
{
attribute(name, static_cast<long long>(value));
}
void attribute(char const *name, int value)
{
attribute(name, static_cast<long long>(value));
}
void attribute(char const *name, unsigned long long value)
{
char buf[64];
Genode::snprintf(buf, sizeof(buf), "%llu", value);
_curr_node->insert_attribute(name, buf);
}
void attribute(char const *name, unsigned long value)
{
attribute(name, static_cast<unsigned long long>(value));
}
void attribute(char const *name, unsigned value)
{
attribute(name, static_cast<unsigned long long>(value));
}
/**
* Append content to XML node
*

View File

@ -35,7 +35,7 @@ compare_output_to {
[init -> test-xml_generator] --- XML generator test started ---
[init -> test-xml_generator] result:
[init -> test-xml_generator]
[init -> test-xml_generator] <config xpos="27" ypos="34" verbose="true">
[init -> test-xml_generator] <config xpos="27" ypos="34">
[init -> test-xml_generator] <box width="320" height="240"/>
[init -> test-xml_generator] <label name="a test">
[init -> test-xml_generator] <sub_label/>
@ -43,9 +43,12 @@ compare_output_to {
[init -> test-xml_generator] <sub_sub_label/>
[init -> test-xml_generator] </another_sub_label>
[init -> test-xml_generator] </label>
[init -> test-xml_generator] <bool true="true" false="false"/>
[init -> test-xml_generator] <signed int="-1" long="-2" longlong="-3"/>
[init -> test-xml_generator] <unsigned int="1" long="2" longlong="3"/>
[init -> test-xml_generator] </config>
[init -> test-xml_generator]
[init -> test-xml_generator] used 200 bytes
[init -> test-xml_generator] used 307 bytes
[init -> test-xml_generator] buffer exceeded (expected error)
[init -> test-xml_generator] --- XML generator test finished ---
}

View File

@ -39,7 +39,23 @@ static size_t fill_buffer_with_xml(char *dst, size_t dst_len)
xml.node("sub_sub_label");
});
});
xml.attribute("verbose", "true");
xml.node("bool", [&] ()
{
xml.attribute("true", true);
xml.attribute("false", false);
});
xml.node("signed", [&] ()
{
xml.attribute("int", -1);
xml.attribute("long", -2L);
xml.attribute("longlong", -3LL);
});
xml.node("unsigned", [&] ()
{
xml.attribute("int", 1U);
xml.attribute("long", 2UL);
xml.attribute("longlong", 3ULL);
});
});
return xml.used();