1 // boost/io/quoted_manip.hpp ---------------------------------------------------------//
3 // Copyright Beman Dawes 2010
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
8 // Library home page http://www.boost.org/libs/io
10 //--------------------------------------------------------------------------------------//
12 #ifndef BOOST_IO_QUOTED_MANIP
13 #define BOOST_IO_QUOTED_MANIP
19 #include <boost/io/ios_state.hpp>
25 namespace detail { template <class String, class Char> struct quoted_proxy; }
27 // ------------ public interface ------------------------------------------------//
29 // manipulator for const std::basic_string&
30 template <class Char, class Traits, class Alloc>
31 detail::quoted_proxy<std::basic_string<Char, Traits, Alloc> const &, Char>
32 quoted(const std::basic_string<Char, Traits, Alloc>& s,
33 Char escape='\\', Char delim='\"');
35 // manipulator for non-const std::basic_string&
36 template <class Char, class Traits, class Alloc>
37 detail::quoted_proxy<std::basic_string<Char, Traits, Alloc> &, Char>
38 quoted(std::basic_string<Char, Traits, Alloc>& s,
39 Char escape='\\', Char delim='\"');
41 // manipulator for const C-string*
43 detail::quoted_proxy<const Char*, Char>
44 quoted(const Char* s, Char escape='\\', Char delim='\"');
46 // ----------- implementation details -------------------------------------------//
50 // proxy used as an argument pack
51 template <class String, class Char>
58 quoted_proxy(String s_, Char escape_, Char delim_)
59 : string(s_), escape(escape_), delim(delim_) {}
61 // String may be a const type, so disable the assignment operator
62 quoted_proxy& operator=(const quoted_proxy&); // = deleted
65 // abstract away difference between proxies with const or non-const basic_strings
66 template <class Char, class Traits, class Alloc>
67 std::basic_ostream<Char, Traits>&
68 basic_string_inserter_imp(std::basic_ostream<Char, Traits>& os,
69 std::basic_string<Char, Traits, Alloc> const & string, Char escape, Char delim)
72 typename std::basic_string<Char, Traits, Alloc>::const_iterator
73 end_it = string.end();
74 for (typename std::basic_string<Char, Traits, Alloc>::const_iterator
79 if (*it == delim || *it == escape)
87 // inserter for const std::basic_string& proxies
88 template <class Char, class Traits, class Alloc>
90 std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
91 const quoted_proxy<std::basic_string<Char, Traits, Alloc> const &, Char>& proxy)
93 return basic_string_inserter_imp(os, proxy.string, proxy.escape, proxy.delim);
96 // inserter for non-const std::basic_string& proxies
97 template <class Char, class Traits, class Alloc>
99 std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
100 const quoted_proxy<std::basic_string<Char, Traits, Alloc>&, Char>& proxy)
102 return basic_string_inserter_imp(os, proxy.string, proxy.escape, proxy.delim);
105 // inserter for const C-string* proxies
106 template <class Char, class Traits>
107 std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
108 const quoted_proxy<const Char*, Char>& proxy)
111 for (const Char* it = proxy.string;
115 if (*it == proxy.delim || *it == proxy.escape)
123 // extractor for non-const std::basic_string& proxies
124 template <class Char, class Traits, class Alloc>
125 std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& is,
126 const quoted_proxy<std::basic_string<Char, Traits, Alloc>&, Char>& proxy)
128 proxy.string.clear();
131 if (c != proxy.delim)
138 boost::io::ios_flags_saver ifs(is);
143 if (!is.good()) // cope with I/O errors or end-of-file
145 if (c == proxy.escape)
148 if (!is.good()) // cope with I/O errors or end-of-file
151 else if (c == proxy.delim)
159 } // namespace detail
161 // manipulator implementation for const std::basic_string&
162 template <class Char, class Traits, class Alloc>
163 inline detail::quoted_proxy<std::basic_string<Char, Traits, Alloc> const &, Char>
164 quoted(const std::basic_string<Char, Traits, Alloc>& s, Char escape, Char delim)
166 return detail::quoted_proxy<std::basic_string<Char, Traits, Alloc> const &, Char>
170 // manipulator implementation for non-const std::basic_string&
171 template <class Char, class Traits, class Alloc>
172 inline detail::quoted_proxy<std::basic_string<Char, Traits, Alloc> &, Char>
173 quoted(std::basic_string<Char, Traits, Alloc>& s, Char escape, Char delim)
175 return detail::quoted_proxy<std::basic_string<Char, Traits, Alloc>&, Char>
179 // manipulator implementation for const C-string*
180 template <class Char>
181 inline detail::quoted_proxy<const Char*, Char>
182 quoted(const Char* s, Char escape, Char delim)
184 return detail::quoted_proxy<const Char*, Char> (s, escape, delim);
190 #endif // BOOST_IO_QUOTED_MANIP