diff --git a/kernel/include/vfs/path.hpp b/kernel/include/vfs/path.hpp index d1a5a02f..60dc0ae9 100644 --- a/kernel/include/vfs/path.hpp +++ b/kernel/include/vfs/path.hpp @@ -34,6 +34,11 @@ struct path { */ path(const std::string& path); + /*! + * \brief Construct a path by concatenating a base path and a string path + */ + path(const path& base_path, const char* path); + /*! * \brief Construct a path by concatenating a base path and a string path */ diff --git a/kernel/src/path.cpp b/kernel/src/path.cpp index 0df5b1ff..c07b365e 100644 --- a/kernel/src/path.cpp +++ b/kernel/src/path.cpp @@ -9,13 +9,9 @@ #include "assert.hpp" -path::path(){ - //Nothing to init -} - -path::path(const char* path){ - thor_assert(path, "Invalid base path"); +namespace { +void append_path_to_names(const char* path, std::vector& names){ size_t i = 0; if(path[0] == '/'){ @@ -43,6 +39,18 @@ path::path(const char* path){ } } +} // end of anonymous namespaces + +path::path(){ + //Nothing to init +} + +path::path(const char* path){ + thor_assert(path, "Invalid base path"); + + append_path_to_names(path, names); +} + path::path(const std::string& path){ if(path[0] == '/'){ names.push_back("/"); @@ -51,6 +59,16 @@ path::path(const std::string& path){ std::split_append(path, names, '/'); } +path::path(const path& base_path, const char* p){ + thor_assert(!p || p[0] != '/', "Impossible to add absolute path to another path"); + + names.reserve(base_path.size() + 1); + + std::copy(base_path.begin(), base_path.end(), std::back_inserter(names)); + + append_path_to_names(p, names); +} + path::path(const path& base_path, const std::string& p){ thor_assert(p.empty() || p[0] != '/', "Impossible to add absolute path to another path"); @@ -212,7 +230,7 @@ path operator/(const path& lhs, const std::string& rhs){ } path operator/(const path& lhs, const char* rhs){ - return {lhs, path(rhs)}; + return {lhs, rhs}; } path operator/(const std::string& lhs, const path& rhs){