file_status.cpp 3.2 KB

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