From ff279652bce90fe47f2cf5853561e7ccb2150fdc Mon Sep 17 00:00:00 2001 From: qPCR4vir Date: Fri, 26 Feb 2016 15:24:51 +0100 Subject: [PATCH] two pretty print functions in filesystem_ext --- include/nana/filesystem/filesystem_ext.hpp | 50 +++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/include/nana/filesystem/filesystem_ext.hpp b/include/nana/filesystem/filesystem_ext.hpp index 3c911b2b..abcdc5d3 100644 --- a/include/nana/filesystem/filesystem_ext.hpp +++ b/include/nana/filesystem/filesystem_ext.hpp @@ -108,6 +108,54 @@ inline regular_file_only_iterator end(const regular_file_only_iterator&) noexcep { return{}; } - + +inline std::string pretty_file_size(const std::experimental::filesystem::path& path) // todo: move to .cpp +{ + try { + std::size_t bytes = std::experimental::filesystem::file_size ( path ); + const char * ustr[] = { " KB", " MB", " GB", " TB" }; + std::stringstream ss; + if (bytes < 1024) + ss << bytes << " Bytes"; + else + { + double cap = bytes / 1024.0; + std::size_t uid = 0; + while ((cap >= 1024.0) && (uid < sizeof(ustr) / sizeof(char *))) + { + cap /= 1024.0; + ++uid; + } + ss << cap; + auto s = ss.str(); + auto pos = s.find('.'); + if (pos != s.npos) + { + if (pos + 2 < s.size()) + s.erase(pos + 2); + } + return s + ustr[uid]; + } + + return ss.str(); + } + catch (...) {} + return {}; +} + +inline std::string pretty_file_date(const std::experimental::filesystem::path& path) // todo: move to .cpp +{ + try { + auto ftime = std::experimental::filesystem::last_write_time(path); + std::time_t cftime = decltype(ftime)::clock::to_time_t(ftime); + std::stringstream tm; + tm << std::put_time(std::localtime(&cftime), "%Y-%m-%d, %H:%M:%S"); + return tm.str(); + } + catch (...) { + return {}; + } +} + }}}} #endif //NANA_FILESYSTEM_EXT_HPP