include/os/path.h: new path_from_label utility

Converts a session label hierarchy to a path hierarchy.

Issue #1777
Issue #2060
This commit is contained in:
Emery Hemingway 2016-08-23 17:05:30 +02:00 committed by Christian Helmuth
parent e39dc445b1
commit 6018f594cf
1 changed files with 44 additions and 2 deletions

View File

@ -328,6 +328,11 @@ class Genode::Path_base
return strcmp(_path, other) != 0;
}
char const *last_element()
{
return last_element(_path)+1;
}
/**
* Print path to output stream
*/
@ -357,7 +362,7 @@ class Genode::Path : public Path_base {
Path(char const *path, char const *pwd = 0)
: Path_base(_buf, sizeof(_buf), path, pwd) { }
constexpr size_t capacity() { return MAX_LEN; }
static constexpr size_t capacity() { return MAX_LEN; }
Path& operator=(char const *path)
{
@ -372,7 +377,44 @@ class Genode::Path : public Path_base {
Genode::strncpy(_buf, other._buf, MAX_LEN);
return *this;
}
};
namespace Genode {
template <typename PATH>
static inline PATH path_from_label(char const *label)
{
PATH path;
char tmp[path.capacity()];
size_t len = strlen(label);
size_t i = 0;
for (size_t j = 1; j < len; ++j) {
if (!strcmp(" -> ", label+j, 4)) {
path.append("/");
strncpy(tmp, label+i, (j-i)+1);
/* rewrite any directory seperators */
for (size_t k = 0; k < path.capacity(); ++k)
if (tmp[k] == '/')
tmp[k] = '_';
path.append(tmp);
j += 4;
i = j;
}
}
path.append("/");
strncpy(tmp, label+i, path.capacity());
/* rewrite any directory seperators */
for (size_t k = 0; k < path.capacity(); ++k)
if (tmp[k] == '/')
tmp[k] = '_';
path.append(tmp);
return path;
}
}
#endif /* _INCLUDE__OS__PATH_H_ */