mfc.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #if _MSC_VER >= 1310
  29. #define SIMPLE_STRING_PARAM class B, bool b
  30. #define SIMPLE_STRING_ARG_LIST B, b
  31. #else
  32. #define SIMPLE_STRING_PARAM class B
  33. #define SIMPLE_STRING_ARG_LIST B
  34. #endif
  35. //
  36. // define regex creation functions:
  37. //
  38. template <SIMPLE_STRING_PARAM>
  39. inline basic_regex<B>
  40. make_regex(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s, ::boost::regex_constants::syntax_option_type f = boost::regex_constants::normal)
  41. {
  42. basic_regex<B> result(s.GetString(), s.GetString() + s.GetLength(), f);
  43. return result;
  44. }
  45. //
  46. // regex_match overloads:
  47. //
  48. template <SIMPLE_STRING_PARAM, class A, class T>
  49. inline bool regex_match(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s,
  50. match_results<const B*, A>& what,
  51. const basic_regex<B, T>& e,
  52. boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  53. {
  54. return ::boost::regex_match(s.GetString(),
  55. s.GetString() + s.GetLength(),
  56. what,
  57. e,
  58. f);
  59. }
  60. template <SIMPLE_STRING_PARAM, class T>
  61. inline bool regex_match(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s,
  62. const basic_regex<B, T>& e,
  63. boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  64. {
  65. return ::boost::regex_match(s.GetString(),
  66. s.GetString() + s.GetLength(),
  67. e,
  68. f);
  69. }
  70. //
  71. // regex_search overloads:
  72. //
  73. template <SIMPLE_STRING_PARAM, class A, class T>
  74. inline bool regex_search(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s,
  75. match_results<const B*, A>& what,
  76. const basic_regex<B, T>& e,
  77. boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  78. {
  79. return ::boost::regex_search(s.GetString(),
  80. s.GetString() + s.GetLength(),
  81. what,
  82. e,
  83. f);
  84. }
  85. template <SIMPLE_STRING_PARAM, class T>
  86. inline bool regex_search(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s,
  87. const basic_regex<B, T>& e,
  88. boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  89. {
  90. return ::boost::regex_search(s.GetString(),
  91. s.GetString() + s.GetLength(),
  92. e,
  93. f);
  94. }
  95. //
  96. // regex_iterator creation:
  97. //
  98. template <SIMPLE_STRING_PARAM>
  99. inline regex_iterator<B const*>
  100. make_regex_iterator(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s, const basic_regex<B>& e, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  101. {
  102. regex_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, f);
  103. return result;
  104. }
  105. template <SIMPLE_STRING_PARAM>
  106. inline regex_token_iterator<B const*>
  107. make_regex_token_iterator(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s, const basic_regex<B>& e, int sub = 0, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  108. {
  109. regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, sub, f);
  110. return result;
  111. }
  112. template <SIMPLE_STRING_PARAM>
  113. inline regex_token_iterator<B const*>
  114. make_regex_token_iterator(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s, const basic_regex<B>& e, const std::vector<int>& subs, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  115. {
  116. regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);
  117. return result;
  118. }
  119. template <SIMPLE_STRING_PARAM, std::size_t N>
  120. inline regex_token_iterator<B const*>
  121. make_regex_token_iterator(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s, const basic_regex<B>& e, const int (& subs)[N], ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
  122. {
  123. regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);
  124. return result;
  125. }
  126. template <class OutputIterator, class BidirectionalIterator, class traits,
  127. SIMPLE_STRING_PARAM>
  128. OutputIterator regex_replace(OutputIterator out,
  129. BidirectionalIterator first,
  130. BidirectionalIterator last,
  131. const basic_regex<B, traits>& e,
  132. const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& fmt,
  133. match_flag_type flags = match_default)
  134. {
  135. return ::boost::regex_replace(out, first, last, e, fmt.GetString(), flags);
  136. }
  137. namespace re_detail{
  138. template <SIMPLE_STRING_PARAM>
  139. class mfc_string_out_iterator
  140. {
  141. ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>* out;
  142. public:
  143. mfc_string_out_iterator(ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s) : out(&s) {}
  144. mfc_string_out_iterator& operator++() { return *this; }
  145. mfc_string_out_iterator& operator++(int) { return *this; }
  146. mfc_string_out_iterator& operator*() { return *this; }
  147. mfc_string_out_iterator& operator=(B v)
  148. {
  149. out->AppendChar(v);
  150. return *this;
  151. }
  152. typedef std::ptrdiff_t difference_type;
  153. typedef B value_type;
  154. typedef value_type* pointer;
  155. typedef value_type& reference;
  156. typedef std::output_iterator_tag iterator_category;
  157. };
  158. }
  159. template <class traits, SIMPLE_STRING_PARAM>
  160. ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST> regex_replace(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s,
  161. const basic_regex<B, traits>& e,
  162. const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& fmt,
  163. match_flag_type flags = match_default)
  164. {
  165. ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST> result(s.GetManager());
  166. re_detail::mfc_string_out_iterator<SIMPLE_STRING_ARG_LIST> i(result);
  167. regex_replace(i, s.GetString(), s.GetString() + s.GetLength(), e, fmt.GetString(), flags);
  168. return result;
  169. }
  170. } // namespace boost.
  171. #endif