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