1// boost/filesystem/exception.hpp -----------------------------------------------------//
2
3// Copyright Beman Dawes 2003
4// Copyright Andrey Semashev 2019
5
6// Distributed under the Boost Software License, Version 1.0.
7// See http://www.boost.org/LICENSE_1_0.txt
8
9// Library home page: http://www.boost.org/libs/filesystem
10
11#include "platform_config.hpp"
12
13#include <string>
14#include <boost/system/error_code.hpp>
15#include <boost/system/system_category.hpp>
16#include <boost/filesystem/config.hpp>
17#include <boost/filesystem/path.hpp>
18#include <boost/filesystem/exception.hpp>
19
20#include "error_handling.hpp"
21
22#include <boost/filesystem/detail/header.hpp> // must be the last #include
23
24namespace boost {
25namespace filesystem {
26
27BOOST_FILESYSTEM_DECL filesystem_error::filesystem_error(const char* what_arg, system::error_code ec) :
28 system::system_error(ec, what_arg)
29{
30 try
31 {
32 m_imp_ptr.reset(rhs: new impl());
33 }
34 catch (...)
35 {
36 m_imp_ptr.reset();
37 }
38}
39
40BOOST_FILESYSTEM_DECL filesystem_error::filesystem_error(std::string const& what_arg, system::error_code ec) :
41 system::system_error(ec, what_arg)
42{
43 try
44 {
45 m_imp_ptr.reset(rhs: new impl());
46 }
47 catch (...)
48 {
49 m_imp_ptr.reset();
50 }
51}
52
53BOOST_FILESYSTEM_DECL filesystem_error::filesystem_error(const char* what_arg, path const& path1_arg, system::error_code ec) :
54 system::system_error(ec, what_arg)
55{
56 try
57 {
58 m_imp_ptr.reset(rhs: new impl(path1_arg));
59 }
60 catch (...)
61 {
62 m_imp_ptr.reset();
63 }
64}
65
66BOOST_FILESYSTEM_DECL filesystem_error::filesystem_error(std::string const& what_arg, path const& path1_arg, system::error_code ec) :
67 system::system_error(ec, what_arg)
68{
69 try
70 {
71 m_imp_ptr.reset(rhs: new impl(path1_arg));
72 }
73 catch (...)
74 {
75 m_imp_ptr.reset();
76 }
77}
78
79BOOST_FILESYSTEM_DECL filesystem_error::filesystem_error(const char* what_arg, path const& path1_arg, path const& path2_arg, system::error_code ec) :
80 system::system_error(ec, what_arg)
81{
82 try
83 {
84 m_imp_ptr.reset(rhs: new impl(path1_arg, path2_arg));
85 }
86 catch (...)
87 {
88 m_imp_ptr.reset();
89 }
90}
91
92BOOST_FILESYSTEM_DECL filesystem_error::filesystem_error(std::string const& what_arg, path const& path1_arg, path const& path2_arg, system::error_code ec) :
93 system::system_error(ec, what_arg)
94{
95 try
96 {
97 m_imp_ptr.reset(rhs: new impl(path1_arg, path2_arg));
98 }
99 catch (...)
100 {
101 m_imp_ptr.reset();
102 }
103}
104
105BOOST_FILESYSTEM_DECL filesystem_error::filesystem_error(filesystem_error const& that) :
106 system::system_error(static_cast< system::system_error const& >(that)),
107 m_imp_ptr(that.m_imp_ptr)
108{
109}
110
111BOOST_FILESYSTEM_DECL filesystem_error& filesystem_error::operator=(filesystem_error const& that)
112{
113 static_cast< system::system_error& >(*this) = static_cast< system::system_error const& >(that);
114 m_imp_ptr = that.m_imp_ptr;
115 return *this;
116}
117
118BOOST_FILESYSTEM_DECL filesystem_error::~filesystem_error() noexcept
119{
120}
121
122BOOST_FILESYSTEM_DECL const char* filesystem_error::what() const noexcept
123{
124 if (m_imp_ptr.get()) try
125 {
126 if (m_imp_ptr->m_what.empty())
127 {
128 m_imp_ptr->m_what = system::system_error::what();
129 if (!m_imp_ptr->m_path1.empty())
130 {
131 m_imp_ptr->m_what += ": \"";
132 m_imp_ptr->m_what += m_imp_ptr->m_path1.string();
133 m_imp_ptr->m_what += "\"";
134 }
135 if (!m_imp_ptr->m_path2.empty())
136 {
137 m_imp_ptr->m_what += ", \"";
138 m_imp_ptr->m_what += m_imp_ptr->m_path2.string();
139 m_imp_ptr->m_what += "\"";
140 }
141 }
142
143 return m_imp_ptr->m_what.c_str();
144 }
145 catch (...)
146 {
147 m_imp_ptr->m_what.clear();
148 }
149
150 return system::system_error::what();
151}
152
153BOOST_FILESYSTEM_DECL path const& filesystem_error::get_empty_path() noexcept
154{
155 static const path empty_path;
156 return empty_path;
157}
158
159// error handling helpers declared in error_handling.hpp -----------------------------------------------------//
160
161void emit_error(err_t error_num, system::error_code* ec, const char* message)
162{
163 if (!ec)
164 BOOST_FILESYSTEM_THROW(filesystem_error(message, system::error_code(error_num, system::system_category())));
165 else
166 ec->assign(val: error_num, cat: system::system_category());
167}
168
169void emit_error(err_t error_num, path const& p, system::error_code* ec, const char* message)
170{
171 if (!ec)
172 BOOST_FILESYSTEM_THROW(filesystem_error(message, p, system::error_code(error_num, system::system_category())));
173 else
174 ec->assign(val: error_num, cat: system::system_category());
175}
176
177void emit_error(err_t error_num, path const& p1, path const& p2, system::error_code* ec, const char* message)
178{
179 if (!ec)
180 BOOST_FILESYSTEM_THROW(filesystem_error(message, p1, p2, system::error_code(error_num, system::system_category())));
181 else
182 ec->assign(val: error_num, cat: system::system_category());
183}
184
185} // namespace filesystem
186} // namespace boost
187
188#include <boost/filesystem/detail/footer.hpp>
189

source code of boost/libs/filesystem/src/exception.cpp