genode/repos/os/include/decorator/xml_utils.h
Norman Feske 17c79a9e23 base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.

While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).

To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.

Issue #1987
2016-08-29 17:27:10 +02:00

136 lines
3.0 KiB
C++

/*
* \brief Utilities for XML parsing
* \author Norman Feske
* \date 2014-01-09
*/
/*
* Copyright (C) 2014 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#ifndef _INCLUDE__DECORATOR__XML_UTILS_H_
#define _INCLUDE__DECORATOR__XML_UTILS_H_
#include <decorator/types.h>
namespace Decorator {
template <typename T>
static T attribute(Xml_node const &, char const *, T);
template <size_t CAPACITY>
Genode::String<CAPACITY> string_attribute(Xml_node const &, char const *,
Genode::String<CAPACITY> const &);
static Point point_attribute(Xml_node const &);
static Area area_attribute(Xml_node const &);
static Rect rect_attribute(Xml_node const &);
template <typename FUNC>
static void for_each_sub_node(Xml_node, char const *, FUNC const &);
static Color color(Xml_node const &);
}
/**
* Read attribute value from XML node
*
* \param node XML node
* \param name attribute name
* \param default_value value returned if no such attribute exists
*/
template <typename T>
static T
Decorator::attribute(Xml_node const &node, char const *name, T default_value)
{
T result = default_value;
if (node.has_attribute(name))
node.attribute(name).value(&result);
return result;
}
/**
* Read string from XML node
*/
template <Genode::size_t CAPACITY>
Genode::String<CAPACITY>
Decorator::string_attribute(Xml_node const &node, char const *attr,
Genode::String<CAPACITY> const &default_value)
{
if (!node.has_attribute(attr))
return default_value;
char buf[CAPACITY];
node.attribute(attr).value(buf, sizeof(buf));
return Genode::String<CAPACITY>(Genode::Cstring(buf));
}
/**
* Read point position from XML node
*/
static inline Decorator::Point Decorator::point_attribute(Genode::Xml_node const &point)
{
return Point(attribute(point, "xpos", 0L),
attribute(point, "ypos", 0L)); }
/**
* Read area size from XML node
*/
static inline Decorator::Area Decorator::area_attribute(Genode::Xml_node const &area)
{
return Area(attribute(area, "width", 0UL),
attribute(area, "height", 0UL));
}
/**
* Read rectangle coordinates from XML node
*/
static inline Decorator::Rect Decorator::rect_attribute(Genode::Xml_node const &rect)
{
return Rect(point_attribute(rect), area_attribute(rect));
}
/**
* Apply functor 'func' to all XML sub nodes of given type
*/
template <typename FUNC>
static void
Decorator::for_each_sub_node(Genode::Xml_node node, char const *type,
FUNC const &func)
{
if (!node.has_sub_node(type))
return;
for (node = node.sub_node(type); ; node = node.next()) {
if (node.has_type(type))
func(node);
if (node.last()) break;
}
}
/**
* Read color attribute from XML node
*/
static inline Genode::Color Decorator::color(Genode::Xml_node const &color)
{
return attribute(color, "color", Color(0, 0, 0));
}
#endif /* _INCLUDE__DECORATOR__XML_UTILS_H_ */