3 * Copyright (c) 1998-2002
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Dr John Maddock makes no representations
11 * about the suitability of this software for any purpose.
12 * It is provided "as is" without express or implied warranty.
17 * LOCATION: see http://www.boost.org for most recent version.
19 * VERSION see <boost/version.hpp>
20 * DESCRIPTION: Declares POSIX API functions
21 * + boost::RegEx high level wrapper.
24 #ifndef BOOST_RE_CREGEX_HPP
25 #define BOOST_RE_CREGEX_HPP
27 #include <boost/regex/config.hpp>
30 #pragma option push -a8 -b -Vx -Ve -pc
33 /* include these defs only for POSIX compatablity */
39 #if defined(__cplusplus) && !defined(BOOST_NO_STDC_NAMESPACE)
40 typedef std::ptrdiff_t regoff_t;
41 typedef std::size_t regsize_t;
43 typedef ptrdiff_t regoff_t;
44 typedef size_t regsize_t;
49 unsigned int re_magic;
50 unsigned int re_nsub; /* number of parenthesized subexpressions */
51 const char* re_endp; /* end pointer for REG_PEND */
52 void* guts; /* none of your business :-) */
53 unsigned int eflags; /* none of your business :-) */
56 #ifndef BOOST_NO_WREGEX
59 unsigned int re_magic;
60 unsigned int re_nsub; /* number of parenthesized subexpressions */
61 const wchar_t* re_endp; /* end pointer for REG_PEND */
62 void* guts; /* none of your business :-) */
63 unsigned int eflags; /* none of your business :-) */
69 regoff_t rm_so; /* start of match */
70 regoff_t rm_eo; /* end of match */
84 REG_ESCAPE_IN_LISTS = 01000,
85 REG_NEWLINE_ALT = 02000,
87 REG_PERL = REG_EXTENDED | REG_NOCOLLATE | REG_ESCAPE_IN_LISTS,
88 REG_AWK = REG_EXTENDED | REG_ESCAPE_IN_LISTS,
89 REG_GREP = REG_BASIC | REG_NEWLINE_ALT,
90 REG_EGREP = REG_EXTENDED | REG_NEWLINE_ALT,
94 REG_ATOI = 255, /* convert name to number (!) */
95 REG_ITOA = 0400 /* convert number to name (!) */
105 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA*, const char*, int);
106 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int, const regex_tA*, char*, regsize_t);
107 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA*, const char*, regsize_t, regmatch_t*, int);
108 BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA*);
110 #ifndef BOOST_NO_WREGEX
111 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompW(regex_tW*, const wchar_t*, int);
112 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int, const regex_tW*, wchar_t*, regsize_t);
113 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecW(const regex_tW*, const wchar_t*, regsize_t, regmatch_t*, int);
114 BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeW(regex_tW*);
118 #define regcomp regcompW
119 #define regerror regerrorW
120 #define regexec regexecW
121 #define regfree regfreeW
122 #define regex_t regex_tW
124 #define regcomp regcompA
125 #define regerror regerrorA
126 #define regexec regexecA
127 #define regfree regfreeA
128 #define regex_t regex_tA
131 /* regerror() flags */
134 REG_NOERROR = 0, /* Success. */
135 REG_NOMATCH = 1, /* Didn't find a match (for regexec). */
137 /* POSIX regcomp return error codes. (In the order listed in the
139 REG_BADPAT = 2, /* Invalid pattern. */
140 REG_ECOLLATE = 3, /* Undefined collating element. */
141 REG_ECTYPE = 4, /* Invalid character class name. */
142 REG_EESCAPE = 5, /* Trailing backslash. */
143 REG_ESUBREG = 6, /* Invalid back reference. */
144 REG_EBRACK = 7, /* Unmatched left bracket. */
145 REG_EPAREN = 8, /* Parenthesis imbalance. */
146 REG_EBRACE = 9, /* Unmatched \{. */
147 REG_BADBR = 10, /* Invalid contents of \{\}. */
148 REG_ERANGE = 11, /* Invalid range end. */
149 REG_ESPACE = 12, /* Ran out of memory. */
150 REG_BADRPT = 13, /* No preceding re for repetition op. */
151 REG_EEND = 14, /* unexpected end of expression */
152 REG_ESIZE = 15, /* expression too big */
153 REG_ERPAREN = 16, /* unmatched right parenthesis */
154 REG_EMPTY = 17, /* empty expression */
155 REG_E_MEMORY = REG_ESIZE, /* out of memory */
156 REG_E_UNKNOWN = 18 /* unknown error */
162 match_not_bol = 1, // first is not start of line
163 match_not_eol = match_not_bol << 1, // last is not end of line
164 match_not_bob = match_not_eol << 1, // first is not start of buffer
165 match_not_eob = match_not_bob << 1, // last is not end of buffer
166 match_not_bow = match_not_eob << 1, // first is not start of word
167 match_not_eow = match_not_bow << 1, // last is not end of word
168 match_not_dot_newline = match_not_eow << 1, // \n is not matched by '.'
169 match_not_dot_null = match_not_dot_newline << 1, // '\0' is not matched by '.'
170 match_prev_avail = match_not_dot_null << 1, // *--first is a valid expression
171 match_init = match_prev_avail << 1, // internal use
172 match_any = match_init << 1, // don't care what we match
173 match_not_null = match_any << 1, // string can't be null
174 match_continuous = match_not_null << 1, // each grep match must continue from
175 // uninterupted from the previous one
176 match_partial = match_continuous << 1, // find partial matches
178 match_stop = match_partial << 1, // stop after first match (grep)
179 match_all = match_stop << 1, // must find the whole of input even if match_any is set
180 match_max = match_all
191 #if __BORLANDC__ > 0x520
198 // C++ high level wrapper goes here:
200 #if defined(__cplusplus)
206 #if __BORLANDC__ == 0x530
207 #pragma option push -a4 -b
208 #elif __BORLANDC__ > 0x530
209 #pragma option push -a8 -b
223 } // namespace re_detail
225 #if defined(BOOST_MSVC) || defined(__BORLANDC__)
226 typedef bool (__cdecl *GrepCallback)(const RegEx& expression);
227 typedef bool (__cdecl *GrepFileCallback)(const char* file, const RegEx& expression);
228 typedef bool (__cdecl *FindFilesCallback)(const char* file);
230 typedef bool (*GrepCallback)(const RegEx& expression);
231 typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression);
232 typedef bool (*FindFilesCallback)(const char* file);
235 class BOOST_REGEX_DECL RegEx
238 re_detail::RegExData* pdata;
241 RegEx(const RegEx& o);
243 explicit RegEx(const char* c, bool icase = false);
244 explicit RegEx(const std::string& s, bool icase = false);
245 RegEx& operator=(const RegEx& o);
246 RegEx& operator=(const char* p);
247 RegEx& operator=(const std::string& s){ return this->operator=(s.c_str()); }
248 unsigned int SetExpression(const char* p, bool icase = false);
249 unsigned int SetExpression(const std::string& s, bool icase = false){ return SetExpression(s.c_str(), icase); }
250 std::string Expression()const;
251 unsigned int error_code()const;
253 // now matching operators:
255 bool Match(const char* p, unsigned int flags = match_default);
256 bool Match(const std::string& s, unsigned int flags = match_default) { return Match(s.c_str(), flags); }
257 bool Search(const char* p, unsigned int flags = match_default);
258 bool Search(const std::string& s, unsigned int flags = match_default) { return Search(s.c_str(), flags); }
259 unsigned int Grep(GrepCallback cb, const char* p, unsigned int flags = match_default);
260 unsigned int Grep(GrepCallback cb, const std::string& s, unsigned int flags = match_default) { return Grep(cb, s.c_str(), flags); }
261 unsigned int Grep(std::vector<std::string>& v, const char* p, unsigned int flags = match_default);
262 unsigned int Grep(std::vector<std::string>& v, const std::string& s, unsigned int flags = match_default) { return Grep(v, s.c_str(), flags); }
263 unsigned int Grep(std::vector<std::size_t>& v, const char* p, unsigned int flags = match_default);
264 unsigned int Grep(std::vector<std::size_t>& v, const std::string& s, unsigned int flags = match_default) { return Grep(v, s.c_str(), flags); }
265 #ifndef BOOST_REGEX_NO_FILEITER
266 unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false, unsigned int flags = match_default);
267 unsigned int GrepFiles(GrepFileCallback cb, const std::string& files, bool recurse = false, unsigned int flags = match_default) { return GrepFiles(cb, files.c_str(), recurse, flags); }
268 unsigned int FindFiles(FindFilesCallback cb, const char* files, bool recurse = false, unsigned int flags = match_default);
269 unsigned int FindFiles(FindFilesCallback cb, const std::string& files, bool recurse = false, unsigned int flags = match_default) { return FindFiles(cb, files.c_str(), recurse, flags); }
272 std::string Merge(const std::string& in, const std::string& fmt,
273 bool copy = true, unsigned int flags = match_default);
274 std::string Merge(const char* in, const char* fmt,
275 bool copy = true, unsigned int flags = match_default);
277 std::size_t Split(std::vector<std::string>& v, std::string& s, unsigned flags = match_default, unsigned max_count = ~0);
279 // now operators for returning what matched in more detail:
281 std::size_t Position(int i = 0)const;
282 std::size_t Length(int i = 0)const;
283 bool Matched(int i = 0)const;
284 unsigned int Line()const;
285 unsigned int Marks()const;
286 std::string What(int i = 0)const;
287 std::string operator[](int i)const { return What(i); }
289 static const unsigned int npos;
291 friend struct re_detail::pred1;
292 friend struct re_detail::pred2;
293 friend struct re_detail::pred3;
294 friend struct re_detail::pred4;
305 #endif // include guard