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