diff options
Diffstat (limited to 'Source/kwsys')
-rw-r--r-- | Source/kwsys/SystemTools.cxx | 60 | ||||
-rw-r--r-- | Source/kwsys/SystemTools.hxx.in | 17 |
2 files changed, 77 insertions, 0 deletions
diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index b3b1e7a..84f207a 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -1614,6 +1614,66 @@ kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative, return newPath; } +//---------------------------------------------------------------------------- +void SystemTools::SplitPath(const char* p, + kwsys_stl::vector<kwsys_stl::string>& components) +{ + // Identify the root component. + const char* c = p; + if(c[0] == '/' && c[1] == '/') + { + // Network path. + components.push_back("//"); + c += 2; + } + else if(c[0] == '/') + { + // Unix path. + components.push_back("/"); + c += 1; + } + else if(c[0] && c[1] == ':' && c[2] == '/') + { + // Windows path. + std::string root = "_:/"; + root[0] = c[0]; + components.push_back(root); + c += 3; + } + else if(c[0] && c[1] == ':') + { + // Path relative to a windows drive working directory. + std::string root = "_:"; + root[0] = c[0]; + components.push_back(root); + c += 2; + } + else + { + // Relative path. + components.push_back(""); + } + + // Parse the remaining components. + const char* first = c; + const char* last = first; + for(;*last; ++last) + { + if(*last == '/') + { + // End of a component. Save it. + components.push_back(std::string(first, last-first)); + first = last+1; + } + } + + // Save the last component unless there were no components. + if(last != c) + { + components.push_back(std::string(first, last-first)); + } +} + bool SystemTools::Split(const char* str, kwsys_stl::vector<kwsys_stl::string>& lines) { kwsys_stl::string data(str); diff --git a/Source/kwsys/SystemTools.hxx.in b/Source/kwsys/SystemTools.hxx.in index e2fee45..e65ee05 100644 --- a/Source/kwsys/SystemTools.hxx.in +++ b/Source/kwsys/SystemTools.hxx.in @@ -237,6 +237,23 @@ public: static kwsys_stl::string CollapseFullPath(const char* in_relative); static kwsys_stl::string CollapseFullPath(const char* in_relative, const char* in_base); + + /** + * Split a path name into its basic components. The first component + * is one of the following roots: + * "/" = UNIX + * "c:/" = Windows full path (can be any drive letter) + * "c:" = Windows drive-letter relative path (can be any drive letter) + * "//" = Network path + * "" = Relative path + * The remaining components form the path. If there is a trailing + * slash then the last component is the empty string. The + * components can be recombined as "c[0]c[1]/c[2]/.../c[n]" to + * produce the original path. The input is assumed to be formatted + * with forward slashes. + */ + static void SplitPath(const char* p, + kwsys_stl::vector<kwsys_stl::string>& components); ///! return path of a full filename (no trailing slashes). static kwsys_stl::string GetFilenamePath(const kwsys_stl::string&); |