Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / filesystem / src / exception.cpp
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 <string>
12 #include <boost/filesystem/config.hpp>
13 #include <boost/filesystem/path.hpp>
14 #include <boost/filesystem/exception.hpp>
15
16 #include "error_handling.hpp"
17
18 #include <boost/config/abi_prefix.hpp> // must be the last #include
19
20 namespace boost {
21 namespace filesystem {
22
23 filesystem_error::filesystem_error(const std::string& what_arg, system::error_code ec) :
24   system::system_error(ec, what_arg)
25 {
26   try
27   {
28     m_imp_ptr.reset(new impl());
29   }
30   catch (...)
31   {
32     m_imp_ptr.reset();
33   }
34 }
35
36 filesystem_error::filesystem_error(const std::string& what_arg, const path& path1_arg, system::error_code ec) :
37   system::system_error(ec, what_arg)
38 {
39   try
40   {
41     m_imp_ptr.reset(new impl(path1_arg));
42   }
43   catch (...)
44   {
45     m_imp_ptr.reset();
46   }
47 }
48
49 filesystem_error::filesystem_error(const std::string& what_arg, const path& path1_arg, const path& path2_arg, system::error_code ec) :
50   system::system_error(ec, what_arg)
51 {
52   try
53   {
54     m_imp_ptr.reset(new impl(path1_arg, path2_arg));
55   }
56   catch (...)
57   {
58     m_imp_ptr.reset();
59   }
60 }
61
62 filesystem_error::filesystem_error(filesystem_error const& that) :
63   system::system_error(static_cast< system::system_error const& >(that)),
64   m_imp_ptr(that.m_imp_ptr)
65 {
66 }
67
68 filesystem_error& filesystem_error::operator= (filesystem_error const& that)
69 {
70   static_cast< system::system_error& >(*this) = static_cast< system::system_error const& >(that);
71   m_imp_ptr = that.m_imp_ptr;
72   return *this;
73 }
74
75 filesystem_error::~filesystem_error() BOOST_NOEXCEPT_OR_NOTHROW
76 {
77 }
78
79 const char* filesystem_error::what() const BOOST_NOEXCEPT_OR_NOTHROW
80 {
81   if (m_imp_ptr.get()) try
82   {
83     if (m_imp_ptr->m_what.empty())
84     {
85       m_imp_ptr->m_what = system::system_error::what();
86       if (!m_imp_ptr->m_path1.empty())
87       {
88         m_imp_ptr->m_what += ": \"";
89         m_imp_ptr->m_what += m_imp_ptr->m_path1.string();
90         m_imp_ptr->m_what += "\"";
91       }
92       if (!m_imp_ptr->m_path2.empty())
93       {
94         m_imp_ptr->m_what += ", \"";
95         m_imp_ptr->m_what += m_imp_ptr->m_path2.string();
96         m_imp_ptr->m_what += "\"";
97       }
98     }
99
100     return m_imp_ptr->m_what.c_str();
101   }
102   catch (...)
103   {
104     m_imp_ptr->m_what.clear();
105   }
106
107   return system::system_error::what();
108 }
109
110 const path& filesystem_error::get_empty_path() BOOST_NOEXCEPT
111 {
112   static const path empty_path;
113   return empty_path;
114 }
115
116 //  error handling helpers declared in error_handling.hpp  -----------------------------------------------------//
117
118 void emit_error(err_t error_num, system::error_code* ec, const char* message)
119 {
120   if (!ec)
121     BOOST_FILESYSTEM_THROW(filesystem_error(message, system::error_code(error_num, system::system_category())));
122   else
123     ec->assign(error_num, system::system_category());
124 }
125
126 void emit_error(err_t error_num, const path& p, system::error_code* ec, const char* message)
127 {
128   if (!ec)
129     BOOST_FILESYSTEM_THROW(filesystem_error(message, p, system::error_code(error_num, system::system_category())));
130   else
131     ec->assign(error_num, system::system_category());
132 }
133
134 void emit_error(err_t error_num, const path& p1, const path& p2, system::error_code* ec, const char* message)
135 {
136   if (ec == 0)
137     BOOST_FILESYSTEM_THROW(filesystem_error(message, p1, p2, system::error_code(error_num, system::system_category())));
138   else
139     ec->assign(error_num, system::system_category());
140 }
141
142 } // namespace filesystem
143 } // namespace boost
144
145 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas