1 | /* |
2 | * |
3 | * Copyright (c) 1998-2005 |
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 regex_workarounds.cpp |
15 | * VERSION see <boost/version.hpp> |
16 | * DESCRIPTION: Declares Misc workarounds. |
17 | */ |
18 | |
19 | #ifndef BOOST_REGEX_WORKAROUND_HPP |
20 | #define BOOST_REGEX_WORKAROUND_HPP |
21 | |
22 | |
23 | #include <new> |
24 | #include <cstring> |
25 | #include <cstdlib> |
26 | #include <cstddef> |
27 | #include <cassert> |
28 | #include <cstdio> |
29 | #include <climits> |
30 | #include <string> |
31 | #include <stdexcept> |
32 | #include <iterator> |
33 | #include <algorithm> |
34 | #include <iosfwd> |
35 | #include <vector> |
36 | #include <map> |
37 | #include <boost/limits.hpp> |
38 | #include <boost/assert.hpp> |
39 | #include <boost/cstdint.hpp> |
40 | #include <boost/throw_exception.hpp> |
41 | #include <boost/scoped_ptr.hpp> |
42 | #include <boost/scoped_array.hpp> |
43 | #include <boost/shared_ptr.hpp> |
44 | #include <boost/mpl/bool_fwd.hpp> |
45 | #include <boost/regex/config.hpp> |
46 | #ifndef BOOST_NO_STD_LOCALE |
47 | # include <locale> |
48 | #endif |
49 | |
50 | #if defined(BOOST_NO_STDC_NAMESPACE) |
51 | namespace std{ |
52 | using ::sprintf; using ::strcpy; using ::strcat; using ::strlen; |
53 | } |
54 | #endif |
55 | |
56 | namespace boost{ namespace BOOST_REGEX_DETAIL_NS{ |
57 | #ifdef BOOST_NO_STD_DISTANCE |
58 | template <class T> |
59 | std::ptrdiff_t distance(const T& x, const T& y) |
60 | { return y - x; } |
61 | #else |
62 | using std::distance; |
63 | #endif |
64 | }} |
65 | |
66 | |
67 | #ifdef BOOST_REGEX_NO_BOOL |
68 | # define BOOST_REGEX_MAKE_BOOL(x) static_cast<bool>((x) ? true : false) |
69 | #else |
70 | # define BOOST_REGEX_MAKE_BOOL(x) static_cast<bool>(x) |
71 | #endif |
72 | |
73 | /***************************************************************************** |
74 | * |
75 | * Fix broken namespace support: |
76 | * |
77 | ****************************************************************************/ |
78 | |
79 | #if defined(BOOST_NO_STDC_NAMESPACE) && defined(__cplusplus) |
80 | |
81 | namespace std{ |
82 | using ::ptrdiff_t; |
83 | using ::size_t; |
84 | using ::abs; |
85 | using ::memset; |
86 | using ::memcpy; |
87 | } |
88 | |
89 | #endif |
90 | |
91 | /***************************************************************************** |
92 | * |
93 | * helper functions pointer_construct/pointer_destroy: |
94 | * |
95 | ****************************************************************************/ |
96 | |
97 | #ifdef __cplusplus |
98 | namespace boost{ namespace BOOST_REGEX_DETAIL_NS{ |
99 | |
100 | #ifdef BOOST_MSVC |
101 | #pragma warning (push) |
102 | #pragma warning (disable : 4100) |
103 | #endif |
104 | |
105 | template <class T> |
106 | inline void pointer_destroy(T* p) |
107 | { p->~T(); (void)p; } |
108 | |
109 | #ifdef BOOST_MSVC |
110 | #pragma warning (pop) |
111 | #endif |
112 | |
113 | template <class T> |
114 | inline void pointer_construct(T* p, const T& t) |
115 | { new (p) T(t); } |
116 | |
117 | }} // namespaces |
118 | #endif |
119 | |
120 | /***************************************************************************** |
121 | * |
122 | * helper function copy: |
123 | * |
124 | ****************************************************************************/ |
125 | |
126 | #ifdef __cplusplus |
127 | namespace boost{ namespace BOOST_REGEX_DETAIL_NS{ |
128 | #if BOOST_WORKAROUND(BOOST_MSVC,>=1400) && BOOST_WORKAROUND(BOOST_MSVC, <1600) && defined(_CPPLIB_VER) && defined(BOOST_DINKUMWARE_STDLIB) && !(defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)) |
129 | // |
130 | // MSVC 8 will either emit warnings or else refuse to compile |
131 | // code that makes perfectly legitimate use of std::copy, when |
132 | // the OutputIterator type is a user-defined class (apparently all user |
133 | // defined iterators are "unsafe"). This code works around that: |
134 | // |
135 | template<class InputIterator, class OutputIterator> |
136 | inline OutputIterator copy( |
137 | InputIterator first, |
138 | InputIterator last, |
139 | OutputIterator dest |
140 | ) |
141 | { |
142 | return stdext::unchecked_copy(first, last, dest); |
143 | } |
144 | template<class InputIterator1, class InputIterator2> |
145 | inline bool equal( |
146 | InputIterator1 first, |
147 | InputIterator1 last, |
148 | InputIterator2 with |
149 | ) |
150 | { |
151 | return stdext::unchecked_equal(first, last, with); |
152 | } |
153 | #elif BOOST_WORKAROUND(BOOST_MSVC, > 1500) |
154 | // |
155 | // MSVC 10 will either emit warnings or else refuse to compile |
156 | // code that makes perfectly legitimate use of std::copy, when |
157 | // the OutputIterator type is a user-defined class (apparently all user |
158 | // defined iterators are "unsafe"). What's more Microsoft have removed their |
159 | // non-standard "unchecked" versions, even though their still in the MS |
160 | // documentation!! Work around this as best we can: |
161 | // |
162 | template<class InputIterator, class OutputIterator> |
163 | inline OutputIterator copy( |
164 | InputIterator first, |
165 | InputIterator last, |
166 | OutputIterator dest |
167 | ) |
168 | { |
169 | while(first != last) |
170 | *dest++ = *first++; |
171 | return dest; |
172 | } |
173 | template<class InputIterator1, class InputIterator2> |
174 | inline bool equal( |
175 | InputIterator1 first, |
176 | InputIterator1 last, |
177 | InputIterator2 with |
178 | ) |
179 | { |
180 | while(first != last) |
181 | if(*first++ != *with++) return false; |
182 | return true; |
183 | } |
184 | #else |
185 | using std::copy; |
186 | using std::equal; |
187 | #endif |
188 | #if BOOST_WORKAROUND(BOOST_MSVC,>=1400) && defined(__STDC_WANT_SECURE_LIB__) && __STDC_WANT_SECURE_LIB__ |
189 | |
190 | // use safe versions of strcpy etc: |
191 | using ::strcpy_s; |
192 | using ::strcat_s; |
193 | #else |
194 | inline std::size_t strcpy_s( |
195 | char *strDestination, |
196 | std::size_t sizeInBytes, |
197 | const char *strSource |
198 | ) |
199 | { |
200 | if(std::strlen(s: strSource)+1 > sizeInBytes) |
201 | return 1; |
202 | std::strcpy(dest: strDestination, src: strSource); |
203 | return 0; |
204 | } |
205 | inline std::size_t strcat_s( |
206 | char *strDestination, |
207 | std::size_t sizeInBytes, |
208 | const char *strSource |
209 | ) |
210 | { |
211 | if(std::strlen(s: strSource) + std::strlen(s: strDestination) + 1 > sizeInBytes) |
212 | return 1; |
213 | std::strcat(dest: strDestination, src: strSource); |
214 | return 0; |
215 | } |
216 | |
217 | #endif |
218 | |
219 | inline void overflow_error_if_not_zero(std::size_t i) |
220 | { |
221 | if(i) |
222 | { |
223 | std::overflow_error e("String buffer too small" ); |
224 | boost::throw_exception(e); |
225 | } |
226 | } |
227 | |
228 | }} // namespaces |
229 | |
230 | #endif // __cplusplus |
231 | |
232 | #endif // include guard |
233 | |
234 | |