1 // libs/filesystem/src/convenience.cpp -------------------------------------//
3 // © Copyright Beman Dawes, 2002
4 // © Copyright Vladimir Prus, 2002
5 // Use, modification, and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See library home page at http://www.boost.org/libs/filesystem
11 //----------------------------------------------------------------------------//
13 // define BOOST_FILESYSTEM_SOURCE so that <boost/filesystem/config.hpp> knows
14 // the library is being built (possibly exporting rather than importing code)
15 #define BOOST_FILESYSTEM_SOURCE
17 #include <boost/filesystem/convenience.hpp>
18 #include <boost/filesystem/exception.hpp>
19 #include <boost/throw_exception.hpp>
21 #include <boost/config/abi_prefix.hpp> // must be the last header
28 // create_directories (contributed by Vladimir Prus) -----------------------//
30 BOOST_FILESYSTEM_DECL bool create_directories(const path& ph)
32 if (ph.empty() || exists(ph))
34 if ( !ph.empty() && !is_directory(ph) )
35 boost::throw_exception( filesystem_error(
36 "boost::filesystem::create_directories",
37 ph, "path exists and is not a directory",
38 not_directory_error ) );
42 // First create branch, by calling ourself recursively
43 create_directories(ph.branch_path());
44 // Now that parent's path exists, create the directory
49 BOOST_FILESYSTEM_DECL std::string extension(const path& ph)
51 std::string leaf = ph.leaf();
53 std::string::size_type n = leaf.rfind('.');
54 if (n != std::string::npos)
55 return leaf.substr(n);
60 BOOST_FILESYSTEM_DECL std::string basename(const path& ph)
62 std::string leaf = ph.leaf();
64 std::string::size_type n = leaf.rfind('.');
65 return leaf.substr(0, n);
68 BOOST_FILESYSTEM_DECL path change_extension(const path& ph, const std::string& new_extension)
70 return ph.branch_path() / (basename(ph) + new_extension);
74 } // namespace filesystem