1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 1998-2002 |
---|
4 | * John Maddock |
---|
5 | * |
---|
6 | * Use, modification and distribution are subject to the |
---|
7 | * Boost Software License, Version 1.0. (See accompanying file |
---|
8 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
9 | * |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * LOCATION: see http://www.boost.org for most recent version. |
---|
14 | * FILE cregex.cpp |
---|
15 | * VERSION see <boost/version.hpp> |
---|
16 | * DESCRIPTION: Declares POSIX API functions |
---|
17 | * + boost::RegEx high level wrapper. |
---|
18 | */ |
---|
19 | |
---|
20 | #ifndef BOOST_RE_CREGEX_HPP_INCLUDED |
---|
21 | #define BOOST_RE_CREGEX_HPP_INCLUDED |
---|
22 | |
---|
23 | #ifndef BOOST_REGEX_CONFIG_HPP |
---|
24 | #include <boost/regex/config.hpp> |
---|
25 | #endif |
---|
26 | #include <boost/regex/v4/match_flags.hpp> |
---|
27 | #include <boost/regex/v4/error_type.hpp> |
---|
28 | |
---|
29 | #ifdef __cplusplus |
---|
30 | #include <cstddef> |
---|
31 | #else |
---|
32 | #include <stddef.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | #ifdef BOOST_HAS_ABI_HEADERS |
---|
36 | # include BOOST_ABI_PREFIX |
---|
37 | #endif |
---|
38 | |
---|
39 | /* include these defs only for POSIX compatablity */ |
---|
40 | #ifdef __cplusplus |
---|
41 | namespace boost{ |
---|
42 | extern "C" { |
---|
43 | #endif |
---|
44 | |
---|
45 | #if defined(__cplusplus) && !defined(BOOST_NO_STDC_NAMESPACE) |
---|
46 | typedef std::ptrdiff_t regoff_t; |
---|
47 | typedef std::size_t regsize_t; |
---|
48 | #else |
---|
49 | typedef ptrdiff_t regoff_t; |
---|
50 | typedef size_t regsize_t; |
---|
51 | #endif |
---|
52 | |
---|
53 | typedef struct |
---|
54 | { |
---|
55 | unsigned int re_magic; |
---|
56 | #ifdef __cplusplus |
---|
57 | std::size_t re_nsub; /* number of parenthesized subexpressions */ |
---|
58 | #else |
---|
59 | size_t re_nsub; |
---|
60 | #endif |
---|
61 | const char* re_endp; /* end pointer for REG_PEND */ |
---|
62 | void* guts; /* none of your business :-) */ |
---|
63 | match_flag_type eflags; /* none of your business :-) */ |
---|
64 | } regex_tA; |
---|
65 | |
---|
66 | #ifndef BOOST_NO_WREGEX |
---|
67 | typedef struct |
---|
68 | { |
---|
69 | unsigned int re_magic; |
---|
70 | #ifdef __cplusplus |
---|
71 | std::size_t re_nsub; /* number of parenthesized subexpressions */ |
---|
72 | #else |
---|
73 | size_t re_nsub; |
---|
74 | #endif |
---|
75 | const wchar_t* re_endp; /* end pointer for REG_PEND */ |
---|
76 | void* guts; /* none of your business :-) */ |
---|
77 | match_flag_type eflags; /* none of your business :-) */ |
---|
78 | } regex_tW; |
---|
79 | #endif |
---|
80 | |
---|
81 | typedef struct |
---|
82 | { |
---|
83 | regoff_t rm_so; /* start of match */ |
---|
84 | regoff_t rm_eo; /* end of match */ |
---|
85 | } regmatch_t; |
---|
86 | |
---|
87 | /* regcomp() flags */ |
---|
88 | typedef enum{ |
---|
89 | REG_BASIC = 0000, |
---|
90 | REG_EXTENDED = 0001, |
---|
91 | REG_ICASE = 0002, |
---|
92 | REG_NOSUB = 0004, |
---|
93 | REG_NEWLINE = 0010, |
---|
94 | REG_NOSPEC = 0020, |
---|
95 | REG_PEND = 0040, |
---|
96 | REG_DUMP = 0200, |
---|
97 | REG_NOCOLLATE = 0400, |
---|
98 | REG_ESCAPE_IN_LISTS = 01000, |
---|
99 | REG_NEWLINE_ALT = 02000, |
---|
100 | REG_PERLEX = 04000, |
---|
101 | |
---|
102 | REG_PERL = REG_EXTENDED | REG_NOCOLLATE | REG_ESCAPE_IN_LISTS | REG_PERLEX, |
---|
103 | REG_AWK = REG_EXTENDED | REG_ESCAPE_IN_LISTS, |
---|
104 | REG_GREP = REG_BASIC | REG_NEWLINE_ALT, |
---|
105 | REG_EGREP = REG_EXTENDED | REG_NEWLINE_ALT, |
---|
106 | |
---|
107 | REG_ASSERT = 15, |
---|
108 | REG_INVARG = 16, |
---|
109 | REG_ATOI = 255, /* convert name to number (!) */ |
---|
110 | REG_ITOA = 0400 /* convert number to name (!) */ |
---|
111 | } reg_comp_flags; |
---|
112 | |
---|
113 | /* regexec() flags */ |
---|
114 | typedef enum{ |
---|
115 | REG_NOTBOL = 00001, |
---|
116 | REG_NOTEOL = 00002, |
---|
117 | REG_STARTEND = 00004 |
---|
118 | } reg_exec_flags; |
---|
119 | |
---|
120 | // |
---|
121 | // POSIX error codes: |
---|
122 | // |
---|
123 | typedef unsigned reg_error_t; |
---|
124 | typedef reg_error_t reg_errcode_t; // backwards compatibility |
---|
125 | |
---|
126 | static const reg_error_t REG_NOERROR = 0; /* Success. */ |
---|
127 | static const reg_error_t REG_NOMATCH = 1; /* Didn't find a match (for regexec). */ |
---|
128 | |
---|
129 | /* POSIX regcomp return error codes. (In the order listed in the |
---|
130 | standard.) */ |
---|
131 | static const reg_error_t REG_BADPAT = 2; /* Invalid pattern. */ |
---|
132 | static const reg_error_t REG_ECOLLATE = 3; /* Undefined collating element. */ |
---|
133 | static const reg_error_t REG_ECTYPE = 4; /* Invalid character class name. */ |
---|
134 | static const reg_error_t REG_EESCAPE = 5; /* Trailing backslash. */ |
---|
135 | static const reg_error_t REG_ESUBREG = 6; /* Invalid back reference. */ |
---|
136 | static const reg_error_t REG_EBRACK = 7; /* Unmatched left bracket. */ |
---|
137 | static const reg_error_t REG_EPAREN = 8; /* Parenthesis imbalance. */ |
---|
138 | static const reg_error_t REG_EBRACE = 9; /* Unmatched \{. */ |
---|
139 | static const reg_error_t REG_BADBR = 10; /* Invalid contents of \{\}. */ |
---|
140 | static const reg_error_t REG_ERANGE = 11; /* Invalid range end. */ |
---|
141 | static const reg_error_t REG_ESPACE = 12; /* Ran out of memory. */ |
---|
142 | static const reg_error_t REG_BADRPT = 13; /* No preceding re for repetition op. */ |
---|
143 | static const reg_error_t REG_EEND = 14; /* unexpected end of expression */ |
---|
144 | static const reg_error_t REG_ESIZE = 15; /* expression too big */ |
---|
145 | static const reg_error_t REG_ERPAREN = 8; /* = REG_EPAREN : unmatched right parenthesis */ |
---|
146 | static const reg_error_t REG_EMPTY = 17; /* empty expression */ |
---|
147 | static const reg_error_t REG_E_MEMORY = 15; /* = REG_ESIZE : out of memory */ |
---|
148 | static const reg_error_t REG_ECOMPLEXITY = 18; /* complexity too high */ |
---|
149 | static const reg_error_t REG_ESTACK = 19; /* out of stack space */ |
---|
150 | static const reg_error_t REG_E_UNKNOWN = 20; /* unknown error */ |
---|
151 | static const reg_error_t REG_ENOSYS = 20; /* = REG_E_UNKNOWN : Reserved. */ |
---|
152 | |
---|
153 | BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA*, const char*, int); |
---|
154 | BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int, const regex_tA*, char*, regsize_t); |
---|
155 | BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA*, const char*, regsize_t, regmatch_t*, int); |
---|
156 | BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA*); |
---|
157 | |
---|
158 | #ifndef BOOST_NO_WREGEX |
---|
159 | BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompW(regex_tW*, const wchar_t*, int); |
---|
160 | BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int, const regex_tW*, wchar_t*, regsize_t); |
---|
161 | BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecW(const regex_tW*, const wchar_t*, regsize_t, regmatch_t*, int); |
---|
162 | BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeW(regex_tW*); |
---|
163 | #endif |
---|
164 | |
---|
165 | #ifdef UNICODE |
---|
166 | #define regcomp regcompW |
---|
167 | #define regerror regerrorW |
---|
168 | #define regexec regexecW |
---|
169 | #define regfree regfreeW |
---|
170 | #define regex_t regex_tW |
---|
171 | #else |
---|
172 | #define regcomp regcompA |
---|
173 | #define regerror regerrorA |
---|
174 | #define regexec regexecA |
---|
175 | #define regfree regfreeA |
---|
176 | #define regex_t regex_tA |
---|
177 | #endif |
---|
178 | |
---|
179 | #ifdef BOOST_HAS_ABI_HEADERS |
---|
180 | # include BOOST_ABI_SUFFIX |
---|
181 | #endif |
---|
182 | |
---|
183 | #ifdef __cplusplus |
---|
184 | } // extern "C" |
---|
185 | } // namespace |
---|
186 | #endif |
---|
187 | |
---|
188 | // |
---|
189 | // C++ high level wrapper goes here: |
---|
190 | // |
---|
191 | #if defined(__cplusplus) |
---|
192 | #include <string> |
---|
193 | #include <vector> |
---|
194 | namespace boost{ |
---|
195 | |
---|
196 | #ifdef BOOST_HAS_ABI_HEADERS |
---|
197 | # include BOOST_ABI_PREFIX |
---|
198 | #endif |
---|
199 | |
---|
200 | class RegEx; |
---|
201 | |
---|
202 | namespace re_detail{ |
---|
203 | |
---|
204 | class RegExData; |
---|
205 | struct pred1; |
---|
206 | struct pred2; |
---|
207 | struct pred3; |
---|
208 | struct pred4; |
---|
209 | |
---|
210 | } // namespace re_detail |
---|
211 | |
---|
212 | #if (defined(BOOST_MSVC) || defined(__BORLANDC__)) && !defined(BOOST_DISABLE_WIN32) |
---|
213 | typedef bool (__cdecl *GrepCallback)(const RegEx& expression); |
---|
214 | typedef bool (__cdecl *GrepFileCallback)(const char* file, const RegEx& expression); |
---|
215 | typedef bool (__cdecl *FindFilesCallback)(const char* file); |
---|
216 | #else |
---|
217 | typedef bool (*GrepCallback)(const RegEx& expression); |
---|
218 | typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression); |
---|
219 | typedef bool (*FindFilesCallback)(const char* file); |
---|
220 | #endif |
---|
221 | |
---|
222 | class BOOST_REGEX_DECL RegEx |
---|
223 | { |
---|
224 | private: |
---|
225 | re_detail::RegExData* pdata; |
---|
226 | public: |
---|
227 | RegEx(); |
---|
228 | RegEx(const RegEx& o); |
---|
229 | ~RegEx(); |
---|
230 | explicit RegEx(const char* c, bool icase = false); |
---|
231 | explicit RegEx(const std::string& s, bool icase = false); |
---|
232 | RegEx& operator=(const RegEx& o); |
---|
233 | RegEx& operator=(const char* p); |
---|
234 | RegEx& operator=(const std::string& s){ return this->operator=(s.c_str()); } |
---|
235 | unsigned int SetExpression(const char* p, bool icase = false); |
---|
236 | unsigned int SetExpression(const std::string& s, bool icase = false){ return SetExpression(s.c_str(), icase); } |
---|
237 | std::string Expression()const; |
---|
238 | unsigned int error_code()const; |
---|
239 | // |
---|
240 | // now matching operators: |
---|
241 | // |
---|
242 | bool Match(const char* p, match_flag_type flags = match_default); |
---|
243 | bool Match(const std::string& s, match_flag_type flags = match_default) { return Match(s.c_str(), flags); } |
---|
244 | bool Search(const char* p, match_flag_type flags = match_default); |
---|
245 | bool Search(const std::string& s, match_flag_type flags = match_default) { return Search(s.c_str(), flags); } |
---|
246 | unsigned int Grep(GrepCallback cb, const char* p, match_flag_type flags = match_default); |
---|
247 | unsigned int Grep(GrepCallback cb, const std::string& s, match_flag_type flags = match_default) { return Grep(cb, s.c_str(), flags); } |
---|
248 | unsigned int Grep(std::vector<std::string>& v, const char* p, match_flag_type flags = match_default); |
---|
249 | unsigned int Grep(std::vector<std::string>& v, const std::string& s, match_flag_type flags = match_default) { return Grep(v, s.c_str(), flags); } |
---|
250 | unsigned int Grep(std::vector<std::size_t>& v, const char* p, match_flag_type flags = match_default); |
---|
251 | unsigned int Grep(std::vector<std::size_t>& v, const std::string& s, match_flag_type flags = match_default) { return Grep(v, s.c_str(), flags); } |
---|
252 | #ifndef BOOST_REGEX_NO_FILEITER |
---|
253 | unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false, match_flag_type flags = match_default); |
---|
254 | unsigned int GrepFiles(GrepFileCallback cb, const std::string& files, bool recurse = false, match_flag_type flags = match_default) { return GrepFiles(cb, files.c_str(), recurse, flags); } |
---|
255 | unsigned int FindFiles(FindFilesCallback cb, const char* files, bool recurse = false, match_flag_type flags = match_default); |
---|
256 | unsigned int FindFiles(FindFilesCallback cb, const std::string& files, bool recurse = false, match_flag_type flags = match_default) { return FindFiles(cb, files.c_str(), recurse, flags); } |
---|
257 | #endif |
---|
258 | |
---|
259 | std::string Merge(const std::string& in, const std::string& fmt, |
---|
260 | bool copy = true, match_flag_type flags = match_default); |
---|
261 | std::string Merge(const char* in, const char* fmt, |
---|
262 | bool copy = true, match_flag_type flags = match_default); |
---|
263 | |
---|
264 | std::size_t Split(std::vector<std::string>& v, std::string& s, match_flag_type flags = match_default, unsigned max_count = ~0); |
---|
265 | // |
---|
266 | // now operators for returning what matched in more detail: |
---|
267 | // |
---|
268 | std::size_t Position(int i = 0)const; |
---|
269 | std::size_t Length(int i = 0)const; |
---|
270 | bool Matched(int i = 0)const; |
---|
271 | std::size_t Marks()const; |
---|
272 | std::string What(int i = 0)const; |
---|
273 | std::string operator[](int i)const { return What(i); } |
---|
274 | |
---|
275 | static const std::size_t npos; |
---|
276 | |
---|
277 | friend struct re_detail::pred1; |
---|
278 | friend struct re_detail::pred2; |
---|
279 | friend struct re_detail::pred3; |
---|
280 | friend struct re_detail::pred4; |
---|
281 | }; |
---|
282 | |
---|
283 | #ifdef BOOST_HAS_ABI_HEADERS |
---|
284 | # include BOOST_ABI_SUFFIX |
---|
285 | #endif |
---|
286 | |
---|
287 | } // namespace boost |
---|
288 | |
---|
289 | #endif |
---|
290 | |
---|
291 | #endif // include guard |
---|
292 | |
---|
293 | |
---|
294 | |
---|
295 | |
---|
296 | |
---|
297 | |
---|
298 | |
---|
299 | |
---|
300 | |
---|
301 | |
---|