Write abritrary printable objects into Xml_generator

Fix #3161
This commit is contained in:
Ehmry - 2019-02-15 19:27:27 +01:00 committed by Norman Feske
parent b24edc1633
commit b5bd6e0114
2 changed files with 53 additions and 0 deletions

View File

@ -248,6 +248,16 @@ class Genode::Xml_generator
_commit_content(content_buffer);
}
/**
* Append character, sanitize it if needed
*/
void append_sanitized(char const c)
{
Out_buffer content_buffer = _content_buffer(false);
content_buffer.append_sanitized(c);
_commit_content(content_buffer);
}
void append_sanitized(char const *src, size_t src_len)
{
Out_buffer content_buffer = _content_buffer(false);
@ -417,6 +427,33 @@ class Genode::Xml_generator
_curr_node->append_sanitized(str, str_len == ~0UL ? strlen(str) : str_len);
}
/**
* Append printable objects to XML node as sanitized content
*
* This method must not be followed by calls of 'attribute'.
*/
template <typename... ARGS>
void append_content(ARGS &&... args)
{
struct Node_output : Genode::Output
{
Node &node; Node_output(Node &n) : node(n) { }
/******************************
** Genode::Output interface **
******************************/
void out_char(char c) override {
node.append_sanitized(c); }
void out_string(char const *str, size_t n) override {
node.append_sanitized(str, n); }
} output { *_curr_node };
Output::out_args(output, args...);
}
size_t used() const { return _out_buffer.used(); }
};

View File

@ -226,6 +226,22 @@ void Component::construct(Genode::Env &env)
}
}
/*
* Test arbitrary content
*/
{
Xml_generator xml(dst, sizeof(dst), "data", [&] () {
xml.append_content(" ", 2 + 2, " == 2 + 2 == ", 4.0, " ");
});
Xml_node node(dst);
auto s = node.decoded_content<String<32>>();
if (s != " 4 == 2 + 2 == 4.0 ") {
error("decoded content does not match expect content");
return;
}
}
log("--- XML generator test finished ---");
genode_exit(0);
}