Steffen Schuemann dc37276a9a Squashed commit of the following:
commit 8d9f245e379d373942c0f49be19dacc9293cdcf0
Merge: 3ceb4b0 857a355
Author: gulrak <s.schuemann@pobox.com>
Date:   Fri May 3 23:52:57 2019 +0200

    Merge branch 'development-v1.1' of https://github.com/gulrak/filesystem into development-v1.1

commit 3ceb4b0e4fe90be46b1fdbdfb7e2b3f160cccf3c
Author: gulrak <s.schuemann@pobox.com>
Date:   Fri May 3 23:51:48 2019 +0200

    Less noisy interoperability testing on weakly_canonial()

commit 38f82a90ef5f4ec1fa341ce3ab34f3836a6bd9ea
Author: gulrak <s.schuemann@pobox.com>
Date:   Fri May 3 23:50:43 2019 +0200

    Additional fix for recursive_directory_iterator

commit 857a3558fb13e8fae76e29b5b91cb76b5e98ea20
Merge: 9da5784 b557b5b
Author: Steffen Schuemann <s.schuemann@pobox.com>
Date:   Fri May 3 23:47:20 2019 +0200

    Merge branch 'development-v1.1' of github.com:gulrak/filesystem into development-v1.1

    # Conflicts:
    #	test/filesystem_test.cpp

commit 9da57846ac4de06c4b0521ab69ea42816fd6a029
Merge: 96e89ae 43e75c3
Author: Steffen Schuemann <s.schuemann@pobox.com>
Date:   Fri May 3 23:42:31 2019 +0200

    Merge remote-tracking branch 'origin/master' into development-v1.1

commit 96e89ae7f9e983cb3d0977d9ba77f421a0c06b0f
Merge: 9d71161 882c60b
Author: Steffen Schuemann <s.schuemann@pobox.com>
Date:   Fri May 3 23:40:18 2019 +0200

    Merge remote-tracking branch 'origin/master' into development-v1.1

commit b557b5b476c085183bd3da72cc9f511df3b52272
Merge: 263a2d2 9d71161
Author: Steffen Schümann <s.schuemann@pobox.com>
Date:   Fri May 3 22:44:25 2019 +0200

    Merge branch 'development-v1.1' of https://github.com/gulrak/filesystem into development-v1.1

commit 263a2d2c5b9f25631513271a6e26a9cb66e8f9da
Author: Steffen Schümann <s.schuemann@pobox.com>
Date:   Fri May 3 22:44:01 2019 +0200

    Work on Windows tests.

commit 9d71161323f22e994b51fda73ebc19af9e67d6e6
Author: Steffen Schuemann <s.schuemann@pobox.com>
Date:   Fri May 3 08:30:40 2019 +0200

    Fix for issues with recursive_directory_iterator not behaving like an input iterator and regarding pop()/depth().

commit 2ac1352b4a37fdb247b56fb63c1e2c3f56761649
Author: gulrak <s.schuemann@pobox.com>
Date:   Thu May 2 09:12:42 2019 +0200

    Refactored builds with std::fs to a CMake macro, added du example, added behaviour switch

commit 624a6f63e5639504c2b554ba89985ef9c01c6fa9
Author: Steffen Schümann <s.schuemann@pobox.com>
Date:   Thu May 2 09:01:15 2019 +0200

    Add path parameters to filesystem_error::what
2019-05-05 08:51:53 +02:00

58 lines
1.5 KiB
C++

#include <iostream>
#include <iomanip>
#include <chrono>
#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include) && __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <ghc/filesystem.hpp>
namespace fs = ghc::filesystem;
#endif
int main(int argc, char* argv[])
{
#ifdef GHC_FILESYSTEM_VERSION
fs::u8arguments u8guard(argc, argv);
if(!u8guard.valid()) {
std::cerr << "Invalid character encoding, UTF-8 based encoding needed." << std::endl;
std::exit(EXIT_FAILURE);
}
#endif
if(argc > 2) {
std::cerr << "USAGE: du <path>" << std::endl;
exit(1);
}
fs::path dir{"."};
if(argc == 2) {
dir = fs::u8path(argv[1]);
}
uint64_t totalSize = 0;
int totalDirs = 0;
int totalFiles = 0;
int maxDepth = 0;
try {
auto rdi = fs::recursive_directory_iterator(dir);
for(auto de : rdi) {
if(rdi.depth() > maxDepth) {
maxDepth = rdi.depth();
}
if(de.is_regular_file()) {
totalSize += de.file_size();
++totalFiles;
}
else if(de.is_directory()) {
++totalDirs;
}
}
}
catch(fs::filesystem_error fe) {
std::cerr << "Error: " << fe.what() << std::endl;
exit(1);
}
std::cout << totalSize << " bytes in " << totalFiles << " and " << totalDirs << " directories, maximum depth: " << maxDepth << std::endl;
return 0;
}