diff options
Diffstat (limited to 'tools/minicargo/path.cpp')
-rw-r--r-- | tools/minicargo/path.cpp | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/tools/minicargo/path.cpp b/tools/minicargo/path.cpp index d0316548..93281e0f 100644 --- a/tools/minicargo/path.cpp +++ b/tools/minicargo/path.cpp @@ -1,6 +1,10 @@ /* */ #include "path.h" +#if _WIN32 +#else +# include <unistd.h> // getcwd/chdir +#endif helpers::path::path(const char* s): m_str(s) @@ -28,6 +32,36 @@ helpers::path::path(const char* s): } } +helpers::path helpers::path::to_absolute() const +{ + if(!this->is_valid()) + throw ::std::runtime_error("Calling to_absolute() on an invalid path"); + + if(this->m_str[0] == SEP) + return *this; + + #if _WIN32 + #else + char cwd[1024]; + if( !getcwd(cwd, sizeof(cwd)) ) + throw ::std::runtime_error("Calling getcwd() failed in path::to_absolute()"); + #endif + auto rv = path(cwd); + for(auto comp : *this) + { + if(comp == ".") + ; + else if( comp == ".." ) + rv.pop_component(); + else + rv /= comp; + } + #if _WIN32 + #else + #endif + return rv; +} + helpers::path helpers::path::normalise() const { path rv; @@ -154,4 +188,4 @@ void helpers::path::ComponentsIter::operator++() if(end == ::std::string::npos) end = p.m_str.size(); } -}
\ No newline at end of file +} |