1 // status.cpp ------------------------------------------------------------------------//
2
3 // Copyright Beman Dawes 2011
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 #include <iostream>
11 #include <boost/config.hpp>
12 #include <boost/version.hpp>
13 #include <boost/filesystem.hpp>
14 #include <boost/detail/lightweight_main.hpp>
15
16 using std::cout; using std::endl;
17 using namespace boost::filesystem;
18
19 namespace
20 {
21 path p;
22
print_boost_macros()23 void print_boost_macros()
24 {
25 std::cout << "Boost "
26 << BOOST_VERSION / 100000 << '.'
27 << BOOST_VERSION / 100 % 1000 << '.'
28 << BOOST_VERSION % 100 << ", "
29 # ifndef _WIN64
30 << BOOST_COMPILER << ", "
31 # else
32 << BOOST_COMPILER << " with _WIN64 defined, "
33 # endif
34 << BOOST_STDLIB << ", "
35 << BOOST_PLATFORM
36 << std::endl;
37 }
38
39 const char* file_type_tab[] =
40 { "status_error", "file_not_found", "regular_file", "directory_file",
41 "symlink_file", "block_file", "character_file", "fifo_file", "socket_file",
42 "type_unknown"
43 };
44
file_type_c_str(enum file_type t)45 const char* file_type_c_str(enum file_type t)
46 {
47 return file_type_tab[t];
48 }
49
show_status(file_status s,boost::system::error_code ec)50 void show_status(file_status s, boost::system::error_code ec)
51 {
52 boost::system::error_condition econd;
53
54 if (ec)
55 {
56 econd = ec.default_error_condition();
57 cout << "sets ec to indicate an error:\n"
58 << " ec.value() is " << ec.value() << '\n'
59 << " ec.message() is \"" << ec.message() << "\"\n"
60 << " ec.default_error_condition().value() is " << econd.value() << '\n'
61 << " ec.default_error_condition().message() is \"" << econd.message() << "\"\n";
62 }
63 else
64 cout << "clears ec.\n";
65
66 cout << "s.type() is " << s.type()
67 << ", which is defined as \"" << file_type_c_str(s.type()) << "\"\n";
68
69 cout << "exists(s) is " << (exists(s) ? "true" : "false") << "\n";
70 cout << "status_known(s) is " << (status_known(s) ? "true" : "false") << "\n";
71 cout << "is_regular_file(s) is " << (is_regular_file(s) ? "true" : "false") << "\n";
72 cout << "is_directory(s) is " << (is_directory(s) ? "true" : "false") << "\n";
73 cout << "is_other(s) is " << (is_other(s) ? "true" : "false") << "\n";
74 cout << "is_symlink(s) is " << (is_symlink(s) ? "true" : "false") << "\n";
75 }
76
try_exists()77 void try_exists()
78 {
79 cout << "\nexists(" << p << ") ";
80 try
81 {
82 bool result = exists(p);
83 cout << "is " << (result ? "true" : "false") << "\n";
84 }
85 catch (const filesystem_error& ex)
86 {
87 cout << "throws a filesystem_error exception: " << ex.what() << "\n";
88 }
89 }
90
91 }
92
cpp_main(int argc,char * argv[])93 int cpp_main(int argc, char* argv[])
94 {
95 print_boost_macros();
96
97 if (argc < 2)
98 {
99 std::cout << "Usage: file_status <path>\n";
100 p = argv[0];
101 }
102 else
103 p = argv[1];
104
105 boost::system::error_code ec;
106 file_status s = status(p, ec);
107 cout << "\nfile_status s = status(" << p << ", ec) ";
108 show_status(s, ec);
109
110 s = symlink_status(p, ec);
111 cout << "\nfile_status s = symlink_status(" << p << ", ec) ";
112 show_status(s, ec);
113
114 try_exists();
115
116 return 0;
117 }
118