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.
15 * VERSION: see <boost/version.hpp>
16 * DESCRIPTION: Implements the Posix API wrappers.
19 #define BOOST_REGEX_SOURCE
21 #include <boost/config.hpp>
23 #include <boost/regex.hpp>
24 #include <boost/cregex.hpp>
26 #if defined(BOOST_NO_STDC_NAMESPACE)
39 unsigned int magic_value = 25631;
41 const char* names[] = {
67 typedef boost::basic_regex<char, c_regex_traits<char> > c_regex_type;
69 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA* expression, const char* ptr, int f)
71 #ifndef BOOST_NO_EXCEPTIONS
74 expression->guts = new c_regex_type();
75 #ifndef BOOST_NO_EXCEPTIONS
82 if(0 == expression->guts)
86 boost::uint_fast32_t flags = (f & REG_PERLEX) ? 0 : ((f & REG_EXTENDED) ? regex::extended : regex::basic);
87 expression->eflags = (f & REG_NEWLINE) ? match_not_dot_newline : match_default;
88 // and translate those that are actually set:
92 flags |= regex::nocollate;
93 #ifndef BOOST_REGEX_V3
94 flags &= ~regex::collate;
100 //expression->eflags |= match_any;
101 flags |= regex::nosubs;
105 flags |= regex::literal;
107 flags |= regex::icase;
108 if(f & REG_ESCAPE_IN_LISTS)
109 flags &= ~regex::no_escape_in_lists;
110 if(f & REG_NEWLINE_ALT)
111 flags |= regex::newline_alt;
115 p2 = expression->re_endp;
116 else p2 = ptr + std::strlen(ptr);
120 #ifndef BOOST_NO_EXCEPTIONS
123 expression->re_magic = magic_value;
124 static_cast<c_regex_type*>(expression->guts)->set_expression(ptr, p2, flags);
125 expression->re_nsub = static_cast<c_regex_type*>(expression->guts)->mark_count();
126 result = static_cast<c_regex_type*>(expression->guts)->error_code();
127 #ifndef BOOST_NO_EXCEPTIONS
129 catch(const boost::regex_error& be)
135 result = REG_E_UNKNOWN;
139 regfreeA(expression);
144 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int code, const regex_tA* e, char* buf, regsize_t buf_size)
146 std::size_t result = 0;
150 if(code <= (int)REG_E_UNKNOWN)
152 result = std::strlen(names[code]) + 1;
153 if(buf_size >= result)
154 BOOST_REGEX_DETAIL_NS::strcpy_s(buf, buf_size, names[code]);
164 for(int i = 0; i <= (int)REG_E_UNKNOWN; ++i)
166 if(std::strcmp(e->re_endp, names[i]) == 0)
169 // We're converting an integer i to a string, and since i <= REG_E_UNKNOWN
170 // a five character string is *always* large enough:
172 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
173 int r = (::sprintf_s)(localbuf, 5, "%d", i);
175 int r = (std::sprintf)(localbuf, "%d", i);
178 return 0; // sprintf failed
179 if(std::strlen(localbuf) < buf_size)
180 BOOST_REGEX_DETAIL_NS::strcpy_s(buf, buf_size, localbuf);
181 return std::strlen(localbuf) + 1;
184 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
185 int r = (::sprintf_s)(localbuf, 5, "%d", 0);
187 int r = (std::sprintf)(localbuf, "%d", 0);
190 return 0; // sprintf failed
191 if(std::strlen(localbuf) < buf_size)
192 BOOST_REGEX_DETAIL_NS::strcpy_s(buf, buf_size, localbuf);
193 return std::strlen(localbuf) + 1;
195 if(code <= (int)REG_E_UNKNOWN)
198 if((e) && (e->re_magic == magic_value))
199 p = static_cast<c_regex_type*>(e->guts)->get_traits().error_string(static_cast< ::boost::regex_constants::error_type>(code));
202 p = BOOST_REGEX_DETAIL_NS::get_default_error_string(static_cast< ::boost::regex_constants::error_type>(code));
204 std::size_t len = p.size();
207 BOOST_REGEX_DETAIL_NS::strcpy_s(buf, buf_size, p.c_str());
216 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA* expression, const char* buf, regsize_t n, regmatch_t* array, int eflags)
219 #pragma warning(push)
220 #pragma warning(disable:4267)
223 match_flag_type flags = match_default | expression->eflags;
228 if(eflags & REG_NOTBOL)
229 flags |= match_not_bol;
230 if(eflags & REG_NOTEOL)
231 flags |= match_not_eol;
232 if(eflags & REG_STARTEND)
234 start = buf + array[0].rm_so;
235 end = buf + array[0].rm_eo;
240 end = buf + std::strlen(buf);
243 #ifndef BOOST_NO_EXCEPTIONS
246 if(expression->re_magic == magic_value)
248 result = regex_search(start, end, m, *static_cast<c_regex_type*>(expression->guts), flags);
252 #ifndef BOOST_NO_EXCEPTIONS
255 return REG_E_UNKNOWN;
261 // extract what matched:
263 for(i = 0; (i < n) && (i < expression->re_nsub + 1); ++i)
265 array[i].rm_so = (m[i].matched == false) ? -1 : (m[i].first - buf);
266 array[i].rm_eo = (m[i].matched == false) ? -1 : (m[i].second - buf);
268 // and set anything else to -1:
269 for(i = expression->re_nsub + 1; i < n; ++i)
282 BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA* expression)
284 if(expression->re_magic == magic_value)
286 delete static_cast<c_regex_type*>(expression->guts);
288 expression->re_magic = 0;