Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <filesystem>
- #include <type_traits>
- namespace fs = std::filesystem;
- auto operator<< (std::ostream& out, fs::file_type const type) -> std::ostream& {
- char c{'\0'};
- switch(type) {
- case fs::file_type::regular: c = '-'; break;
- case fs::file_type::directory: c = 'd'; break;
- case fs::file_type::symlink: c = 'l'; break;
- default: c = '?';
- }
- return out << c;
- }
- auto operator<< (std::ostream& out, fs::perms const permissions) -> std::ostream& {
- //
- char str[10] = "---------";
- // owner
- if ( ( permissions & fs::perms::owner_read ) != fs::perms::none ) str[0] = 'r';
- if ( ( permissions & fs::perms::owner_write ) != fs::perms::none ) str[1] = 'w';
- if ( ( permissions & fs::perms::owner_exec ) != fs::perms::none ) str[2] = 'x';
- // group
- if ( ( permissions & fs::perms::group_read ) != fs::perms::none ) str[3] = 'r';
- if ( ( permissions & fs::perms::group_write ) != fs::perms::none ) str[4] = 'w';
- if ( ( permissions & fs::perms::group_exec ) != fs::perms::none ) str[5] = 'x';
- // others
- if ( ( permissions & fs::perms::others_read ) != fs::perms::none ) str[6] = 'r';
- if ( ( permissions & fs::perms::others_write ) != fs::perms::none ) str[7] = 'w';
- if ( ( permissions & fs::perms::others_exec ) != fs::perms::none ) str[8] = 'x';
- //
- return out << str;
- }
- struct BeautifiedFileSize {
- std::uintmax_t size{0};
- std::uintmax_t multiplier{0};
- };
- auto beautify_file_size(std::uintmax_t const size) -> BeautifiedFileSize {
- BeautifiedFileSize bsize{size, 0};
- while( bsize.size > 1024 ) {
- bsize.size /= 1024;
- bsize.multiplier += 1;
- }
- return bsize;
- }
- auto byte_multiplier(std::size_t const index) -> char const* {
- static char const* BYTE_MULTIPLIER[] = {
- "_B", "kB", "MB", "GB", "TB"
- };
- return index < std::extent_v<decltype(BYTE_MULTIPLIER)>
- ? BYTE_MULTIPLIER[index]
- : "??"
- ;
- }
- auto main() -> int {
- fs::path pwd{"."};
- for(auto const& entry: fs::directory_iterator{pwd}) {
- //
- auto&& path = entry.path();
- auto&& filename = path.filename().string();
- auto&& status = fs::status(path);
- //
- auto&& bsize = beautify_file_size(fs::file_size(path));
- //
- std::cout
- << status.type()
- << status.permissions()
- << std::setw(6) << bsize.size << byte_multiplier(bsize.multiplier)
- << ' '
- << filename << "\n"
- ;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment