/src/CMake/build-dir/Source/cmsys/Glob.hxx
Line | Count | Source |
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 cmsys_Glob_hxx |
4 | | #define cmsys_Glob_hxx |
5 | | |
6 | | #include <cmsys/Configure.h> |
7 | | #include <cmsys/Configure.hxx> |
8 | | |
9 | | #include <string> |
10 | | #include <vector> |
11 | | |
12 | | namespace cmsys { |
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 cmsys_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, std::string const& c) |
41 | 0 | : type(t) |
42 | 0 | , content(c) |
43 | 0 | { |
44 | 0 | } |
45 | 0 | ~Message() = default; |
46 | 0 | Message(Message const& 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(Glob const&) = delete; |
58 | | void operator=(Glob const&) = delete; |
59 | | |
60 | | //! Find all files that match the pattern. |
61 | | bool FindFiles(std::string const& 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 | 0 | void RecurseOn() { this->SetRecurse(true); } |
68 | 0 | void RecurseOff() { this->SetRecurse(false); } |
69 | 1.81k | void SetRecurse(bool i) { this->Recurse = i; } |
70 | 0 | bool GetRecurse() { return this->Recurse; } |
71 | | |
72 | | //! Set recurse through symlinks to true if recursion should traverse the |
73 | | // linked-to directories |
74 | 0 | void RecurseThroughSymlinksOn() { this->SetRecurseThroughSymlinks(true); } |
75 | 0 | void RecurseThroughSymlinksOff() { this->SetRecurseThroughSymlinks(false); } |
76 | 0 | void SetRecurseThroughSymlinks(bool i) { this->RecurseThroughSymlinks = i; } |
77 | 0 | bool GetRecurseThroughSymlinks() { return this->RecurseThroughSymlinks; } |
78 | | |
79 | | //! Get the number of symlinks followed through recursion |
80 | 0 | unsigned int GetFollowedSymlinkCount() { return this->FollowedSymlinkCount; } |
81 | | |
82 | | //! Set relative to true to only show relative path to files. |
83 | | void SetRelative(char const* dir); |
84 | | char const* 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(std::string const& 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 | 0 | void SetListDirs(bool list) { this->ListDirs = list; } |
102 | 0 | bool GetListDirs() const { return this->ListDirs; } |
103 | 0 | void SetRecurseListDirs(bool list) { this->RecurseListDirs = list; } |
104 | 0 | bool GetRecurseListDirs() const { return this->RecurseListDirs; } |
105 | | |
106 | | protected: |
107 | | //! Process directory |
108 | | void ProcessDirectory(std::string::size_type start, std::string const& 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, std::string const& dir, |
114 | | GlobMessages* messages); |
115 | | |
116 | | //! Add regular expression |
117 | | void AddExpression(std::string const& expr); |
118 | | |
119 | | //! Add a file to the list |
120 | | void AddFile(std::vector<std::string>& files, std::string const& 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 cmsys |
133 | | |
134 | | #endif |