]> git.lyx.org Git - lyx.git/blob - boost/boost/cregex.hpp
Jrgen's "Tooltips for the reference dialog" patch. I've tried to incorporate
[lyx.git] / boost / boost / cregex.hpp
1 /*
2  *
3  * Copyright (c) 1998-2002
4  * Dr John Maddock
5  *
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.
13  *
14  */
15  
16  /*
17   *   LOCATION:    see http://www.boost.org for most recent version.
18   *   FILE         cregex.cpp
19   *   VERSION      see <boost/version.hpp>
20   *   DESCRIPTION: Declares POSIX API functions
21   *                + boost::RegEx high level wrapper.
22   */
23
24 #ifndef BOOST_RE_CREGEX_HPP
25 #define BOOST_RE_CREGEX_HPP
26
27 #include <boost/regex/config.hpp>
28
29 #ifdef __BORLANDC__
30    #pragma option push -a4 -b -Ve -pc
31 #endif
32
33 /* include these defs only for POSIX compatablity */
34 #ifdef __cplusplus
35 namespace boost{
36 extern "C" {
37 #endif
38
39 #if defined(__cplusplus) && !defined(BOOST_NO_STDC_NAMESPACE)
40 typedef std::ptrdiff_t regoff_t;
41 typedef std::size_t regsize_t;
42 #else
43 typedef ptrdiff_t regoff_t;
44 typedef size_t regsize_t;
45 #endif
46
47 typedef struct
48 {
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 :-) */
54 } regex_tA;
55
56 #ifndef BOOST_NO_WREGEX
57 typedef struct
58 {
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 :-) */
64 } regex_tW;
65 #endif
66
67 typedef struct
68 {
69    regoff_t rm_so;      /* start of match */
70    regoff_t rm_eo;      /* end of match */
71 } regmatch_t;
72
73 /* regcomp() flags */
74 typedef enum{
75    REG_BASIC = 0000,
76    REG_EXTENDED = 0001,
77    REG_ICASE = 0002,
78    REG_NOSUB = 0004,
79    REG_NEWLINE = 0010,
80    REG_NOSPEC = 0020,
81    REG_PEND = 0040,
82    REG_DUMP = 0200,
83    REG_NOCOLLATE = 0400,
84    REG_ESCAPE_IN_LISTS = 01000,
85    REG_NEWLINE_ALT = 02000,
86
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,
91
92    REG_ASSERT = 15,
93    REG_INVARG = 16,
94    REG_ATOI = 255,   /* convert name to number (!) */
95    REG_ITOA = 0400   /* convert number to name (!) */
96 } reg_comp_flags;
97
98 /* regexec() flags */
99 typedef enum{
100    REG_NOTBOL =    00001,
101    REG_NOTEOL =    00002,
102    REG_STARTEND =  00004
103 } reg_exec_flags;
104
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*);
109
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*);
115 #endif
116
117 #ifdef UNICODE
118 #define regcomp regcompW
119 #define regerror regerrorW
120 #define regexec regexecW
121 #define regfree regfreeW
122 #define regex_t regex_tW
123 #else
124 #define regcomp regcompA
125 #define regerror regerrorA
126 #define regexec regexecA
127 #define regfree regfreeA
128 #define regex_t regex_tA
129 #endif
130
131 /* regerror() flags */
132 typedef enum
133 {
134   REG_NOERROR = 0,   /* Success.  */
135   REG_NOMATCH = 1,      /* Didn't find a match (for regexec).  */
136
137   /* POSIX regcomp return error codes.  (In the order listed in the
138      standard.)  */
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 */
157 } reg_errcode_t;
158
159 enum match_flags
160 {
161    match_default = 0,
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
177    
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
181 };
182
183
184 #ifdef __cplusplus
185 } // extern "C"
186 } // namespace
187 #endif
188
189
190 #ifdef __BORLANDC__
191  #if __BORLANDC__ > 0x520
192   #pragma option pop
193  #endif
194 #endif
195
196
197 //
198 // C++ high level wrapper goes here:
199 //
200 #if defined(__cplusplus)
201 #include <string>
202 #include <vector>
203 namespace boost{
204
205 #ifdef __BORLANDC__
206    #if __BORLANDC__ == 0x530
207     #pragma option push -a4 -b
208    #elif __BORLANDC__ > 0x530
209     #pragma option push -a8 -b
210    #endif
211 #endif
212
213 class RegEx;
214
215 namespace re_detail{
216
217 class RegExData;
218 struct pred1;
219 struct pred2;
220 struct pred3;
221 struct pred4;
222
223 }  // namespace re_detail
224
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);
229 #else
230 typedef bool (*GrepCallback)(const RegEx& expression);
231 typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression);
232 typedef bool (*FindFilesCallback)(const char* file);
233 #endif
234
235 class BOOST_REGEX_DECL RegEx
236 {
237 private:
238    re_detail::RegExData* pdata;
239 public:
240    RegEx();
241    RegEx(const RegEx& o);
242    ~RegEx();
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;
252    //
253    // now matching operators:
254    //
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); }
270 #endif
271
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);
276
277    std::size_t Split(std::vector<std::string>& v, std::string& s, unsigned flags = match_default, unsigned max_count = ~0);
278    //
279    // now operators for returning what matched in more detail:
280    //
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); }
288
289    static const unsigned int npos;
290
291    friend struct re_detail::pred1;
292    friend struct re_detail::pred2;
293    friend struct re_detail::pred3;
294    friend struct re_detail::pred4;
295 };
296
297 #ifdef __BORLANDC__
298   #pragma option pop
299 #endif
300
301 } // namespace boost
302
303 #endif
304
305 #endif // include guard
306
307
308
309
310
311
312