]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/filesystem/example/error_demo.cpp
1 // error_demo.cpp --------------------------------------------------------------------//
3 // Copyright Beman Dawes 2009
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
8 // Library home page: http://www.boost.org/libs/filesystem
10 //--------------------------------------------------------------------------------------//
12 // The purpose of this program is to demonstrate how error reporting works. //
14 //--------------------------------------------------------------------------------------//
16 #include <boost/filesystem.hpp>
17 #include <boost/system/system_error.hpp>
21 using boost::filesystem::path
;
22 using boost::filesystem::filesystem_error
;
23 using boost::system::error_code
;
24 using boost::system::system_error
;
25 namespace fs
= boost::filesystem
;
29 void report_system_error(const system_error
& ex
)
31 cout
<< " threw system_error:\n"
32 << " ex.code().value() is " << ex
.code().value() << '\n'
33 << " ex.code().category().name() is " << ex
.code().category().name() << '\n'
34 << " ex.what() is " << ex
.what() << '\n'
38 void report_filesystem_error(const system_error
& ex
)
40 cout
<< " threw filesystem_error exception:\n"
41 << " ex.code().value() is " << ex
.code().value() << '\n'
42 << " ex.code().category().name() is " << ex
.code().category().name() << '\n'
43 << " ex.what() is " << ex
.what() << '\n'
47 void report_status(fs::file_status s
)
49 cout
<< " file_status::type() is ";
52 case fs::status_error
:
53 cout
<< "status_error\n"; break;
54 case fs::file_not_found
:
55 cout
<< "file_not_found\n"; break;
56 case fs::regular_file
:
57 cout
<< "regular_file\n"; break;
58 case fs::directory_file
:
59 cout
<< "directory_file\n"; break;
60 case fs::symlink_file
:
61 cout
<< "symlink_file\n"; break;
63 cout
<< "block_file\n"; break;
64 case fs::character_file
:
65 cout
<< "character_file\n"; break;
67 cout
<< "fifo_file\n"; break;
69 cout
<< "socket_file\n"; break;
70 case fs::type_unknown
:
71 cout
<< "type_unknown\n"; break;
73 cout
<< "not a valid enumeration constant\n";
77 void report_error_code(const error_code
& ec
)
80 << " value() is " << ec
.value() << '\n'
81 << " category().name() is " << ec
.category().name() << '\n'
82 << " message() is " << ec
.message() << '\n'
90 int main(int argc
, char* argv
[])
94 cout
<< "Usage: error_demo path\n";
100 //// construct path - no error_code
102 //try { path p1(argv[1]); }
103 //catch (const system_error& ex)
105 // cout << "construct path without error_code";
106 // report_system_error(ex);
109 //// construct path - with error_code
115 fs::directory_iterator di
;
117 // get status - no error_code
119 cout
<< "\nstatus(\"" << p
.string() << "\");\n";
120 threw_exception
= false;
122 try { s
= fs::status(p
); }
123 catch (const system_error
& ex
)
125 report_filesystem_error(ex
);
126 threw_exception
= true;
128 if (!threw_exception
)
129 cout
<< " Did not throw exception\n";
132 // get status - with error_code
134 cout
<< "\nstatus(\"" << p
.string() << "\", ec);\n";
135 s
= fs::status(p
, ec
);
137 report_error_code(ec
);
139 // query existence - no error_code
141 cout
<< "\nexists(\"" << p
.string() << "\");\n";
142 threw_exception
= false;
144 try { b
= fs::exists(p
); }
145 catch (const system_error
& ex
)
147 report_filesystem_error(ex
);
148 threw_exception
= true;
150 if (!threw_exception
)
152 cout
<< " Did not throw exception\n"
153 << " Returns: " << (b
? "true" : "false") << '\n';
156 // query existence - with error_code
158 // directory_iterator - no error_code
160 cout
<< "\ndirectory_iterator(\"" << p
.string() << "\");\n";
161 threw_exception
= false;
163 try { di
= fs::directory_iterator(p
); }
164 catch (const system_error
& ex
)
166 report_filesystem_error(ex
);
167 threw_exception
= true;
169 if (!threw_exception
)
171 cout
<< " Did not throw exception\n"
172 << (di
== fs::directory_iterator() ? " Equal" : " Not equal")
173 << " to the end iterator\n";
176 // directory_iterator - with error_code
178 cout
<< "\ndirectory_iterator(\"" << p
.string() << "\", ec);\n";
179 di
= fs::directory_iterator(p
, ec
);
180 cout
<< (di
== fs::directory_iterator() ? " Equal" : " Not equal")
181 << " to the end iterator\n";
182 report_error_code(ec
);