| 1 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */ |
| 3 |
#ifndef @KWSYS_NAMESPACE@_Glob_hxx |
| 4 |
#define @KWSYS_NAMESPACE@_Glob_hxx |
| 5 |
|
| 6 |
#include <@KWSYS_NAMESPACE@/Configure.h> |
| 7 |
#include <@KWSYS_NAMESPACE@/Configure.hxx> |
| 8 |
|
| 9 |
#include <string> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
namespace @KWSYS_NAMESPACE@ { |
| 13 |
|
| 14 |
class GlobInternals; |
| 15 |
|
| 16 |
/** \class Glob |
| 17 |
* \brief Portable globbing searches. |
| 18 |
* |
| 19 |
* Globbing expressions are much simpler than regular |
| 20 |
* expressions. This class will search for files using |
| 21 |
* globbing expressions. |
| 22 |
* |
| 23 |
* Finds all files that match a given globbing expression. |
| 24 |
*/ |
| 25 |
class @KWSYS_NAMESPACE@_EXPORT Glob |
| 26 |
{ |
| 27 |
public: |
| 28 |
enum MessageType |
| 29 |
{ |
| 30 |
error, |
| 31 |
warning, |
| 32 |
cyclicRecursion |
| 33 |
}; |
| 34 |
|
| 35 |
struct Message |
| 36 |
{ |
| 37 |
MessageType type; |
| 38 |
std::string content; |
| 39 |
|
| 40 |
Message(MessageType t, const std::string& c) |
| 41 |
: type(t) |
| 42 |
, content(c) |
| 43 |
{ |
| 44 |
} |
| 45 |
~Message() = default; |
| 46 |
Message(const Message& msg) = default; |
| 47 |
Message& operator=(Message const& msg) = default; |
| 48 |
}; |
| 49 |
|
| 50 |
typedef std::vector<Message> GlobMessages; |
| 51 |
typedef std::vector<Message>::iterator GlobMessagesIterator; |
| 52 |
|
| 53 |
public: |
| 54 |
Glob(); |
| 55 |
~Glob(); |
| 56 |
|
| 57 |
Glob(const Glob&) = delete; |
| 58 |
void operator=(const Glob&) = delete; |
| 59 |
|
| 60 |
//! Find all files that match the pattern. |
| 61 |
bool FindFiles(const std::string& inexpr, GlobMessages* messages = nullptr); |
| 62 |
|
| 63 |
//! Return the list of files that matched. |
| 64 |
std::vector<std::string>& GetFiles(); |
| 65 |
|
| 66 |
//! Set recurse to true to match subdirectories. |
| 67 |
void RecurseOn() { this->SetRecurse(true); } |
| 68 |
void RecurseOff() { this->SetRecurse(false); } |
| 69 |
void SetRecurse(bool i) { this->Recurse = i; } |
| 70 |
bool GetRecurse() { return this->Recurse; } |
| 71 |
|
| 72 |
//! Set recurse through symlinks to true if recursion should traverse the |
| 73 |
// linked-to directories |
| 74 |
void RecurseThroughSymlinksOn() { this->SetRecurseThroughSymlinks(true); } |
| 75 |
void RecurseThroughSymlinksOff() { this->SetRecurseThroughSymlinks(false); } |
| 76 |
void SetRecurseThroughSymlinks(bool i) { this->RecurseThroughSymlinks = i; } |
| 77 |
bool GetRecurseThroughSymlinks() { return this->RecurseThroughSymlinks; } |
| 78 |
|
| 79 |
//! Get the number of symlinks followed through recursion |
| 80 |
unsigned int GetFollowedSymlinkCount() { return this->FollowedSymlinkCount; } |
| 81 |
|
| 82 |
//! Set relative to true to only show relative path to files. |
| 83 |
void SetRelative(const char* dir); |
| 84 |
const char* GetRelative(); |
| 85 |
|
| 86 |
/** Convert the given globbing pattern to a regular expression. |
| 87 |
There is no way to quote meta-characters. The |
| 88 |
require_whole_string argument specifies whether the regex is |
| 89 |
automatically surrounded by "^" and "$" to match the whole |
| 90 |
string. This is on by default because patterns always match |
| 91 |
whole strings, but may be disabled to support concatenating |
| 92 |
expressions more easily (regex1|regex2|etc). */ |
| 93 |
static std::string PatternToRegex(const std::string& pattern, |
| 94 |
bool require_whole_string = true, |
| 95 |
bool preserve_case = false); |
| 96 |
|
| 97 |
/** Getters and setters for enabling and disabling directory |
| 98 |
listing in recursive and non recursive globbing mode. |
| 99 |
If listing is enabled in recursive mode it also lists |
| 100 |
directory symbolic links even if follow symlinks is enabled. */ |
| 101 |
void SetListDirs(bool list) { this->ListDirs = list; } |
| 102 |
bool GetListDirs() const { return this->ListDirs; } |
| 103 |
void SetRecurseListDirs(bool list) { this->RecurseListDirs = list; } |
| 104 |
bool GetRecurseListDirs() const { return this->RecurseListDirs; } |
| 105 |
|
| 106 |
protected: |
| 107 |
//! Process directory |
| 108 |
void ProcessDirectory(std::string::size_type start, const std::string& dir, |
| 109 |
GlobMessages* messages); |
| 110 |
|
| 111 |
//! Process last directory, but only when recurse flags is on. That is |
| 112 |
// effectively like saying: /path/to/file/**/file |
| 113 |
bool RecurseDirectory(std::string::size_type start, const std::string& dir, |
| 114 |
GlobMessages* messages); |
| 115 |
|
| 116 |
//! Add regular expression |
| 117 |
void AddExpression(const std::string& expr); |
| 118 |
|
| 119 |
//! Add a file to the list |
| 120 |
void AddFile(std::vector<std::string>& files, const std::string& file); |
| 121 |
|
| 122 |
GlobInternals* Internals; |
| 123 |
bool Recurse; |
| 124 |
std::string Relative; |
| 125 |
bool RecurseThroughSymlinks; |
| 126 |
unsigned int FollowedSymlinkCount; |
| 127 |
std::vector<std::string> VisitedSymlinks; |
| 128 |
bool ListDirs; |
| 129 |
bool RecurseListDirs; |
| 130 |
}; |
| 131 |
|
| 132 |
} // namespace @KWSYS_NAMESPACE@ |
| 133 |
|
| 134 |
#endif |