quoted.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Copyright 2010 Beman Dawes
  3. Copyright 2019-2020 Glen Joseph Fernandes
  4. (glenjofe@gmail.com)
  5. Distributed under the Boost Software License, Version 1.0.
  6. (http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_IO_QUOTED_HPP
  9. #define BOOST_IO_QUOTED_HPP
  10. #include <boost/io/detail/buffer_fill.hpp>
  11. #include <boost/io/detail/ostream_guard.hpp>
  12. #include <boost/io/ios_state.hpp>
  13. namespace boost {
  14. namespace io {
  15. namespace detail {
  16. template<class String, class Char>
  17. struct quoted_proxy {
  18. String string;
  19. Char escape;
  20. Char delim;
  21. };
  22. template<class Char>
  23. struct quoted_state {
  24. const Char* string;
  25. std::size_t size;
  26. std::size_t count;
  27. };
  28. template<class Char>
  29. inline quoted_state<Char>
  30. quoted_start(const Char* string, Char escape, Char delim)
  31. {
  32. const Char* end = string;
  33. std::size_t count = 2;
  34. for (Char ch; (ch = *end) != 0; ++end) {
  35. count += 1 + (ch == escape || ch == delim);
  36. }
  37. quoted_state<Char> state = { string,
  38. static_cast<std::size_t>(end - string), count };
  39. return state;
  40. }
  41. template<class Char, class String>
  42. inline quoted_state<Char>
  43. quoted_start(const String* string, Char escape, Char delim)
  44. {
  45. const Char* begin = string->data();
  46. std::size_t size = string->size();
  47. std::size_t count = 2;
  48. for (const Char *it = begin, *end = begin + size; it != end; ++it) {
  49. Char ch = *it;
  50. count += 1 + (ch == escape || ch == delim);
  51. }
  52. quoted_state<Char> state = { begin, size, count };
  53. return state;
  54. }
  55. template<class Char, class Traits>
  56. inline bool
  57. quoted_put(std::basic_streambuf<Char, Traits>& buf, const Char* string,
  58. std::size_t size, std::size_t count, Char escape, Char delim)
  59. {
  60. if (buf.sputc(delim) == Traits::eof()) {
  61. return false;
  62. }
  63. if (size == count) {
  64. if (static_cast<std::size_t>(buf.sputn(string, size)) != size) {
  65. return false;
  66. }
  67. } else {
  68. for (const Char* end = string + size; string != end; ++string) {
  69. Char ch = *string;
  70. if ((ch == escape || ch == delim) &&
  71. buf.sputc(escape) == Traits::eof()) {
  72. return false;
  73. }
  74. if (buf.sputc(ch) == Traits::eof()) {
  75. return false;
  76. }
  77. }
  78. }
  79. return buf.sputc(delim) != Traits::eof();
  80. }
  81. template<class Char, class Traits, class String>
  82. inline std::basic_ostream<Char, Traits>&
  83. quoted_out(std::basic_ostream<Char, Traits>& os, String* string, Char escape,
  84. Char delim)
  85. {
  86. typedef std::basic_ostream<Char, Traits> stream;
  87. ostream_guard<Char, Traits> guard(os);
  88. typename stream::sentry entry(os);
  89. if (entry) {
  90. quoted_state<Char> state = boost::io::detail::quoted_start(string,
  91. escape, delim);
  92. std::basic_streambuf<Char, Traits>& buf = *os.rdbuf();
  93. std::size_t width = static_cast<std::size_t>(os.width());
  94. if (width <= state.count) {
  95. if (!boost::io::detail::quoted_put(buf, state.string, state.size,
  96. state.count, escape, delim)) {
  97. return os;
  98. }
  99. } else if ((os.flags() & stream::adjustfield) == stream::left) {
  100. if (!boost::io::detail::quoted_put(buf, state.string, state.size,
  101. state.count, escape, delim) ||
  102. !boost::io::detail::buffer_fill(buf, os.fill(),
  103. width - state.count)) {
  104. return os;
  105. }
  106. } else if (!boost::io::detail::buffer_fill(buf, os.fill(),
  107. width - state.count) ||
  108. !boost::io::detail::quoted_put(buf, state.string, state.size,
  109. state.count, escape, delim)) {
  110. return os;
  111. }
  112. os.width(0);
  113. }
  114. guard.release();
  115. return os;
  116. }
  117. template<class Char, class Traits>
  118. inline std::basic_ostream<Char, Traits>&
  119. operator<<(std::basic_ostream<Char, Traits>& os,
  120. const quoted_proxy<const Char*, Char>& proxy)
  121. {
  122. return boost::io::detail::quoted_out(os, proxy.string, proxy.escape,
  123. proxy.delim);
  124. }
  125. template <class Char, class Traits, class Alloc>
  126. inline std::basic_ostream<Char, Traits>&
  127. operator<<(std::basic_ostream<Char, Traits>& os,
  128. const quoted_proxy<const std::basic_string<Char, Traits, Alloc>*,
  129. Char>& proxy)
  130. {
  131. return boost::io::detail::quoted_out(os, proxy.string, proxy.escape,
  132. proxy.delim);
  133. }
  134. template<class Char, class Traits, class Alloc>
  135. inline std::basic_ostream<Char, Traits>&
  136. operator<<(std::basic_ostream<Char, Traits>& os,
  137. const quoted_proxy<std::basic_string<Char, Traits, Alloc>*, Char>& proxy)
  138. {
  139. return boost::io::detail::quoted_out(os, proxy.string, proxy.escape,
  140. proxy.delim);
  141. }
  142. template<class Char, class Traits, class Alloc>
  143. inline std::basic_istream<Char, Traits>&
  144. operator>>(std::basic_istream<Char, Traits>& is,
  145. const quoted_proxy<std::basic_string<Char, Traits, Alloc>*, Char>& proxy)
  146. {
  147. Char ch;
  148. if (!(is >> ch)) {
  149. return is;
  150. }
  151. if (ch != proxy.delim) {
  152. is.unget();
  153. return is >> *proxy.string;
  154. }
  155. {
  156. boost::io::ios_flags_saver ifs(is);
  157. std::noskipws(is);
  158. proxy.string->clear();
  159. while ((is >> ch) && ch != proxy.delim) {
  160. if (ch == proxy.escape && !(is >> ch)) {
  161. break;
  162. }
  163. proxy.string->push_back(ch);
  164. }
  165. }
  166. return is;
  167. }
  168. } /* detail */
  169. template<class Char, class Traits, class Alloc>
  170. inline detail::quoted_proxy<const std::basic_string<Char, Traits, Alloc>*,
  171. Char>
  172. quoted(const std::basic_string<Char, Traits, Alloc>& s, Char escape='\\',
  173. Char delim='\"')
  174. {
  175. detail::quoted_proxy<const std::basic_string<Char, Traits, Alloc>*,
  176. Char> proxy = { &s, escape, delim };
  177. return proxy;
  178. }
  179. template<class Char, class Traits, class Alloc>
  180. inline detail::quoted_proxy<std::basic_string<Char, Traits, Alloc>*, Char>
  181. quoted(std::basic_string<Char, Traits, Alloc>& s, Char escape='\\',
  182. Char delim='\"')
  183. {
  184. detail::quoted_proxy<std::basic_string<Char, Traits, Alloc>*,
  185. Char> proxy = { &s, escape, delim };
  186. return proxy;
  187. }
  188. template<class Char>
  189. inline detail::quoted_proxy<const Char*, Char>
  190. quoted(const Char* s, Char escape='\\', Char delim='\"')
  191. {
  192. detail::quoted_proxy<const Char*, Char> proxy = { s, escape, delim };
  193. return proxy;
  194. }
  195. } /* io */
  196. } /* boost */
  197. #endif