From 93acc645a3b34eb3f56e33b49578608634036525 Mon Sep 17 00:00:00 2001 From: "J. Zhao" Date: Fri, 9 Jun 2017 17:57:23 +0800 Subject: [PATCH] return system result --- os/osutils.h | 80 +++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/os/osutils.h b/os/osutils.h index 8948d9c..2462501 100644 --- a/os/osutils.h +++ b/os/osutils.h @@ -18,23 +18,23 @@ namespace osutils { * test the existence a file/dir */ inline bool exists(const std::string& file_or_dir) { - struct stat buffer; - return (stat(file_or_dir.c_str(), &buffer) == 0); + struct stat buffer; + return (stat(file_or_dir.c_str(), &buffer) == 0); } -inline void mkdirs(const std::string& filename) { - system(fmt::format("mkdir -p {}", filename).c_str()); +inline int mkdirs(const std::string& filename) { + return system(fmt::format("mkdir -p {}", filename).c_str()); } -inline void rmfile(const std::string& filename) { - system(fmt::format("rm -rf {}", filename).c_str()); +inline int rmfile(const std::string& filename) { + return system(fmt::format("rm -rf {}", filename).c_str()); } inline char pathSeparator() { #ifdef _WIN32 - return '\\'; + return '\\'; #else - return '/'; + return '/'; #endif } @@ -47,45 +47,47 @@ std::string join(const std::string& parent, const std::string& child1, class Timer { private: - std::chrono::steady_clock::time_point last_tick; + std::chrono::steady_clock::time_point last_tick; public: - Timer() { tick(); } + Timer() { tick(); } - void tick() { last_tick = std::chrono::steady_clock::now(); } + void tick() { last_tick = std::chrono::steady_clock::now(); } - double milliseconds() const { - auto&& dif = std::chrono::steady_clock::now() - last_tick; - return std::chrono::duration(dif).count(); - } + double milliseconds() const { + auto&& dif = std::chrono::steady_clock::now() - last_tick; + return std::chrono::duration(dif).count(); + } - double seconds() const { return milliseconds() / 1000; } + double seconds() const { return milliseconds() / 1000; } + + const std::string getStr() const { return prettyTime(seconds()); } + + static std::string prettyTime(const double secs) { + if (secs < 60) + return fmt::format("{:.2f}s", secs); + else { + int isecs = int(secs); + if (isecs < 3600) + return fmt::format("{:02d}m{:02d}s", isecs / 60, isecs % 60); + else + return fmt::format("{:02d}h{:02d}m", isecs / 3600, + isecs % 3600 / 60); + } + } - const std::string getStr() const { return prettyTime(seconds()); } + static char* curTime() { + std::time_t t = std::time(NULL); + static char buf[100]; + std::strftime(buf, sizeof(buf), "%F %T", std::localtime(&t)); + return buf; + } - static std::string prettyTime(const double secs) { - if (secs < 60) - return fmt::format("{:.2f}s", secs); - else { - int isecs = int(secs); - if (isecs < 3600) - return fmt::format("{:02d}m{:02d}s", isecs / 60, isecs % 60); - else - return fmt::format("{:02d}h{:02d}m", isecs / 3600, isecs % 3600 / 60); + static int timestamp() { + auto dur = std::chrono::steady_clock::now().time_since_epoch(); + return std::chrono::duration_cast(dur) + .count(); } - } - - static char* curTime() { - std::time_t t = std::time(NULL); - static char buf[100]; - std::strftime(buf, sizeof(buf), "%F %T", std::localtime(&t)); - return buf; - } - - static int timestamp() { - auto dur = std::chrono::steady_clock::now().time_since_epoch(); - return std::chrono::duration_cast(dur).count(); - } }; }