source: NonGTP/Boost/boost/regex/mfc.hpp @ 857

Revision 857, 7.0 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]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
25namespace boost{
26
27//
28// define the types used for TCHAR's:
29typedef basic_regex<TCHAR> tregex;
30typedef match_results<TCHAR const*> tmatch;
31typedef regex_iterator<TCHAR const*> tregex_iterator;
32typedef 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//
45template <SIMPLE_STRING_PARAM>
46inline basic_regex<B>
47make_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//
55template <SIMPLE_STRING_PARAM, class A, class T>
56inline 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
68template <SIMPLE_STRING_PARAM, class T>
69inline 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//
81template <SIMPLE_STRING_PARAM, class A, class T>
82inline 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
94template <SIMPLE_STRING_PARAM, class T>
95inline 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//
107template <SIMPLE_STRING_PARAM>
108inline regex_iterator<B const*>
109make_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
115template <SIMPLE_STRING_PARAM>
116inline 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
123template <SIMPLE_STRING_PARAM>
124inline regex_token_iterator<B const*>
125make_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
131template <SIMPLE_STRING_PARAM, std::size_t N>
132inline regex_token_iterator<B const*>
133make_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
139template <class OutputIterator, class BidirectionalIterator, class traits,
140          SIMPLE_STRING_PARAM>
141OutputIterator 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
151namespace re_detail{
152
153template <SIMPLE_STRING_PARAM>
154class mfc_string_out_iterator
155{
156   ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>* out;
157public:
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
176template <class traits, SIMPLE_STRING_PARAM>
177ATL::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
Note: See TracBrowser for help on using the repository browser.