]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
1e59de90 TL |
27 | namespace { |
28 | void report_system_error(const system_error& ex) | |
7c673cae | 29 | { |
7c673cae FG |
30 | cout << " threw system_error:\n" |
31 | << " ex.code().value() is " << ex.code().value() << '\n' | |
32 | << " ex.code().category().name() is " << ex.code().category().name() << '\n' | |
1e59de90 TL |
33 | << " ex.what() is " << ex.what() << '\n'; |
34 | } | |
7c673cae | 35 | |
1e59de90 TL |
36 | void report_filesystem_error(const system_error& ex) |
37 | { | |
7c673cae FG |
38 | cout << " threw filesystem_error exception:\n" |
39 | << " ex.code().value() is " << ex.code().value() << '\n' | |
40 | << " ex.code().category().name() is " << ex.code().category().name() << '\n' | |
1e59de90 TL |
41 | << " ex.what() is " << ex.what() << '\n'; |
42 | } | |
7c673cae | 43 | |
1e59de90 TL |
44 | void report_status(fs::file_status s) |
45 | { | |
7c673cae FG |
46 | cout << " file_status::type() is "; |
47 | switch (s.type()) | |
48 | { | |
49 | case fs::status_error: | |
1e59de90 TL |
50 | cout << "status_error\n"; |
51 | break; | |
7c673cae | 52 | case fs::file_not_found: |
1e59de90 TL |
53 | cout << "file_not_found\n"; |
54 | break; | |
7c673cae | 55 | case fs::regular_file: |
1e59de90 TL |
56 | cout << "regular_file\n"; |
57 | break; | |
7c673cae | 58 | case fs::directory_file: |
1e59de90 TL |
59 | cout << "directory_file\n"; |
60 | break; | |
7c673cae | 61 | case fs::symlink_file: |
1e59de90 TL |
62 | cout << "symlink_file\n"; |
63 | break; | |
7c673cae | 64 | case fs::block_file: |
1e59de90 TL |
65 | cout << "block_file\n"; |
66 | break; | |
7c673cae | 67 | case fs::character_file: |
1e59de90 TL |
68 | cout << "character_file\n"; |
69 | break; | |
7c673cae | 70 | case fs::fifo_file: |
1e59de90 TL |
71 | cout << "fifo_file\n"; |
72 | break; | |
7c673cae | 73 | case fs::socket_file: |
1e59de90 TL |
74 | cout << "socket_file\n"; |
75 | break; | |
7c673cae | 76 | case fs::type_unknown: |
1e59de90 TL |
77 | cout << "type_unknown\n"; |
78 | break; | |
7c673cae | 79 | default: |
1e59de90 | 80 | cout << "not a valid enumeration constant\n"; |
7c673cae | 81 | } |
1e59de90 | 82 | } |
7c673cae | 83 | |
1e59de90 TL |
84 | void report_error_code(const error_code& ec) |
85 | { | |
7c673cae FG |
86 | cout << " ec:\n" |
87 | << " value() is " << ec.value() << '\n' | |
88 | << " category().name() is " << ec.category().name() << '\n' | |
1e59de90 TL |
89 | << " message() is " << ec.message() << '\n'; |
90 | } | |
7c673cae | 91 | |
1e59de90 | 92 | bool threw_exception; |
7c673cae | 93 | |
1e59de90 | 94 | } // namespace |
7c673cae FG |
95 | |
96 | int main(int argc, char* argv[]) | |
97 | { | |
1e59de90 TL |
98 | if (argc < 2) |
99 | { | |
100 | cout << "Usage: error_demo path\n"; | |
101 | return 1; | |
102 | } | |
103 | ||
104 | error_code ec; | |
105 | ||
106 | //// construct path - no error_code | |
107 | ||
108 | //try { path p1(argv[1]); } | |
109 | //catch (const system_error& ex) | |
110 | //{ | |
111 | // cout << "construct path without error_code"; | |
112 | // report_system_error(ex); | |
113 | //} | |
114 | ||
115 | //// construct path - with error_code | |
116 | ||
117 | path p(argv[1]); | |
118 | ||
119 | fs::file_status s; | |
120 | bool b(false); | |
121 | fs::directory_iterator di; | |
122 | ||
123 | // get status - no error_code | |
124 | ||
125 | cout << "\nstatus(\"" << p.string() << "\");\n"; | |
126 | threw_exception = false; | |
127 | ||
128 | try | |
129 | { | |
130 | s = fs::status(p); | |
131 | } | |
132 | catch (const system_error& ex) | |
133 | { | |
134 | report_filesystem_error(ex); | |
135 | threw_exception = true; | |
136 | } | |
137 | if (!threw_exception) | |
138 | cout << " Did not throw exception\n"; | |
139 | report_status(s); | |
140 | ||
141 | // get status - with error_code | |
142 | ||
143 | cout << "\nstatus(\"" << p.string() << "\", ec);\n"; | |
144 | s = fs::status(p, ec); | |
145 | report_status(s); | |
146 | report_error_code(ec); | |
147 | ||
148 | // query existence - no error_code | |
149 | ||
150 | cout << "\nexists(\"" << p.string() << "\");\n"; | |
151 | threw_exception = false; | |
152 | ||
153 | try | |
154 | { | |
155 | b = fs::exists(p); | |
156 | } | |
157 | catch (const system_error& ex) | |
158 | { | |
159 | report_filesystem_error(ex); | |
160 | threw_exception = true; | |
161 | } | |
162 | if (!threw_exception) | |
163 | { | |
164 | cout << " Did not throw exception\n" | |
165 | << " Returns: " << (b ? "true" : "false") << '\n'; | |
166 | } | |
167 | ||
168 | // query existence - with error_code | |
169 | ||
170 | // directory_iterator - no error_code | |
171 | ||
172 | cout << "\ndirectory_iterator(\"" << p.string() << "\");\n"; | |
173 | threw_exception = false; | |
174 | ||
175 | try | |
176 | { | |
177 | di = fs::directory_iterator(p); | |
178 | } | |
179 | catch (const system_error& ex) | |
180 | { | |
181 | report_filesystem_error(ex); | |
182 | threw_exception = true; | |
183 | } | |
184 | if (!threw_exception) | |
185 | { | |
186 | cout << " Did not throw exception\n" | |
187 | << (di == fs::directory_iterator() ? " Equal" : " Not equal") | |
188 | << " to the end iterator\n"; | |
189 | } | |
190 | ||
191 | // directory_iterator - with error_code | |
192 | ||
193 | cout << "\ndirectory_iterator(\"" << p.string() << "\", ec);\n"; | |
194 | di = fs::directory_iterator(p, ec); | |
195 | cout << (di == fs::directory_iterator() ? " Equal" : " Not equal") | |
196 | << " to the end iterator\n"; | |
197 | report_error_code(ec); | |
198 | ||
199 | return 0; | |
7c673cae | 200 | } |