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