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