1 // Copyright (c) 2001-2011 Hartmut Kaiser 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #if !defined(BOOST_SPIRIT_KARMA_STRING_GENERATE_FEB_23_2007_1232PM) 7 #define BOOST_SPIRIT_KARMA_STRING_GENERATE_FEB_23_2007_1232PM 8 9 #if defined(_MSC_VER) 10 #pragma once 11 #endif 12 13 #include <boost/spirit/home/support/char_class.hpp> 14 #include <boost/spirit/home/karma/detail/generate_to.hpp> 15 16 namespace boost { namespace spirit { namespace karma { namespace detail 17 { 18 /////////////////////////////////////////////////////////////////////////// 19 // pass through character transformation 20 struct pass_through_filter 21 { 22 template <typename Char> operator ()boost::spirit::karma::detail::pass_through_filter23 Char operator()(Char ch) const 24 { 25 return ch; 26 } 27 }; 28 29 template <typename CharEncoding, typename Tag> 30 struct encoding_filter 31 { 32 template <typename Char> operator ()boost::spirit::karma::detail::encoding_filter33 Char operator()(Char ch) const 34 { 35 return spirit::char_class::convert<CharEncoding>::to(Tag(), ch); 36 } 37 }; 38 39 /////////////////////////////////////////////////////////////////////////// 40 // generate a string given by a std::string, applying the given filter 41 template <typename OutputIterator, typename Char, typename Filter> string_generate(OutputIterator & sink,Char const * str,Filter filter)42 inline bool string_generate(OutputIterator& sink, Char const* str 43 , Filter filter) 44 { 45 for (Char ch = *str; ch != 0; ch = *++str) 46 { 47 *sink = filter(ch); 48 ++sink; 49 } 50 return detail::sink_is_good(sink); 51 } 52 53 template <typename OutputIterator, typename Container, typename Filter> string_generate(OutputIterator & sink,Container const & c,Filter filter)54 inline bool string_generate(OutputIterator& sink 55 , Container const& c, Filter filter) 56 { 57 typedef typename traits::container_iterator<Container const>::type 58 iterator; 59 60 const iterator end = boost::end(c); 61 for (iterator it = boost::begin(c); it != end; ++it) 62 { 63 *sink = filter(*it); 64 ++sink; 65 } 66 return detail::sink_is_good(sink); 67 } 68 69 /////////////////////////////////////////////////////////////////////////// 70 // generate a string without any transformation 71 template <typename OutputIterator, typename Char> string_generate(OutputIterator & sink,Char const * str)72 inline bool string_generate(OutputIterator& sink, Char const* str) 73 { 74 return string_generate(sink, str, pass_through_filter()); 75 } 76 77 template <typename OutputIterator, typename Container> string_generate(OutputIterator & sink,Container const & c)78 inline bool string_generate(OutputIterator& sink 79 , Container const& c) 80 { 81 return string_generate(sink, c, pass_through_filter()); 82 } 83 84 /////////////////////////////////////////////////////////////////////////// 85 // generate a string given by a pointer, converting according using a 86 // given character class and case tag 87 template <typename OutputIterator, typename Char, typename CharEncoding 88 , typename Tag> string_generate(OutputIterator & sink,Char const * str,CharEncoding,Tag)89 inline bool string_generate(OutputIterator& sink 90 , Char const* str 91 , CharEncoding, Tag) 92 { 93 return string_generate(sink, str, encoding_filter<CharEncoding, Tag>()); 94 } 95 96 template <typename OutputIterator, typename Container 97 , typename CharEncoding, typename Tag> 98 inline bool string_generate(OutputIterator & sink,Container const & c,CharEncoding,Tag)99 string_generate(OutputIterator& sink 100 , Container const& c 101 , CharEncoding, Tag) 102 { 103 return string_generate(sink, c, encoding_filter<CharEncoding, Tag>()); 104 } 105 106 /////////////////////////////////////////////////////////////////////////// 107 template <typename OutputIterator, typename Char> string_generate(OutputIterator & sink,Char const * str,unused_type,unused_type)108 inline bool string_generate(OutputIterator& sink 109 , Char const* str 110 , unused_type, unused_type) 111 { 112 return string_generate(sink, str); 113 } 114 115 template <typename OutputIterator, typename Container> string_generate(OutputIterator & sink,Container const & c,unused_type,unused_type)116 inline bool string_generate(OutputIterator& sink 117 , Container const& c 118 , unused_type, unused_type) 119 { 120 return string_generate(sink, c); 121 } 122 123 }}}} 124 125 #endif 126