Chris@16: /* Chris@16: * Chris@16: * Copyright (c) 2004 Chris@16: * John Maddock Chris@16: * Chris@16: * Use, modification and distribution are subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying file Chris@16: * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: */ Chris@16: Chris@16: /* Chris@16: * LOCATION: see http://www.boost.org for most recent version. Chris@16: * FILE mfc.hpp Chris@16: * VERSION see Chris@16: * DESCRIPTION: Overloads and helpers for using MFC/ATL string types with Boost.Regex. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_REGEX_MFC_HPP Chris@16: #define BOOST_REGEX_MFC_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ Chris@16: Chris@16: // Chris@16: // define the types used for TCHAR's: Chris@16: typedef basic_regex tregex; Chris@16: typedef match_results tmatch; Chris@16: typedef regex_iterator tregex_iterator; Chris@16: typedef regex_token_iterator tregex_token_iterator; Chris@16: Chris@101: // Obsolete. Remove Chris@16: #define SIMPLE_STRING_PARAM class B, bool b Chris@16: #define SIMPLE_STRING_ARG_LIST B, b Chris@16: Chris@16: // Chris@16: // define regex creation functions: Chris@16: // Chris@101: template Chris@16: inline basic_regex Chris@101: make_regex(const ATL::CSimpleStringT& s, ::boost::regex_constants::syntax_option_type f = boost::regex_constants::normal) Chris@16: { Chris@16: basic_regex result(s.GetString(), s.GetString() + s.GetLength(), f); Chris@16: return result; Chris@16: } Chris@16: // Chris@16: // regex_match overloads: Chris@16: // Chris@101: template Chris@101: inline bool regex_match(const ATL::CSimpleStringT& s, Chris@16: match_results& what, Chris@16: const basic_regex& e, Chris@16: boost::regex_constants::match_flag_type f = boost::regex_constants::match_default) Chris@16: { Chris@16: return ::boost::regex_match(s.GetString(), Chris@16: s.GetString() + s.GetLength(), Chris@16: what, Chris@16: e, Chris@16: f); Chris@16: } Chris@16: Chris@101: template Chris@101: inline bool regex_match(const ATL::CSimpleStringT& s, Chris@16: const basic_regex& e, Chris@16: boost::regex_constants::match_flag_type f = boost::regex_constants::match_default) Chris@16: { Chris@16: return ::boost::regex_match(s.GetString(), Chris@16: s.GetString() + s.GetLength(), Chris@16: e, Chris@16: f); Chris@16: } Chris@16: // Chris@16: // regex_search overloads: Chris@16: // Chris@101: template Chris@101: inline bool regex_search(const ATL::CSimpleStringT& s, Chris@16: match_results& what, Chris@16: const basic_regex& e, Chris@16: boost::regex_constants::match_flag_type f = boost::regex_constants::match_default) Chris@16: { Chris@16: return ::boost::regex_search(s.GetString(), Chris@16: s.GetString() + s.GetLength(), Chris@16: what, Chris@16: e, Chris@16: f); Chris@16: } Chris@16: Chris@101: template Chris@101: inline bool regex_search(const ATL::CSimpleStringT& s, Chris@16: const basic_regex& e, Chris@16: boost::regex_constants::match_flag_type f = boost::regex_constants::match_default) Chris@16: { Chris@16: return ::boost::regex_search(s.GetString(), Chris@16: s.GetString() + s.GetLength(), Chris@16: e, Chris@16: f); Chris@16: } Chris@16: // Chris@16: // regex_iterator creation: Chris@16: // Chris@101: template Chris@16: inline regex_iterator Chris@101: make_regex_iterator(const ATL::CSimpleStringT& s, const basic_regex& e, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default) Chris@16: { Chris@16: regex_iterator result(s.GetString(), s.GetString() + s.GetLength(), e, f); Chris@16: return result; Chris@16: } Chris@16: Chris@101: template Chris@16: inline regex_token_iterator Chris@101: make_regex_token_iterator(const ATL::CSimpleStringT& s, const basic_regex& e, int sub = 0, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default) Chris@16: { Chris@16: regex_token_iterator result(s.GetString(), s.GetString() + s.GetLength(), e, sub, f); Chris@16: return result; Chris@16: } Chris@16: Chris@101: template Chris@16: inline regex_token_iterator Chris@101: make_regex_token_iterator(const ATL::CSimpleStringT& s, const basic_regex& e, const std::vector& subs, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default) Chris@16: { Chris@16: regex_token_iterator result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f); Chris@16: return result; Chris@16: } Chris@16: Chris@101: template Chris@16: inline regex_token_iterator Chris@101: make_regex_token_iterator(const ATL::CSimpleStringT& s, const basic_regex& e, const int (& subs)[N], ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default) Chris@16: { Chris@16: regex_token_iterator result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f); Chris@16: return result; Chris@16: } Chris@16: Chris@16: template Chris@16: OutputIterator regex_replace(OutputIterator out, Chris@16: BidirectionalIterator first, Chris@16: BidirectionalIterator last, Chris@16: const basic_regex& e, Chris@101: const ATL::CSimpleStringT& fmt, Chris@16: match_flag_type flags = match_default) Chris@16: { Chris@16: return ::boost::regex_replace(out, first, last, e, fmt.GetString(), flags); Chris@16: } Chris@16: Chris@16: namespace re_detail{ Chris@16: Chris@101: template Chris@16: class mfc_string_out_iterator Chris@16: { Chris@101: ATL::CSimpleStringT* out; Chris@16: public: Chris@101: mfc_string_out_iterator(ATL::CSimpleStringT& s) : out(&s) {} Chris@16: mfc_string_out_iterator& operator++() { return *this; } Chris@16: mfc_string_out_iterator& operator++(int) { return *this; } Chris@16: mfc_string_out_iterator& operator*() { return *this; } Chris@16: mfc_string_out_iterator& operator=(B v) Chris@16: { Chris@16: out->AppendChar(v); Chris@16: return *this; Chris@16: } Chris@16: typedef std::ptrdiff_t difference_type; Chris@16: typedef B value_type; Chris@16: typedef value_type* pointer; Chris@16: typedef value_type& reference; Chris@16: typedef std::output_iterator_tag iterator_category; Chris@16: }; Chris@16: Chris@16: } Chris@16: Chris@101: template Chris@101: ATL::CSimpleStringT regex_replace(const ATL::CSimpleStringT& s, Chris@16: const basic_regex& e, Chris@101: const ATL::CSimpleStringT& fmt, Chris@16: match_flag_type flags = match_default) Chris@16: { Chris@101: ATL::CSimpleStringT result(s.GetManager()); Chris@101: re_detail::mfc_string_out_iterator i(result); Chris@16: regex_replace(i, s.GetString(), s.GetString() + s.GetLength(), e, fmt.GetString(), flags); Chris@16: return result; Chris@16: } Chris@16: Chris@16: } // namespace boost. Chris@16: Chris@16: #endif