Use boost::io::quoted I/O manipulator as a better solution to embedded spaces. See #3863

[SVN r63136]
This commit is contained in:
Beman Dawes
2010-06-20 13:41:26 +00:00
parent 77b673eba7
commit 998233d767
4 changed files with 53 additions and 46 deletions

View File

@@ -21,11 +21,12 @@
#include <boost/system/system_error.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/io/detail/quoted_manip.hpp>
#include <boost/static_assert.hpp>
#include <string>
#include <iterator>
#include <cstring>
#include <iosfwd> // needed by basic_path inserter and extractor
#include <iosfwd>
#include <stdexcept>
#include <cassert>
#include <locale>
@@ -542,35 +543,27 @@ namespace filesystem3
inline path operator/(const path& lhs, const path& rhs) { return path(lhs) /= rhs; }
// inserters and extractors
// use boost::io::quoted() to handle spaces in paths
// use '&' as escape character to ease use for Windows paths
inline std::ostream& operator<<(std::ostream & os, const path& p)
template <class Char, class Traits>
inline std::basic_ostream<Char, Traits>&
operator<<(std::basic_ostream<Char, Traits>& os, const path& p)
{
os << p.string();
return os;
return os
<< boost::io::quoted(p.string<std::basic_string<Char> >(), static_cast<Char>('&'));
}
inline std::wostream& operator<<(std::wostream & os, const path& p)
template <class Char, class Traits>
inline std::basic_istream<Char, Traits>&
operator>>(std::basic_istream<Char, Traits>& is, path& p)
{
os << p.wstring();
return os;
}
inline std::istream& operator>>(std::istream & is, path& p)
{
std::string str;
std::getline(is, str); // See ticket #3863
std::basic_string<Char> str;
is >> boost::io::quoted(str, static_cast<Char>('&'));
p = str;
return is;
}
inline std::wistream& operator>>(std::wistream & is, path& p)
{
std::wstring str;
std::getline(is, str); // See ticket #3863
p = str;
return is;
}
// name_checks
BOOST_FILESYSTEM_DECL bool portable_posix_name(const std::string & name);

View File

@@ -1177,36 +1177,30 @@ const string_type external_directory_string() const { return native(); }</pre>
<blockquote>
<p><i>Returns:</i> <code>path(lhs) /= rhs</code>.</p>
</blockquote>
<h3> <a name="path-non-member-operators"><code><font size="4">path</font></code></a><a name="path-inserter-extractor"><span style="background-color: #FFFFFF"> inserters
and extractor</span></a><span style="background-color: #FFFFFF">s</span></h3>
<pre>std::ostream&amp; operator&lt;&lt;(std::ostream &amp; os, const path&amp; p);</pre>
<h3> <a name="path-non-member-operators"><code><font size="4">path</font></code></a><a name="path-inserter-extractor"><span style="background-color: #FFFFFF"> inserter
and extractor</span></a></h3>
<p> The inserter and extractor delimit the string with double-quotes (<code>&quot;</code>)
to ensure that paths with embedded spaces will round trip correctly. Ampersand (<code>&amp;</code>)
is used as an escape character, so the path can itself contain double quotes.</p>
<pre>template &lt;class Char, class Traits&gt;
std::basic_ostream&lt;Char, Traits&gt;&amp; operator&lt;&lt;(std::basic_ostream&lt;Char, Traits&gt;&amp; os, const path&amp; p)
</pre>
<blockquote>
<p><i><span style="background-color: #FFFFFF">Effects:</span></i><span style="background-color: #FFFFFF">&nbsp;
<code>os &lt;&lt; p.string();</code></span></p>
<code>os &lt;&lt; <a href="../../../io/doc/quoted_manip.html">
boost::io::quoted</a>(p.string&lt;std::basic_string&lt;Char&gt;&gt;(), static_cast&lt;Char&gt;('&amp;'));</code></span></p>
<p><i><span style="background-color: #FFFFFF">Returns:</span></i><span style="background-color: #FFFFFF">
</span> <code><span style="background-color: #FFFFFF">os</span></code></p>
</blockquote>
<pre>std::wostream&amp; operator&lt;&lt;(std::wostream &amp; os, const path&amp; p);</pre>
<blockquote style="font-size: 10pt">
<p style="font-size: 10pt"><i><span style="background-color: #FFFFFF">Effects:</span></i><span style="background-color: #FFFFFF">&nbsp;
<code>os &lt;&lt; p.wstring();</code></span></p>
<p style="font-size: 10pt"><i><span style="background-color: #FFFFFF">Returns:</span></i><span style="background-color: #FFFFFF">
</span><code><span style="background-color: #FFFFFF">os</span></code></p>
</blockquote>
<pre>std::istream&amp; operator&gt;&gt;(std::istream &amp; is, path&amp; p);</pre>
<pre>template &lt;class Char, class Traits&gt;
inline std::basic_istream&lt;Char, Traits&gt;&amp; operator&gt;&gt;(std::basic_istream&lt;Char, Traits&gt;&amp; is, path&amp; p)
</pre>
<blockquote>
<p><span style="background-color: #FFFFFF"><i>Effects: </i>
<code>&nbsp; std::string str;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is &gt;&gt; str;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p = str;</code></span></p>
<p><i><span style="background-color: #FFFFFF">Returns:</span></i><span style="background-color: #FFFFFF">
</span> <code><span style="background-color: #FFFFFF">is</span></code></p>
</blockquote>
<pre>std::wistream&amp; operator&gt;&gt;(std::wistream &amp; is, path&amp; p);</pre>
<blockquote>
<p><span style="background-color: #FFFFFF"><i>Effects: </i>
<code>&nbsp; std::wstring str;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is &gt;&gt; str;<br>
<p><span style="background-color: #FFFFFF"><i>Effects:&nbsp; </i>
<code>&nbsp;std::basic_string&lt;Char&gt; str;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is &gt;&gt;
<a href="../../../io/doc/quoted_manip.html">boost::io::quoted</a>(str,
static_cast&lt;Char&gt;('&amp;'));<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p = str;</code></span></p>
<p><i><span style="background-color: #FFFFFF">Returns:</span></i><span style="background-color: #FFFFFF">
</span> <code><span style="background-color: #FFFFFF">is</span></code></p>
@@ -3074,7 +3068,7 @@ multiple string types. His idea became the basis for the version 3 path design.<
<p>Distributed under the Boost Software License, Version 1.0. See
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
<p>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B %Y" startspan -->04 June 2010<!--webbot bot="Timestamp" endspan i-checksum="17550" --></p>
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B %Y" startspan -->20 June 2010<!--webbot bot="Timestamp" endspan i-checksum="17544" --></p>
</body>

View File

@@ -86,6 +86,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tut4", "tut4\tut4.vcproj",
{FFD738F7-96F0-445C-81EA-551665EF53D1} = {FFD738F7-96F0-445C-81EA-551665EF53D1}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hebrew_example", "hebrew_example\hebrew_example.vcproj", "{F9F236A2-8B57-415A-8397-7145144400F5}"
ProjectSection(ProjectDependencies) = postProject
{F94CCADD-A90B-480C-A304-C19D015D36B1} = {F94CCADD-A90B-480C-A304-C19D015D36B1}
{FFD738F7-96F0-445C-81EA-551665EF53D1} = {FFD738F7-96F0-445C-81EA-551665EF53D1}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
@@ -152,6 +158,10 @@ Global
{256EA89A-E073-4CE8-B675-BE2FBC6B2691}.Debug|Win32.Build.0 = Debug|Win32
{256EA89A-E073-4CE8-B675-BE2FBC6B2691}.Release|Win32.ActiveCfg = Release|Win32
{256EA89A-E073-4CE8-B675-BE2FBC6B2691}.Release|Win32.Build.0 = Release|Win32
{F9F236A2-8B57-415A-8397-7145144400F5}.Debug|Win32.ActiveCfg = Debug|Win32
{F9F236A2-8B57-415A-8397-7145144400F5}.Debug|Win32.Build.0 = Debug|Win32
{F9F236A2-8B57-415A-8397-7145144400F5}.Release|Win32.ActiveCfg = Release|Win32
{F9F236A2-8B57-415A-8397-7145144400F5}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -398,6 +398,16 @@ namespace
ss << p1;
ss >> p2;
CHECK(p1 == p2);
path wp1(L"foo bar");
path wp2;
std::wstringstream wss;
CHECK(wp1 != wp2);
wss << wp1;
wss >> wp2;
CHECK(wp1 == wp2);
}
// test_other_non_members ----------------------------------------------------------//