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