]> git.lyx.org Git - features.git/blob - boost/libs/regex/src/posix_api.cpp
boost: update to 1.42.0
[features.git] / boost / libs / regex / src / posix_api.cpp
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:        posix_api.cpp
15   *   VERSION:     see <boost/version.hpp>
16   *   DESCRIPTION: Implements the Posix API wrappers.
17   */
18
19 #define BOOST_REGEX_SOURCE
20
21 #include <cstdio>
22 #include <boost/regex.hpp>
23 #include <boost/cregex.hpp>
24
25 #if defined(BOOST_NO_STDC_NAMESPACE)
26 namespace std{
27    using ::sprintf;
28    using ::strcpy;
29    using ::strcmp;
30 }
31 #endif
32
33
34 namespace boost{
35
36 namespace{
37
38 unsigned int magic_value = 25631;
39
40 const char* names[] = {
41       "REG_NOERROR",
42       "REG_NOMATCH",
43       "REG_BADPAT",
44       "REG_ECOLLATE",
45       "REG_ECTYPE",
46       "REG_EESCAPE",
47       "REG_ESUBREG",
48       "REG_EBRACK",
49       "REG_EPAREN",
50       "REG_EBRACE",
51       "REG_BADBR",
52       "REG_ERANGE",
53       "REG_ESPACE",
54       "REG_BADRPT",
55       "REG_EEND",
56       "REG_ESIZE",
57       "REG_ERPAREN",
58       "REG_EMPTY",
59       "REG_ECOMPLEXITY",
60       "REG_ESTACK",
61       "REG_E_PERL",
62       "REG_E_UNKNOWN",
63 };
64 } // namespace
65
66 typedef boost::basic_regex<char, c_regex_traits<char> > c_regex_type;
67
68 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA* expression, const char* ptr, int f)
69 {
70    if(expression->re_magic != magic_value)
71    {
72       expression->guts = 0;
73 #ifndef BOOST_NO_EXCEPTIONS
74       try{
75 #endif
76       expression->guts = new c_regex_type();
77 #ifndef BOOST_NO_EXCEPTIONS
78       } catch(...)
79       {
80          return REG_ESPACE;
81       }
82 #else
83       if(0 == expression->guts)
84          return REG_E_MEMORY;
85 #endif
86    }
87    // set default flags:
88    boost::uint_fast32_t flags = (f & REG_PERLEX) ? 0 : ((f & REG_EXTENDED) ? regex::extended : regex::basic);
89    expression->eflags = (f & REG_NEWLINE) ? match_not_dot_newline : match_default;
90    // and translate those that are actually set:
91
92    if(f & REG_NOCOLLATE)
93    {
94       flags |= regex::nocollate;
95 #ifndef BOOST_REGEX_V3
96       flags &= ~regex::collate;
97 #endif
98    }
99
100    if(f & REG_NOSUB)
101    {
102       //expression->eflags |= match_any;
103       flags |= regex::nosubs;
104    }
105
106    if(f & REG_NOSPEC)
107       flags |= regex::literal;
108    if(f & REG_ICASE)
109       flags |= regex::icase;
110    if(f & REG_ESCAPE_IN_LISTS)
111       flags &= ~regex::no_escape_in_lists;
112    if(f & REG_NEWLINE_ALT)
113       flags |= regex::newline_alt;
114
115    const char* p2;
116    if(f & REG_PEND)
117       p2 = expression->re_endp;
118    else p2 = ptr + std::strlen(ptr);
119
120    int result;
121
122 #ifndef BOOST_NO_EXCEPTIONS
123    try{
124 #endif
125       expression->re_magic = magic_value;
126       static_cast<c_regex_type*>(expression->guts)->set_expression(ptr, p2, flags);
127       expression->re_nsub = static_cast<c_regex_type*>(expression->guts)->mark_count() - 1;
128       result = static_cast<c_regex_type*>(expression->guts)->error_code();
129 #ifndef BOOST_NO_EXCEPTIONS
130    } 
131    catch(const boost::regex_error& be)
132    {
133       result = be.code();
134    }
135    catch(...)
136    {
137       result = REG_E_UNKNOWN;
138    }
139 #endif
140    if(result)
141       regfreeA(expression);
142    return result;
143
144 }
145
146 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int code, const regex_tA* e, char* buf, regsize_t buf_size)
147 {
148    std::size_t result = 0;
149    if(code & REG_ITOA)
150    {
151       code &= ~REG_ITOA;
152       if(code <= (int)REG_E_UNKNOWN)
153       {
154          result = std::strlen(names[code]) + 1;
155          if(buf_size >= result)
156             re_detail::strcpy_s(buf, buf_size, names[code]);
157          return result;
158       }
159       return result;
160    }
161    if(code == REG_ATOI)
162    {
163       char localbuf[5];
164       if(e == 0)
165          return 0;
166       for(int i = 0; i <= (int)REG_E_UNKNOWN; ++i)
167       {
168          if(std::strcmp(e->re_endp, names[i]) == 0)
169          {
170 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
171             (::sprintf_s)(localbuf, 5, "%d", i);
172 #else
173             (std::sprintf)(localbuf, "%d", i);
174 #endif
175             if(std::strlen(localbuf) < buf_size)
176                re_detail::strcpy_s(buf, buf_size, localbuf);
177             return std::strlen(localbuf) + 1;
178          }
179       }
180 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
181       (::sprintf_s)(localbuf, 5, "%d", 0);
182 #else
183       (std::sprintf)(localbuf, "%d", 0);
184 #endif
185       if(std::strlen(localbuf) < buf_size)
186          re_detail::strcpy_s(buf, buf_size, localbuf);
187       return std::strlen(localbuf) + 1;
188    }
189    if(code <= (int)REG_E_UNKNOWN)
190    {
191       std::string p;
192       if((e) && (e->re_magic == magic_value))
193          p = static_cast<c_regex_type*>(e->guts)->get_traits().error_string(static_cast< ::boost::regex_constants::error_type>(code));
194       else
195       {
196          p = re_detail::get_default_error_string(static_cast< ::boost::regex_constants::error_type>(code));
197       }
198       std::size_t len = p.size();
199       if(len < buf_size)
200       {
201          re_detail::strcpy_s(buf, buf_size, p.c_str());
202       }
203       return len + 1;
204    }
205    if(buf_size)
206       *buf = 0;
207    return 0;
208 }
209
210 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA* expression, const char* buf, regsize_t n, regmatch_t* array, int eflags)
211 {
212 #ifdef BOOST_MSVC
213 #pragma warning(push)
214 #pragma warning(disable:4267)
215 #endif
216    bool result = false;
217    match_flag_type flags = match_default | expression->eflags;
218    const char* end;
219    const char* start;
220    cmatch m;
221    
222    if(eflags & REG_NOTBOL)
223       flags |= match_not_bol;
224    if(eflags & REG_NOTEOL)
225       flags |= match_not_eol;
226    if(eflags & REG_STARTEND)
227    {
228       start = buf + array[0].rm_so;
229       end = buf + array[0].rm_eo;
230    }
231    else
232    {
233       start = buf;
234       end = buf + std::strlen(buf);
235    }
236
237 #ifndef BOOST_NO_EXCEPTIONS
238    try{
239 #endif
240    if(expression->re_magic == magic_value)
241    {
242       result = regex_search(start, end, m, *static_cast<c_regex_type*>(expression->guts), flags);
243    }
244    else
245       return result;
246 #ifndef BOOST_NO_EXCEPTIONS
247    } catch(...)
248    {
249       return REG_E_UNKNOWN;
250    }
251 #endif
252
253    if(result)
254    {
255       // extract what matched:
256       std::size_t i;
257       for(i = 0; (i < n) && (i < expression->re_nsub + 1); ++i)
258       {
259          array[i].rm_so = (m[i].matched == false) ? -1 : (m[i].first - buf);
260          array[i].rm_eo = (m[i].matched == false) ? -1 : (m[i].second - buf);
261       }
262       // and set anything else to -1:
263       for(i = expression->re_nsub + 1; i < n; ++i)
264       {
265          array[i].rm_so = -1;
266          array[i].rm_eo = -1;
267       }
268       return 0;
269    }
270    return REG_NOMATCH;
271 #ifdef BOOST_MSVC
272 #pragma warning(pop)
273 #endif
274 }
275
276 BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA* expression)
277 {
278    if(expression->re_magic == magic_value)
279    {
280       delete static_cast<c_regex_type*>(expression->guts);
281    }
282    expression->re_magic = 0;
283 }
284
285 } // namespace boost
286
287
288
289