ENH: Add method to find file in parent directories if it exists

This commit is contained in:
Andy Cedilnik 2004-10-17 18:50:49 -04:00
parent 9a92c429f2
commit bc66973061
2 changed files with 29 additions and 0 deletions

View File

@ -2071,6 +2071,29 @@ bool SystemTools::IsSubDirectory(const char* cSubdir, const char* cDir)
return false; return false;
} }
kwsys_stl::string SystemTools::FileExistsInParentDirectories(const char* fname,
const char* directory, const char* toplevel)
{
kwsys_stl::string file = fname;
SystemTools::ConvertToUnixSlashes(file);
kwsys_stl::string dir = directory;
SystemTools::ConvertToUnixSlashes(dir);
while ( 1 )
{
kwsys_stl::string path = dir + "/" + file;
if ( SystemTools::FileExists(path.c_str()) )
{
return path;
}
if ( dir.size() < strlen(toplevel) )
{
break;
}
dir = SystemTools::GetParentDirectory(dir.c_str());
}
return "";
}
// These must NOT be initialized. Default initialization to zero is // These must NOT be initialized. Default initialization to zero is
// necessary. // necessary.
unsigned int SystemToolsManagerCount; unsigned int SystemToolsManagerCount;

View File

@ -322,6 +322,12 @@ public:
/** Check if the given file or directory is in subdirectory of dir */ /** Check if the given file or directory is in subdirectory of dir */
static bool IsSubDirectory(const char* fileOrDir, const char* dir); static bool IsSubDirectory(const char* fileOrDir, const char* dir);
/** Check if the given file exists in one of the parent directory of the
* given file or directory and if it does, return the name of the file.
* Toplevel specifies the top-most directory to where it will look.*/
static kwsys_stl::string FileExistsInParentDirectories(const char* fname,
const char* directory, const char* toplevel);
protected: protected:
// these two functions can be called from ConvertToOutputPath // these two functions can be called from ConvertToOutputPath
/** /**