Skip to content

Commit

Permalink
return system result
Browse files Browse the repository at this point in the history
  • Loading branch information
zzjjzzgggg committed Jun 9, 2017
1 parent 5861032 commit 93acc64
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions os/osutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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<double, std::milli>(dif).count();
}
double milliseconds() const {
auto&& dif = std::chrono::steady_clock::now() - last_tick;
return std::chrono::duration<double, std::milli>(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<std::chrono::milliseconds>(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<std::chrono::milliseconds>(dur).count();
}
};
}

Expand Down

0 comments on commit 93acc64

Please sign in to comment.