1 | // |
---|
2 | // Glob.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Glob.h#3 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Filesystem |
---|
8 | // Module: Glob |
---|
9 | // |
---|
10 | // Definition of the Glob class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2009, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Glob_INCLUDED |
---|
40 | #define Foundation_Glob_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/TextIterator.h" |
---|
45 | #include <set> |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | |
---|
50 | |
---|
51 | class Path; |
---|
52 | |
---|
53 | |
---|
54 | class Foundation_API Glob |
---|
55 | /// This class implements glob-style pattern matching |
---|
56 | /// as known from Unix shells. |
---|
57 | /// |
---|
58 | /// In the pattern string, '*' matches any sequence of characters, |
---|
59 | /// '?' matches any single character, [SET] matches any single character |
---|
60 | /// in the specified set, [!SET] matches any character not in the |
---|
61 | /// specified set. |
---|
62 | /// |
---|
63 | /// A set is composed of characters or ranges; a range looks like |
---|
64 | /// character hyphen character (as in 0-9 or A-Z). |
---|
65 | /// [0-9a-zA-Z_] is the set of characters allowed in C identifiers. |
---|
66 | /// Any other character in the pattern must be matched exactly. |
---|
67 | /// |
---|
68 | /// To suppress the special syntactic significance of any of '[]*?!-\', |
---|
69 | /// and match the character exactly, precede it with a backslash. |
---|
70 | /// |
---|
71 | /// All strings are assumed to be UTF-8 encoded. |
---|
72 | { |
---|
73 | public: |
---|
74 | enum Options |
---|
75 | /// Flags that modify the matching behavior. |
---|
76 | { |
---|
77 | GLOB_DEFAULT = 0x00, /// default behavior |
---|
78 | GLOB_DOT_SPECIAL = 0x01, /// '*' and '?' do not match '.' at beginning of subject |
---|
79 | GLOB_FOLLOW_SYMLINKS = 0x02, /// follow symbolic links |
---|
80 | GLOB_CASELESS = 0x04, /// ignore case when comparing characters |
---|
81 | GLOB_DIRS_ONLY = 0x80 /// only glob for directories (for internal use only) |
---|
82 | }; |
---|
83 | |
---|
84 | Glob(const std::string& pattern, int options = 0); |
---|
85 | /// Creates the Glob, using the given pattern. The pattern |
---|
86 | /// must not be an empty string. |
---|
87 | /// |
---|
88 | /// If the GLOB_DOT_SPECIAL option is specified, '*' and '?' do |
---|
89 | /// not match '.' at the beginning of a matched subject. This is useful for |
---|
90 | /// making dot-files invisible in good old Unix-style. |
---|
91 | |
---|
92 | ~Glob(); |
---|
93 | /// Destroys the Glob. |
---|
94 | |
---|
95 | bool match(const std::string& subject); |
---|
96 | /// Matches the given subject against the glob pattern. |
---|
97 | /// Returns true if the subject matches the pattern, false |
---|
98 | /// otherwise. |
---|
99 | |
---|
100 | static void glob(const std::string& pathPattern, std::set<std::string>& files, int options = 0); |
---|
101 | /// Creates a set of files that match the given pathPattern. |
---|
102 | /// |
---|
103 | /// The path may be give in either Unix, Windows or VMS syntax and |
---|
104 | /// is automatically expanded by calling Path::expand(). |
---|
105 | /// |
---|
106 | /// The pattern may contain wildcard expressions even in intermediate |
---|
107 | /// directory names (e.g. /usr/include/*/*.h). |
---|
108 | /// |
---|
109 | /// Note that, for obvious reasons, escaping characters in a pattern |
---|
110 | /// with a backslash does not work in Windows-style paths. |
---|
111 | /// |
---|
112 | /// Directories that for whatever reason cannot be traversed are |
---|
113 | /// ignored. |
---|
114 | |
---|
115 | static void glob(const char* pathPattern, std::set<std::string>& files, int options = 0); |
---|
116 | /// Creates a set of files that match the given pathPattern. |
---|
117 | /// |
---|
118 | /// The path may be give in either Unix, Windows or VMS syntax and |
---|
119 | /// is automatically expanded by calling Path::expand(). |
---|
120 | /// |
---|
121 | /// The pattern may contain wildcard expressions even in intermediate |
---|
122 | /// directory names (e.g. /usr/include/*/*.h). |
---|
123 | /// |
---|
124 | /// Note that, for obvious reasons, escaping characters in a pattern |
---|
125 | /// with a backslash does not work in Windows-style paths. |
---|
126 | /// |
---|
127 | /// Directories that for whatever reason cannot be traversed are |
---|
128 | /// ignored. |
---|
129 | |
---|
130 | static void glob(const Path& pathPattern, std::set<std::string>& files, int options = 0); |
---|
131 | /// Creates a set of files that match the given pathPattern. |
---|
132 | /// |
---|
133 | /// The pattern may contain wildcard expressions even in intermediate |
---|
134 | /// directory names (e.g. /usr/include/*/*.h). |
---|
135 | /// |
---|
136 | /// Note that, for obvious reasons, escaping characters in a pattern |
---|
137 | /// with a backslash does not work in Windows-style paths. |
---|
138 | /// |
---|
139 | /// Directories that for whatever reason cannot be traversed are |
---|
140 | /// ignored. |
---|
141 | |
---|
142 | protected: |
---|
143 | bool match(TextIterator& itp, const TextIterator& endp, TextIterator& its, const TextIterator& ends); |
---|
144 | bool matchAfterAsterisk(TextIterator itp, const TextIterator& endp, TextIterator its, const TextIterator& ends); |
---|
145 | bool matchSet(TextIterator& itp, const TextIterator& endp, int c); |
---|
146 | static void collect(const Path& pathPattern, const Path& base, const Path& current, const std::string& pattern, std::set<std::string>& files, int options); |
---|
147 | static bool isDirectory(const Path& path, bool followSymlink); |
---|
148 | |
---|
149 | private: |
---|
150 | std::string _pattern; |
---|
151 | int _options; |
---|
152 | |
---|
153 | Glob(); |
---|
154 | Glob(const Glob&); |
---|
155 | Glob& operator = (const Glob&); |
---|
156 | }; |
---|
157 | |
---|
158 | |
---|
159 | } // namespace Poco |
---|
160 | |
---|
161 | |
---|
162 | #endif // Foundation_Glob_INCLUDED |
---|