3 * Copyright (c) 1998-2002
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)
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.
19 #define BOOST_REGEX_SOURCE
21 #include <boost/regex/config.hpp>
23 #ifndef BOOST_NO_WREGEX
25 #include <boost/regex.hpp>
26 #include <boost/cregex.hpp>
33 #pragma warning(disable:981)
36 #if defined(BOOST_NO_STDC_NAMESPACE) || defined(__NetBSD__)
38 # ifndef BOOST_NO_SWPRINTF
49 unsigned int wmagic_value
= 28631;
51 const wchar_t* wnames
[] = {
77 typedef boost::basic_regex
<wchar_t, c_regex_traits
<wchar_t> > wc_regex_type
;
80 # pragma warning(push)
81 #pragma warning(disable:26812)
83 BOOST_REGEX_DECL
int BOOST_REGEX_CCALL
regcompW(regex_tW
* expression
, const wchar_t* ptr
, int f
)
85 #ifndef BOOST_NO_EXCEPTIONS
88 expression
->guts
= new wc_regex_type();
89 #ifndef BOOST_NO_EXCEPTIONS
96 if(0 == expression
->guts
)
100 boost::uint_fast32_t flags
= (f
& REG_PERLEX
) ? 0 : ((f
& REG_EXTENDED
) ? wregex::extended
: wregex::basic
);
101 expression
->eflags
= (f
& REG_NEWLINE
) ? match_not_dot_newline
: match_default
;
103 // and translate those that are actually set:
104 if(f
& REG_NOCOLLATE
)
106 flags
|= wregex::nocollate
;
107 #ifndef BOOST_REGEX_V3
108 flags
&= ~wregex::collate
;
114 //expression->eflags |= match_any;
115 flags
|= wregex::nosubs
;
119 flags
|= wregex::literal
;
121 flags
|= wregex::icase
;
122 if(f
& REG_ESCAPE_IN_LISTS
)
123 flags
&= ~wregex::no_escape_in_lists
;
124 if(f
& REG_NEWLINE_ALT
)
125 flags
|= wregex::newline_alt
;
129 p2
= expression
->re_endp
;
130 else p2
= ptr
+ std::wcslen(ptr
);
134 #ifndef BOOST_NO_EXCEPTIONS
137 expression
->re_magic
= wmagic_value
;
138 static_cast<wc_regex_type
*>(expression
->guts
)->set_expression(ptr
, p2
, flags
);
139 expression
->re_nsub
= static_cast<wc_regex_type
*>(expression
->guts
)->mark_count();
140 result
= static_cast<wc_regex_type
*>(expression
->guts
)->error_code();
141 #ifndef BOOST_NO_EXCEPTIONS
143 catch(const boost::regex_error
& be
)
149 result
= REG_E_UNKNOWN
;
153 regfreeW(expression
);
158 # pragma warning(pop)
161 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL
regerrorW(int code
, const regex_tW
* e
, wchar_t* buf
, regsize_t buf_size
)
163 std::size_t result
= 0;
167 if((code
<= (int)REG_E_UNKNOWN
) && (code
>= 0))
169 result
= std::wcslen(wnames
[code
]) + 1;
170 if(buf_size
>= result
)
171 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
172 ::wcscpy_s(buf
, buf_size
, wnames
[code
]);
174 std::wcscpy(buf
, wnames
[code
]);
180 #if !defined(BOOST_NO_SWPRINTF)
186 for(int i
= 0; i
<= (int)REG_E_UNKNOWN
; ++i
)
188 if(std::wcscmp(e
->re_endp
, wnames
[i
]) == 0)
190 #if defined(_WIN32_WCE) && !defined(UNDER_CE)
191 (std::swprintf
)(localbuf
, L
"%d", i
);
193 (std::swprintf
)(localbuf
, 5, L
"%d", i
);
195 if(std::wcslen(localbuf
) < buf_size
)
196 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
197 ::wcscpy_s(buf
, buf_size
, localbuf
);
199 std::wcscpy(buf
, localbuf
);
201 return std::wcslen(localbuf
) + 1;
204 #if defined(_WIN32_WCE) && !defined(UNDER_CE)
205 (std::swprintf
)(localbuf
, L
"%d", 0);
207 (std::swprintf
)(localbuf
, 5, L
"%d", 0);
209 if(std::wcslen(localbuf
) < buf_size
)
210 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
211 ::wcscpy_s(buf
, buf_size
, localbuf
);
213 std::wcscpy(buf
, localbuf
);
215 return std::wcslen(localbuf
) + 1;
218 if(code
<= (int)REG_E_UNKNOWN
)
221 if((e
) && (e
->re_magic
== wmagic_value
))
222 p
= static_cast<wc_regex_type
*>(e
->guts
)->get_traits().error_string(static_cast< ::boost::regex_constants::error_type
>(code
));
225 p
= BOOST_REGEX_DETAIL_NS::get_default_error_string(static_cast< ::boost::regex_constants::error_type
>(code
));
227 std::size_t len
= p
.size();
230 BOOST_REGEX_DETAIL_NS::copy(p
.c_str(), p
.c_str() + p
.size() + 1, buf
);
239 BOOST_REGEX_DECL
int BOOST_REGEX_CCALL
regexecW(const regex_tW
* expression
, const wchar_t* buf
, regsize_t n
, regmatch_t
* array
, int eflags
)
242 #pragma warning(push)
243 #pragma warning(disable:4267)
246 match_flag_type flags
= match_default
| expression
->eflags
;
248 const wchar_t* start
;
251 if(eflags
& REG_NOTBOL
)
252 flags
|= match_not_bol
;
253 if(eflags
& REG_NOTEOL
)
254 flags
|= match_not_eol
;
255 if(eflags
& REG_STARTEND
)
257 start
= buf
+ array
[0].rm_so
;
258 end
= buf
+ array
[0].rm_eo
;
263 end
= buf
+ std::wcslen(buf
);
266 #ifndef BOOST_NO_EXCEPTIONS
269 if(expression
->re_magic
== wmagic_value
)
271 result
= regex_search(start
, end
, m
, *static_cast<wc_regex_type
*>(expression
->guts
), flags
);
275 #ifndef BOOST_NO_EXCEPTIONS
278 return REG_E_UNKNOWN
;
283 // extract what matched:
285 for(i
= 0; (i
< n
) && (i
< expression
->re_nsub
+ 1); ++i
)
287 array
[i
].rm_so
= (m
[i
].matched
== false) ? -1 : (m
[i
].first
- buf
);
288 array
[i
].rm_eo
= (m
[i
].matched
== false) ? -1 : (m
[i
].second
- buf
);
290 // and set anything else to -1:
291 for(i
= expression
->re_nsub
+ 1; i
< n
; ++i
)
304 BOOST_REGEX_DECL
void BOOST_REGEX_CCALL
regfreeW(regex_tW
* expression
)
306 if(expression
->re_magic
== wmagic_value
)
308 delete static_cast<wc_regex_type
*>(expression
->guts
);
310 expression
->re_magic
= 0;
313 } // namespace boost;