From b221d046a6c1005f2110304c0ca36133e9e96cc4 Mon Sep 17 00:00:00 2001 From: Andy Cedilnik Date: Sun, 10 Oct 2004 12:14:58 -0400 Subject: [PATCH] ENH: Add method to retrieve parent directory and for checking if directory is a subdirectory of another directory --- Source/kwsys/SystemTools.cxx | 40 +++++++++++++++++++++++++++++++++ Source/kwsys/SystemTools.hxx.in | 6 +++++ 2 files changed, 46 insertions(+) diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index 3a513e66e..c871f543b 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -2010,6 +2010,46 @@ bool SystemTools::SetPermissions(const char* file, mode_t mode) return true; } +std::string SystemTools::GetParentDirectory(const char* fileOrDir) +{ + if ( !fileOrDir || !*fileOrDir ) + { + return ""; + } + std::string res = fileOrDir; + SystemTools::ConvertToUnixSlashes(res); + std::string::size_type cc = res.size()-1; + if ( res[cc] == '/' ) + { + cc --; + } + for ( ; cc > 0; cc -- ) + { + if ( res[cc] == '/' ) + { + break; + } + } + return res.substr(0, cc); +} + +bool SystemTools::IsSubDirectory(const char* cSubdir, const char* cDir) +{ + std::string subdir = cSubdir; + std::string dir = cDir; + SystemTools::ConvertToUnixSlashes(dir); + std::string path = subdir; + do + { + path = SystemTools::GetParentDirectory(path.c_str()); + if ( dir == path ) + { + return true; + } + } + while ( path.size() > dir.size() ); + return false; +} // These must NOT be initialized. Default initialization to zero is // necessary. diff --git a/Source/kwsys/SystemTools.hxx.in b/Source/kwsys/SystemTools.hxx.in index 1d79f2ba7..89e32a36d 100644 --- a/Source/kwsys/SystemTools.hxx.in +++ b/Source/kwsys/SystemTools.hxx.in @@ -316,6 +316,12 @@ public: static bool GetPermissions(const char* file, mode_t& mode); static bool SetPermissions(const char* file, mode_t mode); + /** Get the parent directory of the directory or file */ + static std::string GetParentDirectory(const char* fileOrDir); + + /** Check if the given file or directory is in subdirectory of dir */ + static bool IsSubDirectory(const char* fileOrDir, const char* dir); + protected: // these two functions can be called from ConvertToOutputPath /**