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