quoted_manip.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  7. <title>Boost &quot;quoted&quot; I/O manipulator</title>
  8. <meta name="generator" content="Microsoft FrontPage 5.0" />
  9. <link rel="stylesheet" type="text/css" href="../../../doc/src/minimal.css" />
  10. </head>
  11. <body>
  12. <table border="0" cellpadding="5" cellspacing="0"
  13. style="border-collapse: collapse">
  14. <tbody>
  15. <tr>
  16. <td width="277"><a href="../../../index.htm"><img
  17. src="../../../boost.png" alt="boost.png (6897 bytes)" align="middle"
  18. width="300" height="86" border="0" /></a></td>
  19. <td>
  20. <h1 align="center">&quot;Quoted&quot;
  21. I/O Manipulators<br>
  22. for Strings</h1>
  23. </td>
  24. </tr>
  25. </tbody>
  26. </table>
  27. <table border="1" cellpadding="5" cellspacing="1" style="border-collapse: collapse" bordercolor="#111111">
  28. <tr>
  29. <td>
  30. <p align="center"><b>&quot;Quoted&quot;
  31. I/O Manipulators
  32. for Strings are not yet accepted into Boost as public components. Thus the
  33. header file is currently located in &lt;boost/io/detail/quoted_manip.hpp&gt;</b></td>
  34. </tr>
  35. </table>
  36. <h2>Introduction</h2>
  37. <p>C++ Standard library stream I/O for strings that contain embedded spaces
  38. can produce unexpected results. For example,</p>
  39. <blockquote>
  40. <pre>std::stringstream ss;
  41. std::string original = &quot;fooled you&quot;;
  42. std::string round_trip;
  43. ss &lt;&lt; original;
  44. ss &gt;&gt; round_trip;
  45. std::cout &lt;&lt; original; // outputs: fooled you
  46. std::cout &lt;&lt; round_trip; // outputs: fooled
  47. assert(original == round_trip); // assert will fire</pre>
  48. </blockquote>
  49. <p>The Boost <code>quoted</code> stream I/O manipulator places delimiters, defaulted
  50. to the double-quote (<code>&quot;</code>), around strings on output, and strips off
  51. the delimiters on input. This ensures strings with embedded spaces round-trip as
  52. desired. For example,</p>
  53. <blockquote>
  54. <pre>std::stringstream ss;
  55. std::string original = &quot;fooled you&quot;;
  56. std::string round_trip;
  57. ss &lt;&lt; quoted(original);
  58. ss &gt;&gt; quoted(round_trip);
  59. std::cout &lt;&lt; quoted(original); // outputs: &quot;fooled you&quot;
  60. std::cout &lt;&lt; round_trip; // outputs: fooled you
  61. assert(original == round_trip); // assert will not fire</pre>
  62. </blockquote>
  63. <p>If the string contains the delimiter character, on output that character will
  64. be preceded by an escape character, as will the escape character itself:</p>
  65. <blockquote>
  66. <pre>std::cout &lt;&lt; quoted(&quot;'Jack &amp; Jill'&quot;, '&amp;', '\''); // outputs: '&amp;'Jack &amp;&amp; Jill&amp;''</pre>
  67. </blockquote>
  68. <h2>Header <a href="../../../boost/io/detail/quoted_manip.hpp">&lt;boost/io/quoted_manip.hpp&gt;</a> synopsis</h2>
  69. <pre>namespace boost
  70. {
  71. namespace io
  72. {
  73. // manipulator for const std::basic_string&amp;
  74. template &lt;class Char, class Traits, class Alloc&gt;
  75. <b><i>unspecified-type1</i></b> quoted(const std::basic_string&lt;Char, Traits, Alloc&gt;&amp; string,
  76. Char escape='\\', Char delim='\&quot;');
  77. // manipulator for const C-string*
  78. template &lt;class Char&gt;
  79. <b><i>unspecified-type2</i></b> quoted(const Char* string,
  80. Char escape='\\', Char delim='\&quot;');
  81. // manipulator for non-const std::basic_string&amp;
  82. template &lt;class Char, class Traits, class Alloc&gt;
  83. <b><i>unspecified-type3</i></b> quoted(std::basic_string&lt;Char, Traits, Alloc&gt;&amp; string,
  84. Char escape='\\', Char delim='\&quot;');
  85. }
  86. }</pre>
  87. <p><i><b><code>unspecified_type1</code></b></i>, <i><b><code>unspecified_type2</code></b></i>,
  88. and <i><b><code>unspecified_type3</code></b></i> are implementation supplied
  89. types with implementation supplied <code>operator&lt;&lt;</code>:</p>
  90. <blockquote>
  91. <pre>template &lt;class Char, class Traits&gt;
  92. std::basic_ostream&lt;Char, Traits&gt;&amp;
  93. operator&lt;&lt;(std::basic_ostream&lt;Char, Traits&gt;&amp; os, const <i><b><code>unspecified_typeN</code></b></i>&amp; proxy);</pre>
  94. <p><i>Effects:</i> Inserts characters into <code>os</code>:</p>
  95. <ul>
  96. <li><code>delim</code>.</li>
  97. <li>Each character in <code>string</code>. If the character to be output is
  98. equal to <code>escape</code> or <code>delim</code>, as determined by <code>
  99. operator==</code>, first output <code>escape</code>. </li>
  100. <li><code>delim</code>.</li>
  101. </ul>
  102. <p><i>Remarks:</i> <code>string</code>, <code>escape</code>, and <code>delim</code>
  103. have the type and value of the corresponding arguments of the call to the <code>
  104. quoted</code> function that constructed <code>proxy</code>.</p>
  105. <p><i>Returns:</i> <code>os</code>. </p>
  106. </blockquote>
  107. <p><i><b><code>unspecified_type3</code></b></i> is an implementation supplied
  108. type with an implementation supplied <code>operator&gt;&gt;</code>:</p>
  109. <blockquote>
  110. <pre>template &lt;class Char, class Traits&gt;
  111. std::basic_istream&lt;Char, Traits&gt;&amp;
  112. operator&gt;&gt;(std::basic_istream&lt;Char, Traits&gt;&amp; is, const <i><b><code>unspecified_type3</code></b></i>&amp; proxy);</pre>
  113. <p><i>Effects:</i> Extracts characters from <code>os</code>:</p>
  114. <ul>
  115. <li>If the first character extracted is equal to delim, as determined by
  116. <code>operator==</code>, then:<ul>
  117. <li>Turn off the <code>skipws</code> flag.</li>
  118. <li><code>string.clear()</code></li>
  119. <li>Until an unescaped <code>delim</code> character is reached or <code>
  120. is.not_good()</code>, extract
  121. characters from <code>os</code> and append them to <code>string</code>,
  122. except that if an <code>escape</code> is reached, ignore it and append the
  123. next character to <code>string</code>.</li>
  124. <li>Discard the final <code>delim</code> character.</li>
  125. <li>Restore the <code>skipws</code> flag to its original value.</li>
  126. </ul>
  127. </li>
  128. <li>Otherwise, <code>os &gt;&gt; string</code>.</li>
  129. </ul>
  130. <p><i>Remarks:</i> <code>string</code>, <code>escape</code>, and <code>delim</code>
  131. have the type and value of the corresponding arguments of the call to the <code>
  132. quoted</code> function that constructed <code>proxy</code>.</p>
  133. <p><i>Returns:</i> <code>is</code>. </p>
  134. </blockquote>
  135. <h2>Acknowledgements</h2>
  136. <p>The <code>quoted()</code> stream manipulator emerged from discussions on the
  137. Boost developers mailing list. Participants included Beman Dawes, Rob Stewart,
  138. Alexander Lamaison, Eric Niebler, Vicente Botet, Andrey Semashev, Phil Richards,
  139. and Rob Murray. Eric Niebler's suggestions provided the basis for the name and
  140. form of the templates. </p>
  141. <hr>
  142. <p>© Copyright Beman Dawes, 2002, 2006, 2007, 2009, 2010</p>
  143. <p>Distributed under the Boost Software License, Version 1.0. See
  144. <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
  145. <p>Revised
  146. <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B %Y" startspan -->08 March 2013<!--webbot bot="Timestamp" endspan i-checksum="27284" --></p>
  147. </body>
  148. </html>