Guest User

Untitled

a guest
May 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <filesystem>
  5. #include <type_traits>
  6.  
  7. namespace fs = std::filesystem;
  8.  
  9. auto operator<< (std::ostream& out, fs::file_type const type) -> std::ostream& {
  10. char c{'\0'};
  11. switch(type) {
  12. case fs::file_type::regular: c = '-'; break;
  13. case fs::file_type::directory: c = 'd'; break;
  14. case fs::file_type::symlink: c = 'l'; break;
  15. default: c = '?';
  16. }
  17. return out << c;
  18. }
  19.  
  20. auto operator<< (std::ostream& out, fs::perms const permissions) -> std::ostream& {
  21. //
  22. char str[10] = "---------";
  23.  
  24. // owner
  25. if ( ( permissions & fs::perms::owner_read ) != fs::perms::none ) str[0] = 'r';
  26. if ( ( permissions & fs::perms::owner_write ) != fs::perms::none ) str[1] = 'w';
  27. if ( ( permissions & fs::perms::owner_exec ) != fs::perms::none ) str[2] = 'x';
  28.  
  29. // group
  30. if ( ( permissions & fs::perms::group_read ) != fs::perms::none ) str[3] = 'r';
  31. if ( ( permissions & fs::perms::group_write ) != fs::perms::none ) str[4] = 'w';
  32. if ( ( permissions & fs::perms::group_exec ) != fs::perms::none ) str[5] = 'x';
  33.  
  34. // others
  35. if ( ( permissions & fs::perms::others_read ) != fs::perms::none ) str[6] = 'r';
  36. if ( ( permissions & fs::perms::others_write ) != fs::perms::none ) str[7] = 'w';
  37. if ( ( permissions & fs::perms::others_exec ) != fs::perms::none ) str[8] = 'x';
  38.  
  39. //
  40. return out << str;
  41. }
  42.  
  43. struct BeautifiedFileSize {
  44. std::uintmax_t size{0};
  45. std::uintmax_t multiplier{0};
  46. };
  47.  
  48. auto beautify_file_size(std::uintmax_t const size) -> BeautifiedFileSize {
  49. BeautifiedFileSize bsize{size, 0};
  50.  
  51. while( bsize.size > 1024 ) {
  52. bsize.size /= 1024;
  53. bsize.multiplier += 1;
  54. }
  55.  
  56. return bsize;
  57. }
  58.  
  59. auto byte_multiplier(std::size_t const index) -> char const* {
  60. static char const* BYTE_MULTIPLIER[] = {
  61. "_B", "kB", "MB", "GB", "TB"
  62. };
  63. return index < std::extent_v<decltype(BYTE_MULTIPLIER)>
  64. ? BYTE_MULTIPLIER[index]
  65. : "??"
  66. ;
  67. }
  68.  
  69.  
  70. auto main() -> int {
  71. fs::path pwd{"."};
  72.  
  73. for(auto const& entry: fs::directory_iterator{pwd}) {
  74. //
  75. auto&& path = entry.path();
  76. auto&& filename = path.filename().string();
  77. auto&& status = fs::status(path);
  78.  
  79. //
  80. auto&& bsize = beautify_file_size(fs::file_size(path));
  81.  
  82. //
  83. std::cout
  84. << status.type()
  85. << status.permissions()
  86. << std::setw(6) << bsize.size << byte_multiplier(bsize.multiplier)
  87. << ' '
  88. << filename << "\n"
  89. ;
  90. }
  91.  
  92. return 0;
  93. }
Add Comment
Please, Sign In to add comment