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