]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/filesystem/example/error_demo.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / filesystem / example / error_demo.cpp
1 // error_demo.cpp --------------------------------------------------------------------//
2
3 // Copyright Beman Dawes 2009
4
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
7
8 // Library home page: http://www.boost.org/libs/filesystem
9
10 //--------------------------------------------------------------------------------------//
11 // //
12 // The purpose of this program is to demonstrate how error reporting works. //
13 // //
14 //--------------------------------------------------------------------------------------//
15
16 #include <boost/filesystem.hpp>
17 #include <boost/system/system_error.hpp>
18 #include <iostream>
19
20 using std::cout;
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;
26
27 namespace
28 {
29 void report_system_error(const system_error& ex)
30 {
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'
35 ;
36 }
37
38 void report_filesystem_error(const system_error& ex)
39 {
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'
44 ;
45 }
46
47 void report_status(fs::file_status s)
48 {
49 cout << " file_status::type() is ";
50 switch (s.type())
51 {
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;
62 case fs::block_file:
63 cout << "block_file\n"; break;
64 case fs::character_file:
65 cout << "character_file\n"; break;
66 case fs::fifo_file:
67 cout << "fifo_file\n"; break;
68 case fs::socket_file:
69 cout << "socket_file\n"; break;
70 case fs::type_unknown:
71 cout << "type_unknown\n"; break;
72 default:
73 cout << "not a valid enumeration constant\n";
74 }
75 }
76
77 void report_error_code(const error_code& ec)
78 {
79 cout << " ec:\n"
80 << " value() is " << ec.value() << '\n'
81 << " category().name() is " << ec.category().name() << '\n'
82 << " message() is " << ec.message() << '\n'
83 ;
84 }
85
86 bool threw_exception;
87
88 }
89
90 int main(int argc, char* argv[])
91 {
92 if (argc < 2)
93 {
94 cout << "Usage: error_demo path\n";
95 return 1;
96 }
97
98 error_code ec;
99
100 //// construct path - no error_code
101
102 //try { path p1(argv[1]); }
103 //catch (const system_error& ex)
104 //{
105 // cout << "construct path without error_code";
106 // report_system_error(ex);
107 //}
108
109 //// construct path - with error_code
110
111 path p (argv[1]);
112
113 fs::file_status s;
114 bool b (false);
115 fs::directory_iterator di;
116
117 // get status - no error_code
118
119 cout << "\nstatus(\"" << p.string() << "\");\n";
120 threw_exception = false;
121
122 try { s = fs::status(p); }
123 catch (const system_error& ex)
124 {
125 report_filesystem_error(ex);
126 threw_exception = true;
127 }
128 if (!threw_exception)
129 cout << " Did not throw exception\n";
130 report_status(s);
131
132 // get status - with error_code
133
134 cout << "\nstatus(\"" << p.string() << "\", ec);\n";
135 s = fs::status(p, ec);
136 report_status(s);
137 report_error_code(ec);
138
139 // query existence - no error_code
140
141 cout << "\nexists(\"" << p.string() << "\");\n";
142 threw_exception = false;
143
144 try { b = fs::exists(p); }
145 catch (const system_error& ex)
146 {
147 report_filesystem_error(ex);
148 threw_exception = true;
149 }
150 if (!threw_exception)
151 {
152 cout << " Did not throw exception\n"
153 << " Returns: " << (b ? "true" : "false") << '\n';
154 }
155
156 // query existence - with error_code
157
158 // directory_iterator - no error_code
159
160 cout << "\ndirectory_iterator(\"" << p.string() << "\");\n";
161 threw_exception = false;
162
163 try { di = fs::directory_iterator(p); }
164 catch (const system_error& ex)
165 {
166 report_filesystem_error(ex);
167 threw_exception = true;
168 }
169 if (!threw_exception)
170 {
171 cout << " Did not throw exception\n"
172 << (di == fs::directory_iterator() ? " Equal" : " Not equal")
173 << " to the end iterator\n";
174 }
175
176 // directory_iterator - with error_code
177
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);
183
184 return 0;
185 }