2003-07-08 22:18:17 +04:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
Program: CMake - Cross-Platform Makefile Generator
|
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
|
|
|
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE. See the above copyright notices for more information.
|
|
|
|
|
|
|
|
=========================================================================*/
|
|
|
|
#include "cmGlob.h"
|
|
|
|
|
|
|
|
#include <cmsys/Directory.hxx>
|
|
|
|
#include <cmsys/RegularExpression.hxx>
|
|
|
|
#include <cmsys/SystemTools.hxx>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2003-07-23 18:26:37 +04:00
|
|
|
#include <ctype.h>
|
2003-07-08 22:18:17 +04:00
|
|
|
|
2003-07-24 19:12:03 +04:00
|
|
|
#if defined( _WIN32 ) || defined( APPLE ) || defined( __CYGWIN__ )
|
|
|
|
// On Windows and apple, no difference between lower and upper case
|
|
|
|
#define CM_GLOB_CASE_INDEPENDENT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined( _WIN32 ) || defined( __CYGWIN__ )
|
|
|
|
// Handle network paths
|
|
|
|
#define CM_GLOB_SUPPORT_NETWORK_PATHS
|
|
|
|
#endif
|
|
|
|
|
2003-07-08 22:18:17 +04:00
|
|
|
class cmGlobInternal
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::vector<std::string> Files;
|
|
|
|
std::vector<cmsys::RegularExpression> Expressions;
|
2003-07-30 02:06:33 +04:00
|
|
|
std::vector<std::string> TextExpressions;
|
2003-07-08 22:18:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
cmGlob::cmGlob()
|
|
|
|
{
|
|
|
|
m_Internals = new cmGlobInternal;
|
2003-07-14 17:15:13 +04:00
|
|
|
m_Recurse = false;
|
2003-07-08 22:18:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmGlob::~cmGlob()
|
|
|
|
{
|
|
|
|
delete m_Internals;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmGlob::Escape(int ch, char* buffer)
|
|
|
|
{
|
|
|
|
if (! (
|
|
|
|
'a' <= ch && ch <= 'z' ||
|
|
|
|
'A' <= ch && ch <= 'Z' ||
|
|
|
|
'0' <= ch && ch <= '9') )
|
|
|
|
{
|
|
|
|
sprintf(buffer, "\\%c", ch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-07-24 19:12:03 +04:00
|
|
|
#if defined( CM_GLOB_CASE_INDEPENDENT )
|
2003-07-23 18:26:37 +04:00
|
|
|
// On Windows and apple, no difference between lower and upper case
|
|
|
|
sprintf(buffer, "%c", tolower(ch));
|
|
|
|
#else
|
2003-07-08 22:18:17 +04:00
|
|
|
sprintf(buffer, "%c", ch);
|
2003-07-23 18:26:37 +04:00
|
|
|
#endif
|
2003-07-08 22:18:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>& cmGlob::GetFiles()
|
|
|
|
{
|
|
|
|
return m_Internals->Files;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmGlob::ConvertExpression(const std::string& expr)
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string::size_type i = 0;
|
|
|
|
std::string::size_type n = expr.size();
|
|
|
|
|
|
|
|
std::string res = "^";
|
|
|
|
std::string stuff = "";
|
|
|
|
|
|
|
|
while ( i < n )
|
|
|
|
{
|
|
|
|
int c = expr[i];
|
|
|
|
i = i+1;
|
|
|
|
if ( c == '*' )
|
|
|
|
{
|
|
|
|
res = res + ".*";
|
|
|
|
}
|
|
|
|
else if ( c == '?' )
|
|
|
|
{
|
|
|
|
res = res + ".";
|
|
|
|
}
|
|
|
|
else if ( c == '[' )
|
|
|
|
{
|
|
|
|
std::string::size_type j = i;
|
2003-07-21 21:46:47 +04:00
|
|
|
if ( j < n && ( expr[j] == '!' || expr[j] == '^' ) )
|
2003-07-08 22:18:17 +04:00
|
|
|
{
|
|
|
|
j = j+1;
|
|
|
|
}
|
|
|
|
if ( j < n && expr[j] == ']' )
|
|
|
|
{
|
|
|
|
j = j+1;
|
|
|
|
}
|
|
|
|
while ( j < n && expr[j] != ']' )
|
|
|
|
{
|
|
|
|
j = j+1;
|
|
|
|
}
|
|
|
|
if ( j >= n )
|
|
|
|
{
|
|
|
|
res = res + "\\[";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stuff = "";
|
|
|
|
std::string::size_type cc;
|
|
|
|
for ( cc = i; cc < j; cc ++ )
|
|
|
|
{
|
|
|
|
if ( expr[cc] == '\\' )
|
|
|
|
{
|
|
|
|
stuff += "\\\\";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stuff += expr[cc];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = j+1;
|
2003-07-21 21:46:47 +04:00
|
|
|
if ( stuff[0] == '!' || stuff[0] == '^' )
|
2003-07-08 22:18:17 +04:00
|
|
|
{
|
|
|
|
stuff = '^' + stuff.substr(1);
|
|
|
|
}
|
|
|
|
else if ( stuff[0] == '^' )
|
|
|
|
{
|
|
|
|
stuff = '\\' + stuff;
|
|
|
|
}
|
|
|
|
res = res + "[" + stuff + "]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buffer[100];
|
|
|
|
buffer[0] = 0;
|
|
|
|
this->Escape(c, buffer);
|
|
|
|
res = res + buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res + "$";
|
|
|
|
}
|
|
|
|
|
2003-07-14 17:15:13 +04:00
|
|
|
void cmGlob::RecurseDirectory(const std::string& dir, bool dir_only)
|
|
|
|
{
|
|
|
|
cmsys::Directory d;
|
|
|
|
if ( !d.Load(dir.c_str()) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned long cc;
|
|
|
|
std::string fullname;
|
2003-07-23 18:26:37 +04:00
|
|
|
std::string fname;
|
2003-07-14 17:15:13 +04:00
|
|
|
for ( cc = 0; cc < d.GetNumberOfFiles(); cc ++ )
|
|
|
|
{
|
2003-07-23 18:26:37 +04:00
|
|
|
fname = d.GetFile(cc);
|
|
|
|
if ( strcmp(fname.c_str(), ".") == 0 ||
|
|
|
|
strcmp(fname.c_str(), "..") == 0 )
|
2003-07-14 17:15:13 +04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2003-07-23 18:26:37 +04:00
|
|
|
|
2003-07-24 19:12:03 +04:00
|
|
|
#if defined( CM_GLOB_CASE_INDEPENDENT )
|
2003-07-23 18:26:37 +04:00
|
|
|
// On Windows and apple, no difference between lower and upper case
|
|
|
|
fname = cmsys::SystemTools::LowerCase(fname);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fullname = dir + "/" + fname;
|
2003-07-14 17:15:13 +04:00
|
|
|
if ( !dir_only || !cmsys::SystemTools::FileIsDirectory(fullname.c_str()) )
|
|
|
|
{
|
2003-07-23 18:26:37 +04:00
|
|
|
if ( m_Internals->Expressions[m_Internals->Expressions.size()-1].find(fname.c_str()) )
|
2003-07-14 17:15:13 +04:00
|
|
|
{
|
|
|
|
m_Internals->Files.push_back(fullname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( cmsys::SystemTools::FileIsDirectory(fullname.c_str()) )
|
|
|
|
{
|
|
|
|
this->RecurseDirectory(fullname, dir_only);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-08 22:18:17 +04:00
|
|
|
void cmGlob::ProcessDirectory(std::string::size_type start,
|
|
|
|
const std::string& dir, bool dir_only)
|
|
|
|
{
|
2003-07-30 02:06:33 +04:00
|
|
|
//std::cout << "ProcessDirectory: " << dir << std::endl;
|
2003-07-14 17:15:13 +04:00
|
|
|
bool last = ( start == m_Internals->Expressions.size()-1 );
|
|
|
|
if ( last && m_Recurse )
|
|
|
|
{
|
|
|
|
this->RecurseDirectory(dir, dir_only);
|
|
|
|
return;
|
|
|
|
}
|
2003-07-08 22:18:17 +04:00
|
|
|
cmsys::Directory d;
|
|
|
|
if ( !d.Load(dir.c_str()) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned long cc;
|
|
|
|
std::string fullname;
|
2003-07-23 18:26:37 +04:00
|
|
|
std::string fname;
|
2003-07-08 22:18:17 +04:00
|
|
|
for ( cc = 0; cc < d.GetNumberOfFiles(); cc ++ )
|
|
|
|
{
|
2003-07-23 18:26:37 +04:00
|
|
|
fname = d.GetFile(cc);
|
|
|
|
if ( strcmp(fname.c_str(), ".") == 0 ||
|
|
|
|
strcmp(fname.c_str(), "..") == 0 )
|
2003-07-08 22:18:17 +04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2003-07-23 18:26:37 +04:00
|
|
|
|
2003-07-24 19:12:03 +04:00
|
|
|
#if defined( CM_GLOB_CASE_INDEPENDENT )
|
2003-07-23 18:26:37 +04:00
|
|
|
// On Windows and apple, no difference between lower and upper case
|
|
|
|
fname = cmsys::SystemTools::LowerCase(fname);
|
|
|
|
#endif
|
|
|
|
|
2003-07-08 22:18:17 +04:00
|
|
|
if ( start == 0 )
|
|
|
|
{
|
2003-07-23 18:26:37 +04:00
|
|
|
fullname = dir + fname;
|
2003-07-08 22:18:17 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-07-23 18:26:37 +04:00
|
|
|
fullname = dir + "/" + fname;
|
2003-07-08 22:18:17 +04:00
|
|
|
}
|
|
|
|
|
2003-07-30 02:06:33 +04:00
|
|
|
//std::cout << "Look at file: " << fname << std::endl;
|
|
|
|
//std::cout << "Match: " << m_Internals->TextExpressions[start].c_str() << std::endl;
|
|
|
|
//std::cout << "Full name: " << fullname << std::endl;
|
|
|
|
|
2003-07-08 22:18:17 +04:00
|
|
|
if ( (!dir_only || !last) && !cmsys::SystemTools::FileIsDirectory(fullname.c_str()) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2003-07-21 23:29:29 +04:00
|
|
|
|
2003-07-23 18:26:37 +04:00
|
|
|
if ( m_Internals->Expressions[start].find(fname.c_str()) )
|
2003-07-08 22:18:17 +04:00
|
|
|
{
|
|
|
|
if ( last )
|
|
|
|
{
|
|
|
|
m_Internals->Files.push_back(fullname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-07-30 02:06:33 +04:00
|
|
|
this->ProcessDirectory(start+1, fullname + "/", dir_only);
|
2003-07-08 22:18:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmGlob::FindFiles(const std::string& inexpr)
|
|
|
|
{
|
|
|
|
std::string cexpr;
|
|
|
|
std::string::size_type cc;
|
|
|
|
std::string expr = inexpr;
|
|
|
|
|
|
|
|
m_Internals->Expressions.empty();
|
|
|
|
m_Internals->Files.empty();
|
|
|
|
|
|
|
|
if ( !cmsys::SystemTools::FileIsFullPath(expr.c_str()) )
|
|
|
|
{
|
|
|
|
expr = cmsys::SystemTools::GetCurrentWorkingDirectory();
|
|
|
|
expr += "/" + inexpr;
|
|
|
|
}
|
2003-07-24 19:12:03 +04:00
|
|
|
|
2003-07-24 19:53:03 +04:00
|
|
|
int skip = 0;
|
2003-07-30 02:06:33 +04:00
|
|
|
|
|
|
|
int last_slash = 0;
|
|
|
|
for ( cc = 0; cc < expr.size(); cc ++ )
|
|
|
|
{
|
|
|
|
if ( cc > 0 && expr[cc] == '/' && expr[cc-1] != '\\' )
|
|
|
|
{
|
|
|
|
last_slash = cc;
|
|
|
|
}
|
|
|
|
if ( cc > 0 &&
|
|
|
|
(expr[cc] == '[' || expr[cc] == '?' || expr[cc] == '*') &&
|
|
|
|
expr[cc-1] != '\\' )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( last_slash > 0 )
|
2003-07-24 19:12:03 +04:00
|
|
|
{
|
2003-07-30 02:06:33 +04:00
|
|
|
//std::cout << "I can skip: " << inexpr.substr(0, last_slash) << std::endl;
|
|
|
|
skip = last_slash;
|
|
|
|
}
|
|
|
|
if ( skip == 0 )
|
|
|
|
{
|
|
|
|
#if defined( CM_GLOB_SUPPORT_NETWORK_PATHS )
|
|
|
|
// Handle network paths
|
|
|
|
if ( expr[0] == '/' && expr[1] == '/' )
|
2003-07-24 19:12:03 +04:00
|
|
|
{
|
2003-07-30 02:06:33 +04:00
|
|
|
int cnt = 0;
|
|
|
|
for ( cc = 2; cc < expr.size(); cc ++ )
|
2003-07-24 19:12:03 +04:00
|
|
|
{
|
2003-07-30 02:06:33 +04:00
|
|
|
if ( expr[cc] == '/' )
|
2003-07-24 19:12:03 +04:00
|
|
|
{
|
2003-07-30 02:06:33 +04:00
|
|
|
cnt ++;
|
|
|
|
if ( cnt == 2 )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2003-07-24 19:12:03 +04:00
|
|
|
}
|
|
|
|
}
|
2003-07-30 02:06:33 +04:00
|
|
|
skip = cc + 1;
|
2003-07-24 19:12:03 +04:00
|
|
|
}
|
2003-07-30 02:06:33 +04:00
|
|
|
else
|
2003-07-24 19:12:03 +04:00
|
|
|
#endif
|
2003-07-30 02:06:33 +04:00
|
|
|
// Handle drive letters on Windows
|
|
|
|
if ( expr[1] == ':' && expr[0] != '/' )
|
|
|
|
{
|
|
|
|
skip = 2;
|
|
|
|
}
|
|
|
|
}
|
2003-07-24 19:12:03 +04:00
|
|
|
|
2003-07-30 02:06:33 +04:00
|
|
|
if ( skip > 0 )
|
2003-07-21 23:29:29 +04:00
|
|
|
{
|
2003-07-30 02:06:33 +04:00
|
|
|
expr = expr.substr(skip);
|
2003-07-21 23:29:29 +04:00
|
|
|
}
|
2003-07-30 02:06:33 +04:00
|
|
|
|
2003-07-24 19:12:03 +04:00
|
|
|
cexpr = "";
|
2003-07-08 22:18:17 +04:00
|
|
|
for ( cc = 0; cc < expr.size(); cc ++ )
|
|
|
|
{
|
|
|
|
int ch = expr[cc];
|
|
|
|
if ( ch == '/' )
|
|
|
|
{
|
|
|
|
if ( cexpr.size() > 0 )
|
|
|
|
{
|
|
|
|
this->AddExpression(cexpr.c_str());
|
|
|
|
}
|
|
|
|
cexpr = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cexpr.append(1, (char)ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( cexpr.size() > 0 )
|
|
|
|
{
|
|
|
|
this->AddExpression(cexpr.c_str());
|
|
|
|
}
|
2003-07-24 19:12:03 +04:00
|
|
|
|
|
|
|
// Handle network paths
|
|
|
|
if ( skip > 0 )
|
|
|
|
{
|
2003-07-30 02:06:33 +04:00
|
|
|
this->ProcessDirectory(0, inexpr.substr(0, skip) + "/",
|
2003-07-24 19:12:03 +04:00
|
|
|
true);
|
|
|
|
}
|
|
|
|
else
|
2003-07-21 23:29:29 +04:00
|
|
|
{
|
|
|
|
this->ProcessDirectory(0, "/", true);
|
|
|
|
}
|
2003-07-08 22:18:17 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmGlob::AddExpression(const char* expr)
|
|
|
|
{
|
|
|
|
m_Internals->Expressions.push_back(
|
|
|
|
cmsys::RegularExpression(
|
|
|
|
this->ConvertExpression(expr).c_str()));
|
2003-07-30 02:06:33 +04:00
|
|
|
m_Internals->TextExpressions.push_back(this->ConvertExpression(expr));
|
2003-07-08 22:18:17 +04:00
|
|
|
}
|
|
|
|
|