aboutsummaryrefslogtreecommitdiffstats
path: root/gulrak-filesystem/examples
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2021-11-10 00:54:13 -0500
committerLibravatarUnit 193 <unit193@unit193.net>2021-11-10 00:54:13 -0500
commitbeb08eb751fa8e1f72042f263316ab5e5ddb596d (patch)
tree3b00df983527648bdae610ac7b88cb639b1f1828 /gulrak-filesystem/examples
parentfbc30002ab3438356c0476e70c4577a0310d52c0 (diff)
New upstream version 2.4.0+dfsg.upstream/2.4.0+dfsgupstream
Diffstat (limited to 'gulrak-filesystem/examples')
-rw-r--r--gulrak-filesystem/examples/CMakeLists.txt17
-rw-r--r--gulrak-filesystem/examples/dir.cpp60
-rw-r--r--gulrak-filesystem/examples/du.cpp61
3 files changed, 138 insertions, 0 deletions
diff --git a/gulrak-filesystem/examples/CMakeLists.txt b/gulrak-filesystem/examples/CMakeLists.txt
new file mode 100644
index 0000000..17bb3d6
--- /dev/null
+++ b/gulrak-filesystem/examples/CMakeLists.txt
@@ -0,0 +1,17 @@
+
+add_executable(fs_dir dir.cpp)
+target_link_libraries(fs_dir ghc_filesystem)
+if(CMAKE_CXX_COMPILER_ID MATCHES MSVC)
+ target_compile_definitions(fs_dir PRIVATE _CRT_SECURE_NO_WARNINGS)
+endif()
+AddExecutableWithStdFS(std_fs_dir dir.cpp)
+
+add_executable(fs_du du.cpp)
+target_link_libraries(fs_du ghc_filesystem)
+AddExecutableWithStdFS(std_fs_du du.cpp)
+
+if(EXISTS "${PROJECT_SOURCE_DIR}/examples/benchmark.cpp")
+ add_executable(fs_benchmark benchmark.cpp)
+ set_property(TARGET fs_benchmark PROPERTY CXX_STANDARD 17)
+ target_link_libraries(fs_benchmark ghc_filesystem)
+endif() \ No newline at end of file
diff --git a/gulrak-filesystem/examples/dir.cpp b/gulrak-filesystem/examples/dir.cpp
new file mode 100644
index 0000000..abd4cc6
--- /dev/null
+++ b/gulrak-filesystem/examples/dir.cpp
@@ -0,0 +1,60 @@
+#include <chrono>
+#include <iomanip>
+#include <iostream>
+#include <string>
+
+#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include)
+#if __has_include(<filesystem>)
+#define GHC_USE_STD_FS
+#include <filesystem>
+namespace fs = std::filesystem;
+#endif
+#endif
+#ifndef GHC_USE_STD_FS
+#include <ghc/filesystem.hpp>
+namespace fs = ghc::filesystem;
+#endif
+
+template <typename TP>
+std::time_t to_time_t(TP tp)
+{
+ // Based on trick from: Nico Josuttis, C++17 - The Complete Guide
+ std::chrono::system_clock::duration dt = std::chrono::duration_cast<std::chrono::system_clock::duration>(tp - TP::clock::now());
+ return std::chrono::system_clock::to_time_t(std::chrono::system_clock::now() + dt);
+}
+
+static std::string perm_to_str(fs::perms prms)
+{
+ std::string result;
+ result.reserve(9);
+ for (int i = 0; i < 9; ++i) {
+ result = ((static_cast<int>(prms) & (1 << i)) ? "xwrxwrxwr"[i] : '-') + result;
+ }
+ return result;
+}
+
+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: dir <path>" << std::endl;
+ exit(1);
+ }
+ fs::path dir{"."};
+ if (argc == 2) {
+ dir = fs::u8path(argv[1]);
+ }
+ for (auto de : fs::directory_iterator(dir)) {
+ auto ft = to_time_t(de.last_write_time());
+ auto ftm = *std::localtime(&ft);
+ std::cout << (de.is_directory() ? "d" : "-") << perm_to_str(de.symlink_status().permissions()) << " " << std::setw(8) << (de.is_directory() ? "-" : std::to_string(de.file_size())) << " " << std::put_time(&ftm, "%Y-%m-%d %H:%M:%S") << " "
+ << de.path().filename().string() << std::endl;
+ }
+ return 0;
+}
diff --git a/gulrak-filesystem/examples/du.cpp b/gulrak-filesystem/examples/du.cpp
new file mode 100644
index 0000000..929b809
--- /dev/null
+++ b/gulrak-filesystem/examples/du.cpp
@@ -0,0 +1,61 @@
+#include <iostream>
+#include <iomanip>
+#include <chrono>
+
+#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include)
+#if __has_include(<filesystem>)
+#define GHC_USE_STD_FS
+#include <filesystem>
+namespace fs = std::filesystem;
+#endif
+#endif
+#ifndef GHC_USE_STD_FS
+#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 << " files and " << totalDirs << " directories, maximum depth: " << maxDepth << std::endl;
+ return 0;
+}