sclang: ServerShmInterface - avoid object duplication in deepCopy
[supercollider.git] / external_libraries / boost / libs / regex / src / posix_api.cpp
blobabafd8f23190c36c78cd675941ba67a7199e75df
1 /*
3 * Copyright (c) 1998-2002
4 * John Maddock
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: posix_api.cpp
15 * VERSION: see <boost/version.hpp>
16 * DESCRIPTION: Implements the Posix API wrappers.
19 #define BOOST_REGEX_SOURCE
21 #include <cstdio>
22 #include <boost/regex.hpp>
23 #include <boost/cregex.hpp>
25 #if defined(BOOST_NO_STDC_NAMESPACE)
26 namespace std{
27 using ::sprintf;
28 using ::strcpy;
29 using ::strcmp;
31 #endif
34 namespace boost{
36 namespace{
38 unsigned int magic_value = 25631;
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_PERL",
62 "REG_E_UNKNOWN",
64 } // namespace
66 typedef boost::basic_regex<char, c_regex_traits<char> > c_regex_type;
68 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA* expression, const char* ptr, int f)
70 if(expression->re_magic != magic_value)
72 expression->guts = 0;
73 #ifndef BOOST_NO_EXCEPTIONS
74 try{
75 #endif
76 expression->guts = new c_regex_type();
77 #ifndef BOOST_NO_EXCEPTIONS
78 } catch(...)
80 return REG_ESPACE;
82 #else
83 if(0 == expression->guts)
84 return REG_E_MEMORY;
85 #endif
87 // set default flags:
88 boost::uint_fast32_t flags = (f & REG_PERLEX) ? 0 : ((f & REG_EXTENDED) ? regex::extended : regex::basic);
89 expression->eflags = (f & REG_NEWLINE) ? match_not_dot_newline : match_default;
90 // and translate those that are actually set:
92 if(f & REG_NOCOLLATE)
94 flags |= regex::nocollate;
95 #ifndef BOOST_REGEX_V3
96 flags &= ~regex::collate;
97 #endif
100 if(f & REG_NOSUB)
102 //expression->eflags |= match_any;
103 flags |= regex::nosubs;
106 if(f & REG_NOSPEC)
107 flags |= regex::literal;
108 if(f & REG_ICASE)
109 flags |= regex::icase;
110 if(f & REG_ESCAPE_IN_LISTS)
111 flags &= ~regex::no_escape_in_lists;
112 if(f & REG_NEWLINE_ALT)
113 flags |= regex::newline_alt;
115 const char* p2;
116 if(f & REG_PEND)
117 p2 = expression->re_endp;
118 else p2 = ptr + std::strlen(ptr);
120 int result;
122 #ifndef BOOST_NO_EXCEPTIONS
123 try{
124 #endif
125 expression->re_magic = magic_value;
126 static_cast<c_regex_type*>(expression->guts)->set_expression(ptr, p2, flags);
127 expression->re_nsub = static_cast<c_regex_type*>(expression->guts)->mark_count() - 1;
128 result = static_cast<c_regex_type*>(expression->guts)->error_code();
129 #ifndef BOOST_NO_EXCEPTIONS
131 catch(const boost::regex_error& be)
133 result = be.code();
135 catch(...)
137 result = REG_E_UNKNOWN;
139 #endif
140 if(result)
141 regfreeA(expression);
142 return result;
146 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int code, const regex_tA* e, char* buf, regsize_t buf_size)
148 std::size_t result = 0;
149 if(code & REG_ITOA)
151 code &= ~REG_ITOA;
152 if(code <= (int)REG_E_UNKNOWN)
154 result = std::strlen(names[code]) + 1;
155 if(buf_size >= result)
156 re_detail::strcpy_s(buf, buf_size, names[code]);
157 return result;
159 return result;
161 if(code == REG_ATOI)
163 char localbuf[5];
164 if(e == 0)
165 return 0;
166 for(int i = 0; i <= (int)REG_E_UNKNOWN; ++i)
168 if(std::strcmp(e->re_endp, names[i]) == 0)
171 // We're converting an integer i to a string, and since i <= REG_E_UNKNOWN
172 // a five character string is *always* large enough:
174 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
175 int r = (::sprintf_s)(localbuf, 5, "%d", i);
176 #else
177 int r = (std::sprintf)(localbuf, "%d", i);
178 #endif
179 if(r < 0)
180 return 0; // sprintf failed
181 if(std::strlen(localbuf) < buf_size)
182 re_detail::strcpy_s(buf, buf_size, localbuf);
183 return std::strlen(localbuf) + 1;
186 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
187 (::sprintf_s)(localbuf, 5, "%d", 0);
188 #else
189 (std::sprintf)(localbuf, "%d", 0);
190 #endif
191 if(std::strlen(localbuf) < buf_size)
192 re_detail::strcpy_s(buf, buf_size, localbuf);
193 return std::strlen(localbuf) + 1;
195 if(code <= (int)REG_E_UNKNOWN)
197 std::string p;
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));
200 else
202 p = re_detail::get_default_error_string(static_cast< ::boost::regex_constants::error_type>(code));
204 std::size_t len = p.size();
205 if(len < buf_size)
207 re_detail::strcpy_s(buf, buf_size, p.c_str());
209 return len + 1;
211 if(buf_size)
212 *buf = 0;
213 return 0;
216 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA* expression, const char* buf, regsize_t n, regmatch_t* array, int eflags)
218 #ifdef BOOST_MSVC
219 #pragma warning(push)
220 #pragma warning(disable:4267)
221 #endif
222 bool result = false;
223 match_flag_type flags = match_default | expression->eflags;
224 const char* end;
225 const char* start;
226 cmatch m;
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;
237 else
239 start = buf;
240 end = buf + std::strlen(buf);
243 #ifndef BOOST_NO_EXCEPTIONS
244 try{
245 #endif
246 if(expression->re_magic == magic_value)
248 result = regex_search(start, end, m, *static_cast<c_regex_type*>(expression->guts), flags);
250 else
251 return result;
252 #ifndef BOOST_NO_EXCEPTIONS
253 } catch(...)
255 return REG_E_UNKNOWN;
257 #endif
259 if(result)
261 // extract what matched:
262 std::size_t i;
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)
271 array[i].rm_so = -1;
272 array[i].rm_eo = -1;
274 return 0;
276 return REG_NOMATCH;
277 #ifdef BOOST_MSVC
278 #pragma warning(pop)
279 #endif
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;
291 } // namespace boost