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