123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- #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)
- #if 0
- #include "Configure.hxx.in"
- #include "Directory.hxx.in"
- #include "Glob.hxx.in"
- #include "RegularExpression.hxx.in"
- #include "SystemTools.hxx.in"
- #endif
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- namespace KWSYS_NAMESPACE {
- #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
- #define KWSYS_GLOB_CASE_INDEPENDENT
- #endif
- #if defined(_WIN32) || defined(__CYGWIN__)
- #define KWSYS_GLOB_SUPPORT_NETWORK_PATHS
- #endif
- class GlobInternals
- {
- public:
- std::vector<std::string> Files;
- std::vector<kwsys::RegularExpression> Expressions;
- };
- Glob::Glob()
- {
- this->Internals = new GlobInternals;
- this->Recurse = false;
- this->Relative = "";
- this->RecurseThroughSymlinks = true;
-
-
- this->FollowedSymlinkCount = 0;
-
- this->ListDirs = true;
- this->RecurseListDirs = false;
- }
- Glob::~Glob()
- {
- delete this->Internals;
- }
- std::vector<std::string>& Glob::GetFiles()
- {
- return this->Internals->Files;
- }
- std::string Glob::PatternToRegex(const std::string& pattern,
- bool require_whole_string, bool preserve_case)
- {
-
- std::string regex = require_whole_string ? "^" : "";
- std::string::const_iterator pattern_first = pattern.begin();
- std::string::const_iterator pattern_last = pattern.end();
- for (std::string::const_iterator i = pattern_first; i != pattern_last; ++i) {
- int c = *i;
- if (c == '*') {
-
-
-
-
- regex += "[^/]*";
- } else if (c == '?') {
-
-
-
-
- regex += "[^/]";
- } else if (c == '[') {
-
-
- std::string::const_iterator bracket_first = i + 1;
- std::string::const_iterator bracket_last = bracket_first;
-
- if (bracket_last != pattern_last &&
- (*bracket_last == '!' || *bracket_last == '^')) {
- ++bracket_last;
- }
-
-
- if (bracket_last != pattern_last && *bracket_last == ']') {
- ++bracket_last;
- }
-
- while (bracket_last != pattern_last && *bracket_last != ']') {
- ++bracket_last;
- }
-
- if (bracket_last == pattern_last) {
-
-
- regex += "\\[";
- } else {
-
- std::string::const_iterator k = bracket_first;
-
- regex += "[";
-
- if (k != bracket_last && *k == '!') {
- regex += "^";
- ++k;
- }
-
- for (; k != bracket_last; ++k) {
-
- if (*k == '\\') {
- regex += "\\";
- }
-
- regex += *k;
- }
-
- regex += "]";
-
- i = bracket_last;
- }
- } else {
-
- int ch = c;
- if (!(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') ||
- ('0' <= ch && ch <= '9'))) {
-
- regex += "\\";
- }
- #if defined(KWSYS_GLOB_CASE_INDEPENDENT)
- else {
-
-
- if (!preserve_case) {
- ch = tolower(ch);
- }
- }
- #endif
- (void)preserve_case;
-
- regex.append(1, static_cast<char>(ch));
- }
- }
- if (require_whole_string) {
- regex += "$";
- }
- return regex;
- }
- bool Glob::RecurseDirectory(std::string::size_type start,
- const std::string& dir, GlobMessages* messages)
- {
- kwsys::Directory d;
- if (!d.Load(dir)) {
- return true;
- }
- unsigned long cc;
- std::string realname;
- std::string fname;
- for (cc = 0; cc < d.GetNumberOfFiles(); cc++) {
- fname = d.GetFile(cc);
- if (fname == "." || fname == "..") {
- continue;
- }
- if (start == 0) {
- realname = dir + fname;
- } else {
- realname = dir + "/" + fname;
- }
- #if defined(KWSYS_GLOB_CASE_INDEPENDENT)
-
- fname = kwsys::SystemTools::LowerCase(fname);
- #endif
- bool isDir = kwsys::SystemTools::FileIsDirectory(realname);
- bool isSymLink = kwsys::SystemTools::FileIsSymlink(realname);
- if (isDir && (!isSymLink || this->RecurseThroughSymlinks)) {
- if (isSymLink) {
- ++this->FollowedSymlinkCount;
- std::string realPathErrorMessage;
- std::string canonicalPath(
- SystemTools::GetRealPath(dir, &realPathErrorMessage));
- if (!realPathErrorMessage.empty()) {
- if (messages) {
- messages->push_back(Message(
- Glob::error, "Canonical path generation from path '" + dir +
- "' failed! Reason: '" + realPathErrorMessage + "'"));
- }
- return false;
- }
- if (std::find(this->VisitedSymlinks.begin(),
- this->VisitedSymlinks.end(),
- canonicalPath) == this->VisitedSymlinks.end()) {
- if (this->RecurseListDirs) {
-
- this->AddFile(this->Internals->Files, realname);
- }
- this->VisitedSymlinks.push_back(canonicalPath);
- if (!this->RecurseDirectory(start + 1, realname, messages)) {
- this->VisitedSymlinks.pop_back();
- return false;
- }
- this->VisitedSymlinks.pop_back();
- }
-
- else if (messages) {
- std::string message;
- for (std::vector<std::string>::const_iterator pathIt =
- std::find(this->VisitedSymlinks.begin(),
- this->VisitedSymlinks.end(), canonicalPath);
- pathIt != this->VisitedSymlinks.end(); ++pathIt) {
- message += *pathIt + "\n";
- }
- message += canonicalPath + "/" + fname;
- messages->push_back(Message(Glob::cyclicRecursion, message));
- }
- } else {
- if (this->RecurseListDirs) {
- this->AddFile(this->Internals->Files, realname);
- }
- if (!this->RecurseDirectory(start + 1, realname, messages)) {
- return false;
- }
- }
- } else {
- if (!this->Internals->Expressions.empty() &&
- this->Internals->Expressions.rbegin()->find(fname)) {
- this->AddFile(this->Internals->Files, realname);
- }
- }
- }
- return true;
- }
- void Glob::ProcessDirectory(std::string::size_type start,
- const std::string& dir, GlobMessages* messages)
- {
-
- bool last = (start == this->Internals->Expressions.size() - 1);
- if (last && this->Recurse) {
- this->RecurseDirectory(start, dir, messages);
- return;
- }
- if (start >= this->Internals->Expressions.size()) {
- return;
- }
- kwsys::Directory d;
- if (!d.Load(dir)) {
- return;
- }
- unsigned long cc;
- std::string realname;
- std::string fname;
- for (cc = 0; cc < d.GetNumberOfFiles(); cc++) {
- fname = d.GetFile(cc);
- if (fname == "." || fname == "..") {
- continue;
- }
- if (start == 0) {
- realname = dir + fname;
- } else {
- realname = dir + "/" + fname;
- }
- #if defined(KWSYS_GLOB_CASE_INDEPENDENT)
-
- fname = kwsys::SystemTools::LowerCase(fname);
- #endif
-
-
-
-
- if ((!last && !kwsys::SystemTools::FileIsDirectory(realname)) ||
- (!this->ListDirs && last &&
- kwsys::SystemTools::FileIsDirectory(realname))) {
- continue;
- }
- if (this->Internals->Expressions[start].find(fname)) {
- if (last) {
- this->AddFile(this->Internals->Files, realname);
- } else {
- this->ProcessDirectory(start + 1, realname, messages);
- }
- }
- }
- }
- bool Glob::FindFiles(const std::string& inexpr, GlobMessages* messages)
- {
- std::string cexpr;
- std::string::size_type cc;
- std::string expr = inexpr;
- this->Internals->Expressions.clear();
- this->Internals->Files.clear();
- if (!kwsys::SystemTools::FileIsFullPath(expr)) {
- expr = kwsys::SystemTools::GetCurrentWorkingDirectory();
- expr += "/" + inexpr;
- }
- std::string fexpr = expr;
- std::string::size_type skip = 0;
- std::string::size_type 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) {
-
-
- skip = last_slash;
- }
- if (skip == 0) {
- #if defined(KWSYS_GLOB_SUPPORT_NETWORK_PATHS)
-
- if (expr[0] == '/' && expr[1] == '/') {
- int cnt = 0;
- for (cc = 2; cc < expr.size(); cc++) {
- if (expr[cc] == '/') {
- cnt++;
- if (cnt == 2) {
- break;
- }
- }
- }
- skip = int(cc + 1);
- } else
- #endif
-
- if (expr[1] == ':' && expr[0] != '/') {
- skip = 2;
- }
- }
- if (skip > 0) {
- expr = expr.substr(skip);
- }
- cexpr = "";
- for (cc = 0; cc < expr.size(); cc++) {
- int ch = expr[cc];
- if (ch == '/') {
- if (!cexpr.empty()) {
- this->AddExpression(cexpr);
- }
- cexpr = "";
- } else {
- cexpr.append(1, static_cast<char>(ch));
- }
- }
- if (!cexpr.empty()) {
- this->AddExpression(cexpr);
- }
-
- if (skip > 0) {
- this->ProcessDirectory(0, fexpr.substr(0, skip) + "/", messages);
- } else {
- this->ProcessDirectory(0, "/", messages);
- }
- return true;
- }
- void Glob::AddExpression(const std::string& expr)
- {
- this->Internals->Expressions.push_back(
- kwsys::RegularExpression(this->PatternToRegex(expr)));
- }
- void Glob::SetRelative(const char* dir)
- {
- if (!dir) {
- this->Relative = "";
- return;
- }
- this->Relative = dir;
- }
- const char* Glob::GetRelative()
- {
- if (this->Relative.empty()) {
- return KWSYS_NULLPTR;
- }
- return this->Relative.c_str();
- }
- void Glob::AddFile(std::vector<std::string>& files, const std::string& file)
- {
- if (!this->Relative.empty()) {
- files.push_back(kwsys::SystemTools::RelativePath(this->Relative, file));
- } else {
- files.push_back(file);
- }
- }
- }
|