]> git.lyx.org Git - lyx.git/blob - boost/boost/cregex.hpp
simplify some, ws, begin minibuffer simplification
[lyx.git] / boost / boost / cregex.hpp
1 /*
2  *
3  * Copyright (c) 1998-2000
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      3.03
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/re_detail/regex_config.hpp>
28
29 #ifdef __BORLANDC__
30    #if __BORLANDC__ == 0x530
31     #pragma option push -a4 -b -Ve
32    #elif __BORLANDC__ > 0x530
33     #pragma option push -a8 -b -Ve
34    #endif
35 #endif
36
37 /* include these defs only for POSIX compatablity */
38 #ifdef __cplusplus
39 namespace boost{
40 extern "C" {
41 #endif
42
43 typedef int regoff_t;
44
45 typedef struct
46 {
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 :-) */
52 } regex_tA;
53
54 #ifndef BOOST_RE_NO_WCSTRING
55 typedef struct
56 {
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 :-) */
62 } regex_tW;
63 #endif
64
65 typedef struct
66 {
67    regoff_t rm_so;      /* start of match */
68    regoff_t rm_eo;      /* end of match */
69 } regmatch_t;
70
71 /* regcomp() flags */
72 typedef enum{
73    REG_BASIC = 0000,
74    REG_EXTENDED = 0001,
75    REG_ICASE = 0002,
76    REG_NOSUB = 0004,
77    REG_NEWLINE = 0010,
78    REG_NOSPEC = 0020,
79    REG_PEND = 0040,
80    REG_DUMP = 0200,
81    REG_NOCOLLATE = 0400,
82    REG_ESCAPE_IN_LISTS = 01000,
83    REG_NEWLINE_ALT = 02000,
84
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,
89
90    REG_ASSERT = 15,
91    REG_INVARG = 16,
92    REG_ATOI = 255,   /* convert name to number (!) */
93    REG_ITOA = 0400   /* convert number to name (!) */
94 } reg_comp_flags;
95
96 /* regexec() flags */
97 typedef enum{
98    REG_NOTBOL =    00001,
99    REG_NOTEOL =    00002,
100    REG_STARTEND =  00004
101 } reg_exec_flags;
102
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*);
107
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*);
113 #endif
114
115 #ifdef UNICODE
116 #define regcomp regcompW
117 #define regerror regerrorW
118 #define regexec regexecW
119 #define regfree regfreeW
120 #define regex_t regex_tW
121 #else
122 #define regcomp regcompA
123 #define regerror regerrorA
124 #define regexec regexecA
125 #define regfree regfreeA
126 #define regex_t regex_tA
127 #endif
128
129 /* regerror() flags */
130 typedef enum
131 {
132   REG_NOERROR = 0,   /* Success.  */
133   REG_NOMATCH = 1,      /* Didn't find a match (for regexec).  */
134
135   /* POSIX regcomp return error codes.  (In the order listed in the
136      standard.)  */
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 */
155 } reg_errcode_t;
156
157 enum match_flags
158 {
159    match_default = 0,
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
175    
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
179 };
180
181
182 #ifdef __cplusplus
183 } // extern "C"
184 } // namespace
185 #endif
186
187
188 #ifdef __BORLANDC__
189  #if __BORLANDC__ > 0x520
190   #pragma option pop
191  #endif
192 #endif
193
194
195 //
196 // C++ high level wrapper goes here:
197 //
198 #if defined(__cplusplus) && !defined(BOOST_RE_NO_STRING_H)
199 #include <string>
200 #include <vector>
201 namespace boost{
202
203 #ifdef __BORLANDC__
204    #if __BORLANDC__ == 0x530
205     #pragma option push -a4 -b
206    #elif __BORLANDC__ > 0x530
207     #pragma option push -a8 -b
208    #endif
209 #endif
210
211 class RegEx;
212
213 namespace re_detail{
214
215 class RegExData;
216 struct pred1;
217 struct pred2;
218 struct pred3;
219 struct pred4;
220
221 }  // namespace re_detail
222
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);
227 #else
228 typedef bool (*GrepCallback)(const RegEx& expression);
229 typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression);
230 typedef bool (*FindFilesCallback)(const char* file);
231 #endif
232
233 class BOOST_RE_IX_DECL RegEx
234 {
235 private:
236    re_detail::RegExData* pdata;
237 public:
238    RegEx();
239    RegEx(const RegEx& o);
240    ~RegEx();
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;
249    //
250    // now matching operators:
251    //
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); }
266
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);
271
272    unsigned int Split(std::vector<std::string>& v, std::string& s, unsigned flags = match_default, unsigned max_count = ~0);
273    //
274    // now operators for returning what matched in more detail:
275    //
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); }
282
283    friend struct re_detail::pred1;
284    friend struct re_detail::pred2;
285    friend struct re_detail::pred3;
286    friend struct re_detail::pred4;
287 };
288
289 #ifdef __BORLANDC__
290  #if __BORLANDC__ > 0x520
291   #pragma option pop
292  #endif
293 #endif
294
295 } // namespace boost
296
297 #endif
298
299 #endif // include guard
300
301
302
303
304
305