regex_workaround.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE regex_workarounds.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares Misc workarounds.
  16. */
  17. #ifndef BOOST_REGEX_WORKAROUND_HPP
  18. #define BOOST_REGEX_WORKAROUND_HPP
  19. #include <boost/regex/config.hpp>
  20. #ifndef BOOST_REGEX_AS_MODULE
  21. #include <algorithm>
  22. #include <stdexcept>
  23. #include <cstring>
  24. #endif
  25. #ifndef BOOST_REGEX_STANDALONE
  26. #include <boost/detail/workaround.hpp>
  27. #include <boost/throw_exception.hpp>
  28. #endif
  29. #ifdef BOOST_REGEX_NO_BOOL
  30. # define BOOST_REGEX_MAKE_BOOL(x) static_cast<bool>((x) ? true : false)
  31. #else
  32. # define BOOST_REGEX_MAKE_BOOL(x) static_cast<bool>(x)
  33. #endif
  34. /*****************************************************************************
  35. *
  36. * helper functions pointer_construct/pointer_destroy:
  37. *
  38. ****************************************************************************/
  39. #ifdef __cplusplus
  40. namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
  41. #ifdef BOOST_REGEX_MSVC
  42. #pragma warning (push)
  43. #pragma warning (disable : 4100)
  44. #endif
  45. template <class T>
  46. inline void pointer_destroy(T* p)
  47. { p->~T(); (void)p; }
  48. #ifdef BOOST_REGEX_MSVC
  49. #pragma warning (pop)
  50. #endif
  51. template <class T>
  52. inline void pointer_construct(T* p, const T& t)
  53. { new (p) T(t); }
  54. }} // namespaces
  55. #endif
  56. /*****************************************************************************
  57. *
  58. * helper function copy:
  59. *
  60. ****************************************************************************/
  61. #if defined(BOOST_WORKAROUND)
  62. #if BOOST_WORKAROUND(BOOST_REGEX_MSVC, >= 1400) && defined(__STDC_WANT_SECURE_LIB__) && __STDC_WANT_SECURE_LIB__
  63. #define BOOST_REGEX_HAS_STRCPY_S
  64. #endif
  65. #endif
  66. #ifdef __cplusplus
  67. namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
  68. #if defined(BOOST_REGEX_MSVC) && (BOOST_REGEX_MSVC < 1910)
  69. //
  70. // MSVC 10 will either emit warnings or else refuse to compile
  71. // code that makes perfectly legitimate use of std::copy, when
  72. // the OutputIterator type is a user-defined class (apparently all user
  73. // defined iterators are "unsafe"). What's more Microsoft have removed their
  74. // non-standard "unchecked" versions, even though they are still in the MS
  75. // documentation!! Work around this as best we can:
  76. //
  77. template<class InputIterator, class OutputIterator>
  78. inline OutputIterator copy(
  79. InputIterator first,
  80. InputIterator last,
  81. OutputIterator dest
  82. )
  83. {
  84. while (first != last)
  85. *dest++ = *first++;
  86. return dest;
  87. }
  88. #else
  89. using std::copy;
  90. #endif
  91. #if defined(BOOST_REGEX_HAS_STRCPY_S)
  92. // use safe versions of strcpy etc:
  93. using ::strcpy_s;
  94. using ::strcat_s;
  95. #else
  96. inline std::size_t strcpy_s(
  97. char *strDestination,
  98. std::size_t sizeInBytes,
  99. const char *strSource
  100. )
  101. {
  102. std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
  103. if (lenSourceWithNull > sizeInBytes)
  104. return 1;
  105. std::memcpy(strDestination, strSource, lenSourceWithNull);
  106. return 0;
  107. }
  108. inline std::size_t strcat_s(
  109. char *strDestination,
  110. std::size_t sizeInBytes,
  111. const char *strSource
  112. )
  113. {
  114. std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
  115. std::size_t lenDestination = std::strlen(strDestination);
  116. if (lenSourceWithNull + lenDestination > sizeInBytes)
  117. return 1;
  118. std::memcpy(strDestination + lenDestination, strSource, lenSourceWithNull);
  119. return 0;
  120. }
  121. #endif
  122. inline void overflow_error_if_not_zero(std::size_t i)
  123. {
  124. if(i)
  125. {
  126. std::overflow_error e("String buffer too small");
  127. #ifndef BOOST_REGEX_STANDALONE
  128. boost::throw_exception(e);
  129. #else
  130. throw e;
  131. #endif
  132. }
  133. }
  134. }} // namespaces
  135. #endif // __cplusplus
  136. #endif // include guard