From 1daba5b2fa044c1822d104fb65eeb88b6aeb5499 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 9 Oct 2015 10:32:03 +0200 Subject: [PATCH] function to get mtime --- src/io/filesystem.cpp | 63 ++++++++++++++++++++++++++++++------------- src/io/filesystem.hpp | 6 +++++ 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/io/filesystem.cpp b/src/io/filesystem.cpp index 8a06d1c..1fd94a0 100644 --- a/src/io/filesystem.cpp +++ b/src/io/filesystem.cpp @@ -9,36 +9,61 @@ namespace blank { -bool is_dir(const char *path) { +namespace { #ifdef _WIN32 - struct _stat info; - if (_stat(path, &info) != 0) { - return false; + using Stat = struct _stat; + int do_stat(const char *path, Stat &info) { + return _stat(path, &info); + } + bool is_dir(const Stat &info) { + return (info.st_mode & _S_IFDIR) != 0; + } + bool is_file(const Stat &info) { + return (info.st_mode & _S_IFEG) != 0; } - return (info.st_mode & _S_IFDIR) != 0; #else - struct stat info; - if (stat(path, &info) != 0) { - return false; + using Stat = struct stat; + int do_stat(const char *path, Stat &info) { + return stat(path, &info); + } + bool is_dir(const Stat &info) { + return S_ISDIR(info.st_mode); } - return S_ISDIR(info.st_mode); + bool is_file(const Stat &info) { + return S_ISREG(info.st_mode); + } +#endif + std::time_t get_mtime(const Stat &info) { +#ifdef __APPLE__ + return info.st_mtimespec.tv_sec; +#else + return info.st_mtime; #endif + } } -bool is_file(const char *path) { -#ifdef _WIN32 - struct _stat info; - if (_stat(path, &info) != 0) { +bool is_dir(const char *path) { + Stat info; + if (do_stat(path, info) != 0) { return false; } - return (info.st_mode & _S_IFREG) != 0; -#else - struct stat info; - if (stat(path, &info) != 0) { + return is_dir(info); +} + +bool is_file(const char *path) { + Stat info; + if (do_stat(path, info) != 0) { return false; } - return S_ISREG(info.st_mode); -#endif + return is_file(info); +} + +std::time_t file_mtime(const char *path) { + Stat info; + if (do_stat(path, info) != 0) { + return 0; + } + return get_mtime(info); } diff --git a/src/io/filesystem.hpp b/src/io/filesystem.hpp index 3829e0d..b69352a 100644 --- a/src/io/filesystem.hpp +++ b/src/io/filesystem.hpp @@ -1,6 +1,7 @@ #ifndef BLANK_IO_FILESYSTEM_HPP_ #define BLANK_IO_FILESYSTEM_HPP_ +#include #include @@ -16,6 +17,11 @@ bool is_file(const char *); inline bool is_file(const std::string &s) { return is_file(s.c_str()); } +/// get timestamp of last modification +std::time_t file_mtime(const char *); +inline std::time_t file_mtime(const std::string &s) { + return file_mtime(s.c_str()); +} /// create given directory /// @return true if the directory was created -- 2.39.2