mfc.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. *
  3. * Copyright (c) 2004
  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 mfc.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Overloads and helpers for using MFC/ATL string types with Boost.Regex.
  16. */
  17. #ifndef BOOST_REGEX_MFC_HPP
  18. #define BOOST_REGEX_MFC_HPP
  19. #include <atlsimpstr.h>
  20. #include <boost/regex.hpp>
  21. namespace boost{
  22. //
  23. // define the types used for TCHAR's:
  24. typedef basic_regex<TCHAR> tregex;
  25. typedef match_results<TCHAR const*> tmatch;
  26. typedef regex_iterator<TCHAR const*> tregex_iterator;
  27. typedef regex_token_iterator<TCHAR const*> tregex_token_iterator;
  28. // Obsolete. Remove
  29. #define SIMPLE_STRING_PARAM class B, bool b
  30. #define SIMPLE_STRING_ARG_LIST B, b
  31. //
  32. // define regex creation functions:
  33. //
  34. template <class B, bool b>
  35. inline basic_regex<B>
  36. make_regex(const ATL::CSimpleStringT<B, b>& s, ::boost::regex_constants::syntax_option_type f = boost::regex_constants::normal)
  37. {
  38. basic_regex<B> result(s.GetString(), s.GetString() + s.GetLength(), f);
  39. return result;
  40. }
  41. //
  42. // regex_match overloads:
  43. //
  44. template <class B, bool b, class A, class T>
  45. inline bool regex_match(const ATL::CSimpleStringT<B, b>& s,
  46. match_results<const B*, A>& what,
  47. const basic_regex<B, T>& e,
  48. boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  49. {
  50. return ::boost::regex_match(s.GetString(),
  51. s.GetString() + s.GetLength(),
  52. what,
  53. e,
  54. f);
  55. }
  56. template <class B, bool b, class T>
  57. inline bool regex_match(const ATL::CSimpleStringT<B, b>& s,
  58. const basic_regex<B, T>& e,
  59. boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  60. {
  61. return ::boost::regex_match(s.GetString(),
  62. s.GetString() + s.GetLength(),
  63. e,
  64. f);
  65. }
  66. //
  67. // regex_search overloads:
  68. //
  69. template <class B, bool b, class A, class T>
  70. inline bool regex_search(const ATL::CSimpleStringT<B, b>& s,
  71. match_results<const B*, A>& what,
  72. const basic_regex<B, T>& e,
  73. boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  74. {
  75. return ::boost::regex_search(s.GetString(),
  76. s.GetString() + s.GetLength(),
  77. what,
  78. e,
  79. f);
  80. }
  81. template <class B, bool b, class T>
  82. inline bool regex_search(const ATL::CSimpleStringT<B, b>& s,
  83. const basic_regex<B, T>& e,
  84. boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  85. {
  86. return ::boost::regex_search(s.GetString(),
  87. s.GetString() + s.GetLength(),
  88. e,
  89. f);
  90. }
  91. //
  92. // regex_iterator creation:
  93. //
  94. template <class B, bool b>
  95. inline regex_iterator<B const*>
  96. make_regex_iterator(const ATL::CSimpleStringT<B, b>& s, const basic_regex<B>& e, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  97. {
  98. regex_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, f);
  99. return result;
  100. }
  101. template <class B, bool b>
  102. inline regex_token_iterator<B const*>
  103. make_regex_token_iterator(const ATL::CSimpleStringT<B, b>& s, const basic_regex<B>& e, int sub = 0, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  104. {
  105. regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, sub, f);
  106. return result;
  107. }
  108. template <class B, bool b>
  109. inline regex_token_iterator<B const*>
  110. make_regex_token_iterator(const ATL::CSimpleStringT<B, b>& s, const basic_regex<B>& e, const std::vector<int>& subs, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  111. {
  112. regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);
  113. return result;
  114. }
  115. template <class B, bool b, std::size_t N>
  116. inline regex_token_iterator<B const*>
  117. make_regex_token_iterator(const ATL::CSimpleStringT<B, b>& s, const basic_regex<B>& e, const int (& subs)[N], ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  118. {
  119. regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);
  120. return result;
  121. }
  122. template <class OutputIterator, class BidirectionalIterator, class traits,
  123. class B, bool b>
  124. OutputIterator regex_replace(OutputIterator out,
  125. BidirectionalIterator first,
  126. BidirectionalIterator last,
  127. const basic_regex<B, traits>& e,
  128. const ATL::CSimpleStringT<B, b>& fmt,
  129. match_flag_type flags = match_default)
  130. {
  131. return ::boost::regex_replace(out, first, last, e, fmt.GetString(), flags);
  132. }
  133. namespace BOOST_REGEX_DETAIL_NS{
  134. template <class B, bool b>
  135. class mfc_string_out_iterator
  136. {
  137. ATL::CSimpleStringT<B, b>* out;
  138. public:
  139. mfc_string_out_iterator(ATL::CSimpleStringT<B, b>& s) : out(&s) {}
  140. mfc_string_out_iterator& operator++() { return *this; }
  141. mfc_string_out_iterator& operator++(int) { return *this; }
  142. mfc_string_out_iterator& operator*() { return *this; }
  143. mfc_string_out_iterator& operator=(B v)
  144. {
  145. out->AppendChar(v);
  146. return *this;
  147. }
  148. typedef std::ptrdiff_t difference_type;
  149. typedef B value_type;
  150. typedef value_type* pointer;
  151. typedef value_type& reference;
  152. typedef std::output_iterator_tag iterator_category;
  153. };
  154. }
  155. template <class traits, class B, bool b>
  156. ATL::CSimpleStringT<B, b> regex_replace(const ATL::CSimpleStringT<B, b>& s,
  157. const basic_regex<B, traits>& e,
  158. const ATL::CSimpleStringT<B, b>& fmt,
  159. match_flag_type flags = match_default)
  160. {
  161. ATL::CSimpleStringT<B, b> result(s.GetManager());
  162. BOOST_REGEX_DETAIL_NS::mfc_string_out_iterator<B, b> i(result);
  163. regex_replace(i, s.GetString(), s.GetString() + s.GetLength(), e, fmt.GetString(), flags);
  164. return result;
  165. }
  166. } // namespace boost.
  167. #endif