| 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 | #include <boost/range/begin.hpp> |
| 16 | #include <boost/range/end.hpp> |
| 17 | |
| 18 | namespace boost { namespace spirit { namespace karma { namespace detail |
| 19 | { |
| 20 | /////////////////////////////////////////////////////////////////////////// |
| 21 | // pass through character transformation |
| 22 | struct pass_through_filter |
| 23 | { |
| 24 | template <typename Char> |
| 25 | Char operator()(Char ch) const |
| 26 | { |
| 27 | return ch; |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | template <typename CharEncoding, typename Tag> |
| 32 | struct encoding_filter |
| 33 | { |
| 34 | template <typename Char> |
| 35 | Char operator()(Char ch) const |
| 36 | { |
| 37 | return spirit::char_class::convert<CharEncoding>::to(Tag(), ch); |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | /////////////////////////////////////////////////////////////////////////// |
| 42 | // generate a string given by a std::string, applying the given filter |
| 43 | template <typename OutputIterator, typename Char, typename Filter> |
| 44 | inline bool string_generate(OutputIterator& sink, Char const* str |
| 45 | , Filter filter) |
| 46 | { |
| 47 | for (Char ch = *str; ch != 0; ch = *++str) |
| 48 | { |
| 49 | *sink = filter(ch); |
| 50 | ++sink; |
| 51 | } |
| 52 | return detail::sink_is_good(sink); |
| 53 | } |
| 54 | |
| 55 | template <typename OutputIterator, typename Container, typename Filter> |
| 56 | inline bool string_generate(OutputIterator& sink |
| 57 | , Container const& c, Filter filter) |
| 58 | { |
| 59 | typedef typename traits::container_iterator<Container const>::type |
| 60 | iterator; |
| 61 | |
| 62 | const iterator end = boost::end(c); |
| 63 | for (iterator it = boost::begin(c); it != end; ++it) |
| 64 | { |
| 65 | *sink = filter(*it); |
| 66 | ++sink; |
| 67 | } |
| 68 | return detail::sink_is_good(sink); |
| 69 | } |
| 70 | |
| 71 | /////////////////////////////////////////////////////////////////////////// |
| 72 | // generate a string without any transformation |
| 73 | template <typename OutputIterator, typename Char> |
| 74 | inline bool string_generate(OutputIterator& sink, Char const* str) |
| 75 | { |
| 76 | return string_generate(sink, str, pass_through_filter()); |
| 77 | } |
| 78 | |
| 79 | template <typename OutputIterator, typename Container> |
| 80 | inline bool string_generate(OutputIterator& sink |
| 81 | , Container const& c) |
| 82 | { |
| 83 | return string_generate(sink, c, pass_through_filter()); |
| 84 | } |
| 85 | |
| 86 | /////////////////////////////////////////////////////////////////////////// |
| 87 | // generate a string given by a pointer, converting according using a |
| 88 | // given character class and case tag |
| 89 | template <typename OutputIterator, typename Char, typename CharEncoding |
| 90 | , typename Tag> |
| 91 | inline bool string_generate(OutputIterator& sink |
| 92 | , Char const* str |
| 93 | , CharEncoding, Tag) |
| 94 | { |
| 95 | return string_generate(sink, str, encoding_filter<CharEncoding, Tag>()); |
| 96 | } |
| 97 | |
| 98 | template <typename OutputIterator, typename Container |
| 99 | , typename CharEncoding, typename Tag> |
| 100 | inline bool |
| 101 | string_generate(OutputIterator& sink |
| 102 | , Container const& c |
| 103 | , CharEncoding, Tag) |
| 104 | { |
| 105 | return string_generate(sink, c, encoding_filter<CharEncoding, Tag>()); |
| 106 | } |
| 107 | |
| 108 | /////////////////////////////////////////////////////////////////////////// |
| 109 | template <typename OutputIterator, typename Char> |
| 110 | inline bool string_generate(OutputIterator& sink |
| 111 | , Char const* str |
| 112 | , unused_type, unused_type) |
| 113 | { |
| 114 | return string_generate(sink, str); |
| 115 | } |
| 116 | |
| 117 | template <typename OutputIterator, typename Container> |
| 118 | inline bool string_generate(OutputIterator& sink |
| 119 | , Container const& c |
| 120 | , unused_type, unused_type) |
| 121 | { |
| 122 | return string_generate(sink, c); |
| 123 | } |
| 124 | |
| 125 | }}}} |
| 126 | |
| 127 | #endif |
| 128 | |