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