Add path::is_absolute.

This is actually not entirely trivial since we can have a path that starts with a slash but includes more ‘..’ components than there are actual directories in the path.
This commit is contained in:
Allan Odgaard
2012-08-13 22:53:16 +02:00
parent b85aafb9a9
commit 4259a8f884
3 changed files with 32 additions and 0 deletions

View File

@@ -178,6 +178,17 @@ namespace path
return !path.empty() && path[0] == '/' ? normalize(path) : normalize(base + "/" + path);
}
bool is_absolute (std::string const& path)
{
if(!path.empty() && path[0] == '/')
{
std::string p = normalize(path);
if(p != "/.." && p.find("/../") != 0)
return true;
}
return false;
}
std::string with_tilde (std::string const& p)
{
std::string const& base = home();

View File

@@ -21,6 +21,7 @@ namespace path
PUBLIC std::string join (std::string const& base, std::string const& path); // this will normalize the (resulting) path
PUBLIC bool is_absolute (std::string const& path);
PUBLIC std::string with_tilde (std::string const& path); // /Users/me/foo.html.erb → ~/foo.html.erb
PUBLIC std::string relative_to (std::string const& path, std::string const& base); // /Users/me/foo.html.erb (arg: ~/Desktop) → ../foo.html.erb