ENH: Add support for relative paths and cleanup

This commit is contained in:
Andy Cedilnik 2006-03-21 16:02:47 -05:00
parent 1cdd8b4fbd
commit 9dbf4438dc
2 changed files with 77 additions and 29 deletions

View File

@ -61,22 +61,23 @@ public:
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
Glob::Glob() Glob::Glob()
{ {
m_Internals = new GlobInternals; this->Internals = new GlobInternals;
m_Recurse = false; this->Recurse = false;
this->Relative = "";
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
Glob::~Glob() Glob::~Glob()
{ {
delete m_Internals; delete this->Internals;
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void Glob::Escape(int ch, char* buffer) void Glob::Escape(int ch, char* buffer)
{ {
if (! ( if (! (
'a' <= ch && ch <= 'z' || 'a' <= ch && ch <= 'z' ||
'A' <= ch && ch <= 'Z' || 'A' <= ch && ch <= 'Z' ||
'0' <= ch && ch <= '9') ) '0' <= ch && ch <= '9') )
{ {
sprintf(buffer, "\\%c", ch); sprintf(buffer, "\\%c", ch);
@ -95,13 +96,13 @@ void Glob::Escape(int ch, char* buffer)
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
kwsys_stl::vector<kwsys_stl::string>& Glob::GetFiles() kwsys_stl::vector<kwsys_stl::string>& Glob::GetFiles()
{ {
return m_Internals->Files; return this->Internals->Files;
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
kwsys_stl::string Glob::ConvertExpression(const kwsys_stl::string& expr) kwsys_stl::string Glob::ConvertExpression(const kwsys_stl::string& expr)
{ {
kwsys_stl::string::size_type i = 0; kwsys_stl::string::size_type i = 0;
kwsys_stl::string::size_type n = expr.size(); kwsys_stl::string::size_type n = expr.size();
@ -130,7 +131,7 @@ kwsys_stl::string Glob::ConvertExpression(const kwsys_stl::string& expr)
if ( j < n && expr[j] == ']' ) if ( j < n && expr[j] == ']' )
{ {
j = j+1; j = j+1;
} }
while ( j < n && expr[j] != ']' ) while ( j < n && expr[j] != ']' )
{ {
j = j+1; j = j+1;
@ -224,9 +225,10 @@ void Glob::RecurseDirectory(kwsys_stl::string::size_type start,
if ( !dir_only || !kwsys::SystemTools::FileIsDirectory(realname.c_str()) ) if ( !dir_only || !kwsys::SystemTools::FileIsDirectory(realname.c_str()) )
{ {
if ( m_Internals->Expressions[m_Internals->Expressions.size()-1].find(fname.c_str()) ) if ( this->Internals->Expressions[
this->Internals->Expressions.size()-1].find(fname.c_str()) )
{ {
m_Internals->Files.push_back(realname); this->AddFile(this->Internals->Files, realname.c_str());
} }
} }
if ( kwsys::SystemTools::FileIsDirectory(realname.c_str()) ) if ( kwsys::SystemTools::FileIsDirectory(realname.c_str()) )
@ -237,12 +239,12 @@ void Glob::RecurseDirectory(kwsys_stl::string::size_type start,
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void Glob::ProcessDirectory(kwsys_stl::string::size_type start, void Glob::ProcessDirectory(kwsys_stl::string::size_type start,
const kwsys_stl::string& dir, bool dir_only) const kwsys_stl::string& dir, bool dir_only)
{ {
//kwsys_ios::cout << "ProcessDirectory: " << dir << kwsys_ios::endl; //kwsys_ios::cout << "ProcessDirectory: " << dir << kwsys_ios::endl;
bool last = ( start == m_Internals->Expressions.size()-1 ); bool last = ( start == this->Internals->Expressions.size()-1 );
if ( last && m_Recurse ) if ( last && this->Recurse )
{ {
this->RecurseDirectory(start, dir, dir_only); this->RecurseDirectory(start, dir, dir_only);
return; return;
@ -289,19 +291,21 @@ void Glob::ProcessDirectory(kwsys_stl::string::size_type start,
} }
//kwsys_ios::cout << "Look at file: " << fname << kwsys_ios::endl; //kwsys_ios::cout << "Look at file: " << fname << kwsys_ios::endl;
//kwsys_ios::cout << "Match: " << m_Internals->TextExpressions[start].c_str() << kwsys_ios::endl; //kwsys_ios::cout << "Match: "
// << this->Internals->TextExpressions[start].c_str() << kwsys_ios::endl;
//kwsys_ios::cout << "Full name: " << fullname << kwsys_ios::endl; //kwsys_ios::cout << "Full name: " << fullname << kwsys_ios::endl;
if ( (!dir_only || !last) && !kwsys::SystemTools::FileIsDirectory(realname.c_str()) ) if ( (!dir_only || !last) &&
!kwsys::SystemTools::FileIsDirectory(realname.c_str()) )
{ {
continue; continue;
} }
if ( m_Internals->Expressions[start].find(fname.c_str()) ) if ( this->Internals->Expressions[start].find(fname.c_str()) )
{ {
if ( last ) if ( last )
{ {
m_Internals->Files.push_back(realname); this->AddFile(this->Internals->Files, realname.c_str());
} }
else else
{ {
@ -318,8 +322,8 @@ bool Glob::FindFiles(const kwsys_stl::string& inexpr)
kwsys_stl::string::size_type cc; kwsys_stl::string::size_type cc;
kwsys_stl::string expr = inexpr; kwsys_stl::string expr = inexpr;
m_Internals->Expressions.clear(); this->Internals->Expressions.clear();
m_Internals->Files.clear(); this->Internals->Files.clear();
if ( !kwsys::SystemTools::FileIsFullPath(expr.c_str()) ) if ( !kwsys::SystemTools::FileIsFullPath(expr.c_str()) )
{ {
@ -336,7 +340,7 @@ bool Glob::FindFiles(const kwsys_stl::string& inexpr)
{ {
last_slash = cc; last_slash = cc;
} }
if ( cc > 0 && if ( cc > 0 &&
(expr[cc] == '[' || expr[cc] == '?' || expr[cc] == '*') && (expr[cc] == '[' || expr[cc] == '?' || expr[cc] == '*') &&
expr[cc-1] != '\\' ) expr[cc-1] != '\\' )
{ {
@ -345,7 +349,8 @@ bool Glob::FindFiles(const kwsys_stl::string& inexpr)
} }
if ( last_slash > 0 ) if ( last_slash > 0 )
{ {
//kwsys_ios::cout << "I can skip: " << fexpr.substr(0, last_slash) << kwsys_ios::endl; //kwsys_ios::cout << "I can skip: " << fexpr.substr(0, last_slash)
//<< kwsys_ios::endl;
skip = last_slash; skip = last_slash;
} }
if ( skip == 0 ) if ( skip == 0 )
@ -408,7 +413,7 @@ bool Glob::FindFiles(const kwsys_stl::string& inexpr)
if ( skip > 0 ) if ( skip > 0 )
{ {
this->ProcessDirectory(0, fexpr.substr(0, skip) + "/", this->ProcessDirectory(0, fexpr.substr(0, skip) + "/",
true); true);
} }
else else
{ {
@ -417,12 +422,47 @@ bool Glob::FindFiles(const kwsys_stl::string& inexpr)
return true; return true;
} }
//----------------------------------------------------------------------------
void Glob::AddExpression(const char* expr) void Glob::AddExpression(const char* expr)
{ {
m_Internals->Expressions.push_back( this->Internals->Expressions.push_back(
kwsys::RegularExpression( kwsys::RegularExpression(
this->ConvertExpression(expr).c_str())); this->ConvertExpression(expr).c_str()));
m_Internals->TextExpressions.push_back(this->ConvertExpression(expr)); this->Internals->TextExpressions.push_back(this->ConvertExpression(expr));
}
//----------------------------------------------------------------------------
void Glob::SetRelative(const char* dir)
{
if ( !dir )
{
this->Relative = "";
return;
}
this->Relative = dir;
}
//----------------------------------------------------------------------------
const char* Glob::GetRelative()
{
if ( this->Relative.empty() )
{
return 0;
}
return this->Relative.c_str();
}
//----------------------------------------------------------------------------
void Glob::AddFile(kwsys_stl::vector<kwsys_stl::string>& files, const char* file)
{
if ( !this->Relative.empty() )
{
files.push_back(kwsys::SystemTools::RelativePath(this->Relative.c_str(), file));
}
else
{
files.push_back(file);
}
} }
} // namespace KWSYS_NAMESPACE } // namespace KWSYS_NAMESPACE

View File

@ -54,12 +54,16 @@ public:
//! Set recurse to true to match subdirectories. //! Set recurse to true to match subdirectories.
void RecurseOn() { this->SetRecurse(true); } void RecurseOn() { this->SetRecurse(true); }
void RecurseOff() { this->SetRecurse(false); } void RecurseOff() { this->SetRecurse(false); }
void SetRecurse(bool i) { m_Recurse = i; } void SetRecurse(bool i) { this->Recurse = i; }
bool GetRecurse() { return m_Recurse; } bool GetRecurse() { return this->Recurse; }
//! Set relative to true to only show relative path to files.
void SetRelative(const char* dir);
const char* GetRelative();
protected: protected:
//! Process directory //! Process directory
void ProcessDirectory(kwsys_stl::string::size_type start, void ProcessDirectory(kwsys_stl::string::size_type start,
const kwsys_stl::string& dir, bool dir_only); const kwsys_stl::string& dir, bool dir_only);
//! Process last directory, but only when recurse flags is on. That is //! Process last directory, but only when recurse flags is on. That is
@ -78,8 +82,12 @@ protected:
//! Add regular expression //! Add regular expression
void AddExpression(const char* expr); void AddExpression(const char* expr);
GlobInternals* m_Internals; //! Add a file to the list
bool m_Recurse; void AddFile(kwsys_stl::vector<kwsys_stl::string>& files, const char* file);
GlobInternals* Internals;
bool Recurse;
kwsys_stl::string Relative;
}; };
} // namespace @KWSYS_NAMESPACE@ } // namespace @KWSYS_NAMESPACE@