3 * Copyright (c) 1998-2000
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.
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/re_detail/regex_config.hpp>
30 #if __BORLANDC__ == 0x530
31 #pragma option push -a4 -b -Ve
32 #elif __BORLANDC__ > 0x530
33 #pragma option push -a8 -b -Ve
37 /* include these defs only for POSIX compatablity */
47 unsigned int re_magic;
48 unsigned int re_nsub; /* number of parenthesized subexpressions */
49 const char* re_endp; /* end pointer for REG_PEND */
50 void* guts; /* none of your business :-) */
51 unsigned int eflags; /* none of your business :-) */
54 #ifndef BOOST_RE_NO_WCSTRING
57 unsigned int re_magic;
58 unsigned int re_nsub; /* number of parenthesized subexpressions */
59 const wchar_t* re_endp; /* end pointer for REG_PEND */
60 void* guts; /* none of your business :-) */
61 unsigned int eflags; /* none of your business :-) */
67 regoff_t rm_so; /* start of match */
68 regoff_t rm_eo; /* end of match */
82 REG_ESCAPE_IN_LISTS = 01000,
83 REG_NEWLINE_ALT = 02000,
85 REG_PERL = REG_EXTENDED | REG_NOCOLLATE | REG_ESCAPE_IN_LISTS,
86 REG_AWK = REG_EXTENDED | REG_ESCAPE_IN_LISTS,
87 REG_GREP = REG_BASIC | REG_NEWLINE_ALT,
88 REG_EGREP = REG_EXTENDED | REG_NEWLINE_ALT,
92 REG_ATOI = 255, /* convert name to number (!) */
93 REG_ITOA = 0400 /* convert number to name (!) */
103 BOOST_RE_IX_DECL int BOOST_RE_CCALL regcompA(regex_tA*, const char*, int);
104 BOOST_RE_IX_DECL unsigned int BOOST_RE_CCALL regerrorA(int, const regex_tA*, char*, unsigned int);
105 BOOST_RE_IX_DECL int BOOST_RE_CCALL regexecA(const regex_tA*, const char*, unsigned int, regmatch_t*, int);
106 BOOST_RE_IX_DECL void BOOST_RE_CCALL regfreeA(regex_tA*);
108 #ifndef BOOST_RE_NO_WCSTRING
109 BOOST_RE_IX_DECL int BOOST_RE_CCALL regcompW(regex_tW*, const wchar_t*, int);
110 BOOST_RE_IX_DECL unsigned int BOOST_RE_CCALL regerrorW(int, const regex_tW*, wchar_t*, unsigned int);
111 BOOST_RE_IX_DECL int BOOST_RE_CCALL regexecW(const regex_tW*, const wchar_t*, unsigned int, regmatch_t*, int);
112 BOOST_RE_IX_DECL void BOOST_RE_CCALL regfreeW(regex_tW*);
116 #define regcomp regcompW
117 #define regerror regerrorW
118 #define regexec regexecW
119 #define regfree regfreeW
120 #define regex_t regex_tW
122 #define regcomp regcompA
123 #define regerror regerrorA
124 #define regexec regexecA
125 #define regfree regfreeA
126 #define regex_t regex_tA
129 /* regerror() flags */
132 REG_NOERROR = 0, /* Success. */
133 REG_NOMATCH = 1, /* Didn't find a match (for regexec). */
135 /* POSIX regcomp return error codes. (In the order listed in the
137 REG_BADPAT = 2, /* Invalid pattern. */
138 REG_ECOLLATE = 3, /* Undefined collating element. */
139 REG_ECTYPE = 4, /* Invalid character class name. */
140 REG_EESCAPE = 5, /* Trailing backslash. */
141 REG_ESUBREG = 6, /* Invalid back reference. */
142 REG_EBRACK = 7, /* Unmatched left bracket. */
143 REG_EPAREN = 8, /* Parenthesis imbalance. */
144 REG_EBRACE = 9, /* Unmatched \{. */
145 REG_BADBR = 10, /* Invalid contents of \{\}. */
146 REG_ERANGE = 11, /* Invalid range end. */
147 REG_ESPACE = 12, /* Ran out of memory. */
148 REG_BADRPT = 13, /* No preceding re for repetition op. */
149 REG_EEND = 14, /* unexpected end of expression */
150 REG_ESIZE = 15, /* expression too big */
151 REG_ERPAREN = 16, /* unmatched right parenthesis */
152 REG_EMPTY = 17, /* empty expression */
153 REG_E_MEMORY = REG_ESIZE, /* out of memory */
154 REG_E_UNKNOWN = 18 /* unknown error */
160 match_not_bol = 1, // first is not start of line
161 match_not_eol = match_not_bol << 1, // last is not end of line
162 match_not_bob = match_not_eol << 1, // first is not start of buffer
163 match_not_eob = match_not_bob << 1, // last is not end of buffer
164 match_not_bow = match_not_eob << 1, // first is not start of word
165 match_not_eow = match_not_bow << 1, // last is not end of word
166 match_not_dot_newline = match_not_eow << 1, // \n is not matched by '.'
167 match_not_dot_null = match_not_dot_newline << 1, // '\0' is not matched by '.'
168 match_prev_avail = match_not_dot_null << 1, // *--first is a valid expression
169 match_init = match_prev_avail << 1, // internal use
170 match_any = match_init << 1, // don't care what we match
171 match_not_null = match_any << 1, // string can't be null
172 match_continuous = match_not_null << 1, // each grep match must continue from
173 // uninterupted from the previous one
174 match_partial = match_continuous << 1, // find partial matches
176 match_stop = match_partial << 1, // stop after first match (grep)
177 match_all = match_stop << 1, // must find the whole of input even if match_any is set
178 match_max = match_all
189 #if __BORLANDC__ > 0x520
196 // C++ high level wrapper goes here:
198 #if defined(__cplusplus) && !defined(BOOST_RE_NO_STRING_H)
204 #if __BORLANDC__ == 0x530
205 #pragma option push -a4 -b
206 #elif __BORLANDC__ > 0x530
207 #pragma option push -a8 -b
221 } // namespace re_detail
223 #if defined(BOOST_MSVC) || defined(__BORLANDC__)
224 typedef bool (__cdecl *GrepCallback)(const RegEx& expression);
225 typedef bool (__cdecl *GrepFileCallback)(const char* file, const RegEx& expression);
226 typedef bool (__cdecl *FindFilesCallback)(const char* file);
228 typedef bool (*GrepCallback)(const RegEx& expression);
229 typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression);
230 typedef bool (*FindFilesCallback)(const char* file);
233 class BOOST_RE_IX_DECL RegEx
236 re_detail::RegExData* pdata;
239 RegEx(const RegEx& o);
241 explicit RegEx(const char* c, bool icase = false);
242 explicit RegEx(const std::string& s, bool icase = false);
243 RegEx& operator=(const RegEx& o);
244 RegEx& operator=(const char* p);
245 RegEx& operator=(const std::string& s){ return this->operator=(s.c_str()); }
246 unsigned int SetExpression(const char* p, bool icase = false);
247 unsigned int SetExpression(const std::string& s, bool icase = false){ return SetExpression(s.c_str(), icase); }
248 std::string Expression()const;
250 // now matching operators:
252 bool Match(const char* p, unsigned int flags = match_default);
253 bool Match(const std::string& s, unsigned int flags = match_default) { return Match(s.c_str(), flags); }
254 bool Search(const char* p, unsigned int flags = match_default);
255 bool Search(const std::string& s, unsigned int flags = match_default) { return Search(s.c_str(), flags); }
256 unsigned int Grep(GrepCallback cb, const char* p, unsigned int flags = match_default);
257 unsigned int Grep(GrepCallback cb, const std::string& s, unsigned int flags = match_default) { return Grep(cb, s.c_str(), flags); }
258 unsigned int Grep(std::vector<std::string>& v, const char* p, unsigned int flags = match_default);
259 unsigned int Grep(std::vector<std::string>& v, const std::string& s, unsigned int flags = match_default) { return Grep(v, s.c_str(), flags); }
260 unsigned int Grep(std::vector<unsigned int>& v, const char* p, unsigned int flags = match_default);
261 unsigned int Grep(std::vector<unsigned int>& v, const std::string& s, unsigned int flags = match_default) { return Grep(v, s.c_str(), flags); }
262 unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false, unsigned int flags = match_default);
263 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); }
264 unsigned int FindFiles(FindFilesCallback cb, const char* files, bool recurse = false, unsigned int flags = match_default);
265 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); }
267 std::string Merge(const std::string& in, const std::string& fmt,
268 bool copy = true, unsigned int flags = match_default);
269 std::string Merge(const char* in, const char* fmt,
270 bool copy = true, unsigned int flags = match_default);
272 unsigned int Split(std::vector<std::string>& v, std::string& s, unsigned flags = match_default, unsigned max_count = ~0);
274 // now operators for returning what matched in more detail:
276 unsigned int Position(int i = 0)const;
277 unsigned int Length(int i = 0)const;
278 unsigned int Line()const;
279 unsigned int Marks()const;
280 std::string What(int i = 0)const;
281 std::string operator[](int i)const { return What(i); }
283 friend struct re_detail::pred1;
284 friend struct re_detail::pred2;
285 friend struct re_detail::pred3;
286 friend struct re_detail::pred4;
290 #if __BORLANDC__ > 0x520
299 #endif // include guard