restore from disk crash

[SVN r18353]
This commit is contained in:
Dave Abrahams 2003-05-08 02:19:10 +00:00
parent 534befe242
commit 2d756c6eb1
3 changed files with 124 additions and 3 deletions

View File

@ -24,7 +24,10 @@ ways.</p>
<dl class="index">
<dt><a href="#create_directories">create_directories</a>
</ol>
<dt><a href="#extension">extension</a>
<dt><a href="#basename">basename</a>
<dt><a href="#change_extension">change_extension</a>
</dl>
<h2 id="create_directoies">create_directories</h2>
@ -41,10 +44,75 @@ forall p: p == ph || is_parent(p, ph): is_directory(p) || !exists( p )</code>
</blockquote>
<h2 id="extension">extension</h2>
<blockquote>
<p><code>std::string extension( const path &amp; ph );</code></p>
<p><b>Returns:</b> if <code>ph.leaf()</code> contains a dot ('.'),
returns the substring of <code>ph.leaf()</code> starting from the last dot and
ending at the string's end. Otherwise, returns empty string.
<p><b>Rationale:</b> <ul>
<li>The dot is included in the return value so that it's
possible to tell if extension is empty or absent.
<li>It was noted that this defintion of extension is probably not sufficient
when using <a href="http://tinyurl.com/9tih">Alternate Data Streams</a> &mdash;
a filesystem feature specific to NTFS. However, semantic in this case was not
clear, and the current one is quite usefull still.
</ul>
<p><b>Acknowlegements:</b> Carl Daniel and Pavel Vozenilek noticed and
discussed the ADS issue.
<p>Contributed by Vladimir Prus.</p>
</blockquote>
<h2 id="basename">basename</h2>
<blockquote>
<p><code>std::string basename( const path &amp; ph );</code></p>
<p><b>Returns:</b> if <code>ph.leaf()</code> contains a dot ('.'),
returns the substring of <code>ph.leaf()</code> starting from beginning and
ending at the last dot (the dot is not included). Otherwise, returns
<code>ph.leaf()</code>
</p>
<p>Contributed by Vladimir Prus.</p>
</blockquote>
<h2 id="change_extension">change_extension</h2>
<blockquote>
<p><code>path basename( const path &amp; ph, const std::string &amp; new_extension );</code></p>
<p><b>Postcondition:</b> <code>basename(return_value) == basename(ph)
&amp;&amp; extension(return_value) == new_extension</code>
<p><b>Note:</b> It follows from the semantic of <code>extension</code> that
<code>new_extension</code> should include dot to achieve reasonable results.
</p>
<p><b>Rationale:</b> Previously, this functions had
<code>!ph.leaf().empty()</code> as precondition. It's not clear if it was
right or wrong. Changing extension of an empty path looks pointless. On the
other hand, the value of precondition was questionable: one would better place such
checks at the points where paths are entered by the user. Current decision
is to drop the precondition.</p>
<p>Contributed by Vladimir Prus.</p>
</blockquote>
<hr>
<p>© Copyright Beman Dawes, 2002</p>
<p>© Copyright Vladimir Prus, 2003</p>
<p>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->23 December, 2002<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p>
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->5 May, 2003<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p>
<p>&nbsp;</p>
</body>

View File

@ -28,5 +28,30 @@ namespace boost
create_directory(ph);
}
std::string extension(const path& ph)
{
std::string leaf = ph.leaf();
std::string::size_type n = leaf.rfind('.');
if (n != std::string::npos)
return leaf.substr(n);
else
return std::string();
}
std::string basename(const path& ph)
{
std::string leaf = ph.leaf();
std::string::size_type n = leaf.rfind('.');
return leaf.substr(0, n);
}
path change_extension(const path& ph, const std::string& new_extension)
{
return ph.branch_path() / (basename(ph) + new_extension);
}
} // namespace filesystem
} // namespace boost

View File

@ -35,6 +35,34 @@ int test_main( int, char*[] )
BOOST_TEST( fs::is_directory( "xx" ) );
BOOST_TEST( fs::is_directory( "xx/ww" ) );
BOOST_TEST( fs::is_directory( "xx/ww/zz" ) );
// extension() tests ----------------------------------------------------------//
BOOST_TEST( fs::extension("a/b") == "" );
BOOST_TEST( fs::extension("a/b.txt") == ".txt" );
BOOST_TEST( fs::extension("a/b.") == "." );
BOOST_TEST( fs::extension("a.b.c") == ".c" );
BOOST_TEST( fs::extension("a.b.c.") == "." );
BOOST_TEST( fs::extension("") == "" );
BOOST_TEST( fs::extension("a/") == "" );
// basename() tests ----------------------------------------------------------//
BOOST_TEST( fs::basename("b") == "b" );
BOOST_TEST( fs::basename("a/b.txt") == "b" );
BOOST_TEST( fs::basename("a/b.") == "b" );
BOOST_TEST( fs::basename("a.b.c") == "a.b" );
BOOST_TEST( fs::basename("a.b.c.") == "a.b.c" );
BOOST_TEST( fs::basename("") == "" );
// change_extension tests ---------------------------------------------------//
BOOST_TEST( fs::change_extension("a.txt", ".tex").string() == "a.tex" );
BOOST_TEST( fs::change_extension("a.", ".tex").string() == "a.tex" );
BOOST_TEST( fs::change_extension("a", ".txt").string() == "a.txt" );
BOOST_TEST( fs::change_extension("a.b.txt", ".tex").string() == "a.b.tex" );
// see the rationale in html docs for explanation why this works
BOOST_TEST( fs::change_extension("", ".png").string() == ".png" );
return 0;
}