2016-01-12 14:11:58 +01:00
|
|
|
/*
|
|
|
|
* \brief Session label utility class
|
|
|
|
* \author Emery Hemingway
|
2016-05-12 14:58:51 +02:00
|
|
|
* \author Norman Feske
|
2016-01-12 14:11:58 +01:00
|
|
|
* \date 2016-07-01
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2016-2017 Genode Labs GmbH
|
2016-01-12 14:11:58 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2016-01-12 14:11:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__BASE__SESSION_LABEL_H_
|
|
|
|
#define _INCLUDE__BASE__SESSION_LABEL_H_
|
|
|
|
|
|
|
|
#include <base/snprintf.h>
|
|
|
|
#include <util/arg_string.h>
|
|
|
|
#include <util/string.h>
|
|
|
|
|
|
|
|
namespace Genode { struct Session_label; }
|
|
|
|
|
|
|
|
struct Genode::Session_label : String<160>
|
|
|
|
{
|
2016-05-12 14:58:51 +02:00
|
|
|
private:
|
2016-01-12 14:11:58 +01:00
|
|
|
|
2016-05-12 14:58:51 +02:00
|
|
|
static char const *_separator() { return " -> "; }
|
|
|
|
static size_t _separator_len() { return 4; }
|
2016-01-12 14:11:58 +01:00
|
|
|
|
2016-05-12 14:58:51 +02:00
|
|
|
public:
|
2016-01-12 14:11:58 +01:00
|
|
|
|
2018-06-05 13:52:26 +02:00
|
|
|
using String = String<capacity()>;
|
2016-05-12 14:58:51 +02:00
|
|
|
using String::String;
|
2016-01-12 14:11:58 +01:00
|
|
|
|
2016-05-12 14:58:51 +02:00
|
|
|
Session_label last_element() const
|
|
|
|
{
|
|
|
|
char const * const full = string();
|
|
|
|
size_t const full_len = strlen(full);
|
2016-01-12 14:11:58 +01:00
|
|
|
|
2016-05-12 14:58:51 +02:00
|
|
|
if (full_len < _separator_len())
|
|
|
|
return full;
|
|
|
|
|
2016-11-24 15:28:32 +01:00
|
|
|
unsigned i = full_len - _separator_len();
|
|
|
|
do {
|
2016-05-12 14:58:51 +02:00
|
|
|
if (!strcmp(_separator(), full + i, _separator_len()))
|
|
|
|
return full + i + _separator_len();
|
2016-11-24 15:28:32 +01:00
|
|
|
} while (i-- > 0);
|
2016-05-12 14:58:51 +02:00
|
|
|
|
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-07-13 19:07:09 +02:00
|
|
|
return Session_label(Cstring(full));
|
2016-05-12 14:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return part of the label without the last element
|
|
|
|
*/
|
|
|
|
inline Session_label prefix() const
|
|
|
|
{
|
|
|
|
if (length() < _separator_len() + 1)
|
|
|
|
return Session_label();
|
|
|
|
|
|
|
|
/* search for last occurrence of the separator */
|
|
|
|
unsigned prefix_len = length() - _separator_len() - 1;
|
|
|
|
char const * const full = string();
|
|
|
|
|
|
|
|
for (; prefix_len > 0; prefix_len--)
|
|
|
|
if (strcmp(full + prefix_len, _separator(), _separator_len()) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* construct new label with only the prefix part */
|
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-07-13 19:07:09 +02:00
|
|
|
return Session_label(Cstring(full, prefix_len));
|
2016-05-12 14:58:51 +02:00
|
|
|
}
|
2016-01-12 14:11:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
namespace Genode {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract label from session arguments in the form of 'label="..."'
|
|
|
|
*/
|
|
|
|
inline Session_label label_from_args(char const *args)
|
|
|
|
{
|
|
|
|
char buf[Session_label::capacity()];
|
|
|
|
Arg_string::find_arg(args, "label").string(buf, sizeof(buf), "");
|
|
|
|
|
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-07-13 19:07:09 +02:00
|
|
|
return Session_label(Cstring(buf));
|
2016-01-12 14:11:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a compound label in the form of 'prefix -> label'
|
|
|
|
*/
|
2016-05-12 14:58:51 +02:00
|
|
|
template <size_t N1, size_t N2>
|
|
|
|
inline Session_label prefixed_label(String<N1> const &prefix,
|
|
|
|
String<N2> const &label)
|
2016-01-12 14:11:58 +01:00
|
|
|
{
|
2016-11-24 15:28:32 +01:00
|
|
|
String<N1 + N2 + 4> const prefixed_label(prefix, " -> ", label);
|
|
|
|
return Session_label(prefixed_label);
|
2016-01-12 14:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__BASE__SESSION_LABEL_H_ */
|