cmake/Source/kwsys/Glob.cxx

448 lines
12 KiB
C++
Raw Normal View History

2017-04-14 19:02:05 +02:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
#include "kwsysPrivate.h"
#include KWSYS_HEADER(Glob.hxx)
#include KWSYS_HEADER(Configure.hxx)
#include KWSYS_HEADER(RegularExpression.hxx)
#include KWSYS_HEADER(SystemTools.hxx)
#include KWSYS_HEADER(Directory.hxx)
// Work-around CMake dependency scanning limitation. This must
// duplicate the above list of headers.
#if 0
2017-04-14 19:02:05 +02:00
#include "Configure.hxx.in"
#include "Directory.hxx.in"
#include "Glob.hxx.in"
#include "RegularExpression.hxx.in"
#include "SystemTools.hxx.in"
#endif
2017-04-14 19:02:05 +02:00
#include <algorithm>
2015-11-17 17:22:37 +01:00
#include <string>
#include <vector>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
2017-04-14 19:02:05 +02:00
namespace KWSYS_NAMESPACE {
#if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
// On Windows and apple, no difference between lower and upper case
2017-04-14 19:02:05 +02:00
#define KWSYS_GLOB_CASE_INDEPENDENT
#endif
#if defined(_WIN32) || defined(__CYGWIN__)
// Handle network paths
2017-04-14 19:02:05 +02:00
#define KWSYS_GLOB_SUPPORT_NETWORK_PATHS
#endif
class GlobInternals
{
public:
2015-11-17 17:22:37 +01:00
std::vector<std::string> Files;
std::vector<kwsys::RegularExpression> Expressions;
};
Glob::Glob()
{
this->Internals = new GlobInternals;
this->Recurse = false;
this->Relative = "";
this->RecurseThroughSymlinks = true;
2017-04-14 19:02:05 +02:00
// RecurseThroughSymlinks is true by default for backwards compatibility,
// not because it's a good idea...
this->FollowedSymlinkCount = 0;
2015-08-17 11:37:30 +02:00
// Keep separate variables for directory listing for back compatibility
this->ListDirs = true;
this->RecurseListDirs = false;
}
Glob::~Glob()
{
delete this->Internals;
}
2015-11-17 17:22:37 +01:00
std::vector<std::string>& Glob::GetFiles()
{
return this->Internals->Files;
}
2015-11-17 17:22:37 +01:00
std::string Glob::PatternToRegex(const std::string& pattern,
2017-04-14 19:02:05 +02:00
bool require_whole_string, bool preserve_case)
{
// Incrementally build the regular expression from the pattern.
2017-04-14 19:02:05 +02:00
std::string regex = require_whole_string ? "^" : "";
2015-11-17 17:22:37 +01:00
std::string::const_iterator pattern_first = pattern.begin();
std::string::const_iterator pattern_last = pattern.end();
2017-04-14 19:02:05 +02:00
for (std::string::const_iterator i = pattern_first; i != pattern_last; ++i) {
int c = *i;
2017-04-14 19:02:05 +02:00
if (c == '*') {
// A '*' (not between brackets) matches any string.
// We modify this to not match slashes since the orignal glob
// pattern documentation was meant for matching file name
// components separated by slashes.
regex += "[^/]*";
2017-04-14 19:02:05 +02:00
} else if (c == '?') {
// A '?' (not between brackets) matches any single character.
// We modify this to not match slashes since the orignal glob
// pattern documentation was meant for matching file name
// components separated by slashes.
regex += "[^/]";
2017-04-14 19:02:05 +02:00
} else if (c == '[') {
// Parse out the bracket expression. It begins just after the
// opening character.
2017-04-14 19:02:05 +02:00
std::string::const_iterator bracket_first = i + 1;
2015-11-17 17:22:37 +01:00
std::string::const_iterator bracket_last = bracket_first;
// The first character may be complementation '!' or '^'.
2017-04-14 19:02:05 +02:00
if (bracket_last != pattern_last &&
(*bracket_last == '!' || *bracket_last == '^')) {
++bracket_last;
2017-04-14 19:02:05 +02:00
}
// If the next character is a ']' it is included in the brackets
// because the bracket string may not be empty.
2017-04-14 19:02:05 +02:00
if (bracket_last != pattern_last && *bracket_last == ']') {
++bracket_last;
2017-04-14 19:02:05 +02:00
}
// Search for the closing ']'.
2017-04-14 19:02:05 +02:00
while (bracket_last != pattern_last && *bracket_last != ']') {
++bracket_last;
2017-04-14 19:02:05 +02:00
}
// Check whether we have a complete bracket string.
2017-04-14 19:02:05 +02:00
if (bracket_last == pattern_last) {
// The bracket string did not end, so it was opened simply by
// a '[' that is supposed to be matched literally.
regex += "\\[";
2017-04-14 19:02:05 +02:00
} else {
// Convert the bracket string to its regex equivalent.
2015-11-17 17:22:37 +01:00
std::string::const_iterator k = bracket_first;
// Open the regex block.
regex += "[";
// A regex range complement uses '^' instead of '!'.
2017-04-14 19:02:05 +02:00
if (k != bracket_last && *k == '!') {
regex += "^";
++k;
2017-04-14 19:02:05 +02:00
}
// Convert the remaining characters.
2017-04-14 19:02:05 +02:00
for (; k != bracket_last; ++k) {
// Backslashes must be escaped.
2017-04-14 19:02:05 +02:00
if (*k == '\\') {
regex += "\\";
2017-04-14 19:02:05 +02:00
}
// Store this character.
regex += *k;
2017-04-14 19:02:05 +02:00
}
// Close the regex block.
regex += "]";
// Jump to the end of the bracket string.
i = bracket_last;
}
2017-04-14 19:02:05 +02:00
} else {
// A single character matches itself.
int ch = c;
2017-04-14 19:02:05 +02:00
if (!(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') ||
('0' <= ch && ch <= '9'))) {
// Escape the non-alphanumeric character.
regex += "\\";
2017-04-14 19:02:05 +02:00
}
#if defined(KWSYS_GLOB_CASE_INDEPENDENT)
2017-04-14 19:02:05 +02:00
else {
// On case-insensitive systems file names are converted to lower
// case before matching.
2017-04-14 19:02:05 +02:00
if (!preserve_case) {
2009-10-04 10:30:41 +03:00
ch = tolower(ch);
}
2017-04-14 19:02:05 +02:00
}
#endif
2009-10-04 10:30:41 +03:00
(void)preserve_case;
// Store the character.
regex.append(1, static_cast<char>(ch));
}
2017-04-14 19:02:05 +02:00
}
2017-04-14 19:02:05 +02:00
if (require_whole_string) {
regex += "$";
2017-04-14 19:02:05 +02:00
}
return regex;
}
2015-11-17 17:22:37 +01:00
bool Glob::RecurseDirectory(std::string::size_type start,
2017-04-14 19:02:05 +02:00
const std::string& dir, GlobMessages* messages)
{
kwsys::Directory d;
2017-04-14 19:02:05 +02:00
if (!d.Load(dir)) {
2015-08-17 11:37:30 +02:00
return true;
2017-04-14 19:02:05 +02:00
}
unsigned long cc;
2015-11-17 17:22:37 +01:00
std::string realname;
std::string fname;
2017-04-14 19:02:05 +02:00
for (cc = 0; cc < d.GetNumberOfFiles(); cc++) {
fname = d.GetFile(cc);
2017-04-14 19:02:05 +02:00
if (fname == "." || fname == "..") {
continue;
2017-04-14 19:02:05 +02:00
}
2017-04-14 19:02:05 +02:00
if (start == 0) {
realname = dir + fname;
2017-04-14 19:02:05 +02:00
} else {
realname = dir + "/" + fname;
2017-04-14 19:02:05 +02:00
}
2017-04-14 19:02:05 +02:00
#if defined(KWSYS_GLOB_CASE_INDEPENDENT)
// On Windows and apple, no difference between lower and upper case
fname = kwsys::SystemTools::LowerCase(fname);
#endif
2015-04-27 22:25:09 +02:00
bool isDir = kwsys::SystemTools::FileIsDirectory(realname);
bool isSymLink = kwsys::SystemTools::FileIsSymlink(realname);
2012-02-18 12:40:36 +02:00
2017-04-14 19:02:05 +02:00
if (isDir && (!isSymLink || this->RecurseThroughSymlinks)) {
if (isSymLink) {
2012-02-18 12:40:36 +02:00
++this->FollowedSymlinkCount;
2015-11-17 17:22:37 +01:00
std::string realPathErrorMessage;
2017-04-14 19:02:05 +02:00
std::string canonicalPath(
SystemTools::GetRealPath(dir, &realPathErrorMessage));
2015-08-17 11:37:30 +02:00
2017-04-14 19:02:05 +02:00
if (!realPathErrorMessage.empty()) {
if (messages) {
2015-08-17 11:37:30 +02:00
messages->push_back(Message(
2017-04-14 19:02:05 +02:00
Glob::error, "Canonical path generation from path '" + dir +
"' failed! Reason: '" + realPathErrorMessage + "'"));
2015-08-17 11:37:30 +02:00
}
2017-04-14 19:02:05 +02:00
return false;
}
2015-08-17 11:37:30 +02:00
2017-04-14 19:02:05 +02:00
if (std::find(this->VisitedSymlinks.begin(),
this->VisitedSymlinks.end(),
canonicalPath) == this->VisitedSymlinks.end()) {
if (this->RecurseListDirs) {
2015-08-17 11:37:30 +02:00
// symlinks are treated as directories
this->AddFile(this->Internals->Files, realname);
2017-04-14 19:02:05 +02:00
}
2015-08-17 11:37:30 +02:00
this->VisitedSymlinks.push_back(canonicalPath);
2017-04-14 19:02:05 +02:00
if (!this->RecurseDirectory(start + 1, realname, messages)) {
2015-08-17 11:37:30 +02:00
this->VisitedSymlinks.pop_back();
return false;
}
2017-04-14 19:02:05 +02:00
this->VisitedSymlinks.pop_back();
}
2015-08-17 11:37:30 +02:00
// else we have already visited this symlink - prevent cyclic recursion
2017-04-14 19:02:05 +02:00
else if (messages) {
2015-11-17 17:22:37 +01:00
std::string message;
2017-04-14 19:02:05 +02:00
for (std::vector<std::string>::const_iterator pathIt =
std::find(this->VisitedSymlinks.begin(),
this->VisitedSymlinks.end(), canonicalPath);
pathIt != this->VisitedSymlinks.end(); ++pathIt) {
2015-08-17 11:37:30 +02:00
message += *pathIt + "\n";
2017-04-14 19:02:05 +02:00
}
2015-08-17 11:37:30 +02:00
message += canonicalPath + "/" + fname;
messages->push_back(Message(Glob::cyclicRecursion, message));
}
2017-04-14 19:02:05 +02:00
} else {
if (this->RecurseListDirs) {
2015-08-17 11:37:30 +02:00
this->AddFile(this->Internals->Files, realname);
2017-04-14 19:02:05 +02:00
}
if (!this->RecurseDirectory(start + 1, realname, messages)) {
2015-08-17 11:37:30 +02:00
return false;
}
}
2017-04-14 19:02:05 +02:00
} else {
if (!this->Internals->Expressions.empty() &&
this->Internals->Expressions.rbegin()->find(fname)) {
2015-04-27 22:25:09 +02:00
this->AddFile(this->Internals->Files, realname);
}
}
2017-04-14 19:02:05 +02:00
}
2015-08-17 11:37:30 +02:00
return true;
}
2015-11-17 17:22:37 +01:00
void Glob::ProcessDirectory(std::string::size_type start,
2017-04-14 19:02:05 +02:00
const std::string& dir, GlobMessages* messages)
{
2017-04-14 19:02:05 +02:00
// std::cout << "ProcessDirectory: " << dir << std::endl;
bool last = (start == this->Internals->Expressions.size() - 1);
if (last && this->Recurse) {
2015-08-17 11:37:30 +02:00
this->RecurseDirectory(start, dir, messages);
return;
2017-04-14 19:02:05 +02:00
}
2017-04-14 19:02:05 +02:00
if (start >= this->Internals->Expressions.size()) {
return;
2017-04-14 19:02:05 +02:00
}
kwsys::Directory d;
2017-04-14 19:02:05 +02:00
if (!d.Load(dir)) {
return;
2017-04-14 19:02:05 +02:00
}
unsigned long cc;
2015-11-17 17:22:37 +01:00
std::string realname;
std::string fname;
2017-04-14 19:02:05 +02:00
for (cc = 0; cc < d.GetNumberOfFiles(); cc++) {
fname = d.GetFile(cc);
2017-04-14 19:02:05 +02:00
if (fname == "." || fname == "..") {
continue;
2017-04-14 19:02:05 +02:00
}
2017-04-14 19:02:05 +02:00
if (start == 0) {
realname = dir + fname;
2017-04-14 19:02:05 +02:00
} else {
realname = dir + "/" + fname;
2017-04-14 19:02:05 +02:00
}
#if defined(KWSYS_GLOB_CASE_INDEPENDENT)
// On case-insensitive file systems convert to lower case for matching.
fname = kwsys::SystemTools::LowerCase(fname);
#endif
2017-04-14 19:02:05 +02:00
// std::cout << "Look at file: " << fname << std::endl;
// std::cout << "Match: "
2015-11-17 17:22:37 +01:00
// << this->Internals->TextExpressions[start].c_str() << std::endl;
2017-04-14 19:02:05 +02:00
// std::cout << "Real name: " << realname << std::endl;
2017-04-14 19:02:05 +02:00
if ((!last && !kwsys::SystemTools::FileIsDirectory(realname)) ||
(!this->ListDirs && last &&
kwsys::SystemTools::FileIsDirectory(realname))) {
continue;
2017-04-14 19:02:05 +02:00
}
2017-04-14 19:02:05 +02:00
if (this->Internals->Expressions[start].find(fname)) {
if (last) {
2015-04-27 22:25:09 +02:00
this->AddFile(this->Internals->Files, realname);
2017-04-14 19:02:05 +02:00
} else {
this->ProcessDirectory(start + 1, realname, messages);
}
}
2017-04-14 19:02:05 +02:00
}
}
2015-11-17 17:22:37 +01:00
bool Glob::FindFiles(const std::string& inexpr, GlobMessages* messages)
{
2015-11-17 17:22:37 +01:00
std::string cexpr;
std::string::size_type cc;
std::string expr = inexpr;
this->Internals->Expressions.clear();
this->Internals->Files.clear();
2017-04-14 19:02:05 +02:00
if (!kwsys::SystemTools::FileIsFullPath(expr)) {
expr = kwsys::SystemTools::GetCurrentWorkingDirectory();
expr += "/" + inexpr;
2017-04-14 19:02:05 +02:00
}
2015-11-17 17:22:37 +01:00
std::string fexpr = expr;
2015-11-17 17:22:37 +01:00
std::string::size_type skip = 0;
std::string::size_type last_slash = 0;
2017-04-14 19:02:05 +02:00
for (cc = 0; cc < expr.size(); cc++) {
if (cc > 0 && expr[cc] == '/' && expr[cc - 1] != '\\') {
2009-10-04 10:30:41 +03:00
last_slash = cc;
2017-04-14 19:02:05 +02:00
}
if (cc > 0 && (expr[cc] == '[' || expr[cc] == '?' || expr[cc] == '*') &&
expr[cc - 1] != '\\') {
break;
}
2017-04-14 19:02:05 +02:00
}
if (last_slash > 0) {
// std::cout << "I can skip: " << fexpr.substr(0, last_slash)
2015-11-17 17:22:37 +01:00
// << std::endl;
skip = last_slash;
2017-04-14 19:02:05 +02:00
}
if (skip == 0) {
#if defined(KWSYS_GLOB_SUPPORT_NETWORK_PATHS)
// Handle network paths
2017-04-14 19:02:05 +02:00
if (expr[0] == '/' && expr[1] == '/') {
int cnt = 0;
2017-04-14 19:02:05 +02:00
for (cc = 2; cc < expr.size(); cc++) {
if (expr[cc] == '/') {
cnt++;
if (cnt == 2) {
break;
}
}
}
2017-04-14 19:02:05 +02:00
skip = int(cc + 1);
} else
#endif
// Handle drive letters on Windows
2017-04-14 19:02:05 +02:00
if (expr[1] == ':' && expr[0] != '/') {
skip = 2;
}
2017-04-14 19:02:05 +02:00
}
2017-04-14 19:02:05 +02:00
if (skip > 0) {
expr = expr.substr(skip);
2017-04-14 19:02:05 +02:00
}
cexpr = "";
2017-04-14 19:02:05 +02:00
for (cc = 0; cc < expr.size(); cc++) {
int ch = expr[cc];
2017-04-14 19:02:05 +02:00
if (ch == '/') {
if (!cexpr.empty()) {
2015-04-27 22:25:09 +02:00
this->AddExpression(cexpr);
}
2017-04-14 19:02:05 +02:00
cexpr = "";
} else {
cexpr.append(1, static_cast<char>(ch));
}
2017-04-14 19:02:05 +02:00
}
if (!cexpr.empty()) {
2015-04-27 22:25:09 +02:00
this->AddExpression(cexpr);
2017-04-14 19:02:05 +02:00
}
// Handle network paths
2017-04-14 19:02:05 +02:00
if (skip > 0) {
2015-08-17 11:37:30 +02:00
this->ProcessDirectory(0, fexpr.substr(0, skip) + "/", messages);
2017-04-14 19:02:05 +02:00
} else {
2015-08-17 11:37:30 +02:00
this->ProcessDirectory(0, "/", messages);
2017-04-14 19:02:05 +02:00
}
return true;
}
2015-11-17 17:22:37 +01:00
void Glob::AddExpression(const std::string& expr)
{
this->Internals->Expressions.push_back(
2017-04-14 19:02:05 +02:00
kwsys::RegularExpression(this->PatternToRegex(expr)));
}
void Glob::SetRelative(const char* dir)
{
2017-04-14 19:02:05 +02:00
if (!dir) {
this->Relative = "";
return;
2017-04-14 19:02:05 +02:00
}
this->Relative = dir;
}
const char* Glob::GetRelative()
{
2017-04-14 19:02:05 +02:00
if (this->Relative.empty()) {
return 0;
2017-04-14 19:02:05 +02:00
}
return this->Relative.c_str();
}
2015-11-17 17:22:37 +01:00
void Glob::AddFile(std::vector<std::string>& files, const std::string& file)
{
2017-04-14 19:02:05 +02:00
if (!this->Relative.empty()) {
2015-04-27 22:25:09 +02:00
files.push_back(kwsys::SystemTools::RelativePath(this->Relative, file));
2017-04-14 19:02:05 +02:00
} else {
files.push_back(file);
2017-04-14 19:02:05 +02:00
}
}
} // namespace KWSYS_NAMESPACE