1 | // libs/filesystem/src/convenience.cpp -------------------------------------// |
---|
2 | |
---|
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) |
---|
8 | |
---|
9 | // See library home page at http://www.boost.org/libs/filesystem |
---|
10 | |
---|
11 | //----------------------------------------------------------------------------// |
---|
12 | |
---|
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 |
---|
16 | |
---|
17 | #include <boost/filesystem/convenience.hpp> |
---|
18 | #include <boost/filesystem/exception.hpp> |
---|
19 | #include <boost/throw_exception.hpp> |
---|
20 | |
---|
21 | #include <boost/config/abi_prefix.hpp> // must be the last header |
---|
22 | |
---|
23 | namespace boost |
---|
24 | { |
---|
25 | namespace filesystem |
---|
26 | { |
---|
27 | |
---|
28 | // create_directories (contributed by Vladimir Prus) -----------------------// |
---|
29 | |
---|
30 | BOOST_FILESYSTEM_DECL bool create_directories(const path& ph) |
---|
31 | { |
---|
32 | if (ph.empty() || exists(ph)) |
---|
33 | { |
---|
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 ) ); |
---|
39 | return false; |
---|
40 | } |
---|
41 | |
---|
42 | // First create branch, by calling ourself recursively |
---|
43 | create_directories(ph.branch_path()); |
---|
44 | // Now that parent's path exists, create the directory |
---|
45 | create_directory(ph); |
---|
46 | return true; |
---|
47 | } |
---|
48 | |
---|
49 | BOOST_FILESYSTEM_DECL std::string extension(const path& ph) |
---|
50 | { |
---|
51 | std::string leaf = ph.leaf(); |
---|
52 | |
---|
53 | std::string::size_type n = leaf.rfind('.'); |
---|
54 | if (n != std::string::npos) |
---|
55 | return leaf.substr(n); |
---|
56 | else |
---|
57 | return std::string(); |
---|
58 | } |
---|
59 | |
---|
60 | BOOST_FILESYSTEM_DECL std::string basename(const path& ph) |
---|
61 | { |
---|
62 | std::string leaf = ph.leaf(); |
---|
63 | |
---|
64 | std::string::size_type n = leaf.rfind('.'); |
---|
65 | return leaf.substr(0, n); |
---|
66 | } |
---|
67 | |
---|
68 | BOOST_FILESYSTEM_DECL path change_extension(const path& ph, const std::string& new_extension) |
---|
69 | { |
---|
70 | return ph.branch_path() / (basename(ph) + new_extension); |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | } // namespace filesystem |
---|
75 | } // namespace boost |
---|