diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 2d44c04d2..37ee2252e 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1985,6 +1985,9 @@ static const LuaWrapper::FunctionReg dfhack_filesystem_module[] = { WRAPM(Filesystem, exists), WRAPM(Filesystem, isfile), WRAPM(Filesystem, isdir), + WRAPM(Filesystem, atime), + WRAPM(Filesystem, ctime), + WRAPM(Filesystem, mtime), {NULL, NULL} }; diff --git a/library/include/modules/Filesystem.h b/library/include/modules/Filesystem.h index dd30b03c3..0e996e7e2 100644 --- a/library/include/modules/Filesystem.h +++ b/library/include/modules/Filesystem.h @@ -88,16 +88,6 @@ SOFTWARE. #include #endif -typedef struct dir_data { - int closed; -#ifdef _WIN32 - intptr_t hFile; - char pattern[MAX_PATH+1]; -#else - DIR *dir; -#endif -} dir_data; - #ifdef _WIN32 #ifdef __BORLANDC__ #define lfs_setmode(L,file,m) ((void)L, setmode(_fileno(file), m)) @@ -164,5 +154,8 @@ namespace DFHack { DFHACK_EXPORT _filetype filetype (std::string path); DFHACK_EXPORT bool isfile (std::string path); DFHACK_EXPORT bool isdir (std::string path); + DFHACK_EXPORT int64_t atime (std::string path); + DFHACK_EXPORT int64_t ctime (std::string path); + DFHACK_EXPORT int64_t mtime (std::string path); } } diff --git a/library/modules/Filesystem.cpp b/library/modules/Filesystem.cpp index 20ac6fa4a..5b4a049e9 100644 --- a/library/modules/Filesystem.cpp +++ b/library/modules/Filesystem.cpp @@ -49,12 +49,14 @@ SOFTWARE. #include "modules/Filesystem.h" -bool DFHack::Filesystem::chdir (std::string path) +using namespace DFHack; + +bool Filesystem::chdir (std::string path) { return !(bool)::chdir(path.c_str()); } -std::string DFHack::Filesystem::getcwd () +std::string Filesystem::getcwd () { char *path; char buf[FILENAME_MAX]; @@ -68,7 +70,7 @@ std::string DFHack::Filesystem::getcwd () return result; } -bool DFHack::Filesystem::mkdir (std::string path) +bool Filesystem::mkdir (std::string path) { int fail; #ifdef _WIN32 @@ -80,7 +82,7 @@ bool DFHack::Filesystem::mkdir (std::string path) return !(bool)fail; } -bool DFHack::Filesystem::rmdir (std::string path) +bool Filesystem::rmdir (std::string path) { int fail; #ifdef _WIN32 @@ -114,31 +116,45 @@ _filetype mode2type (mode_t mode) { return FILETYPE_UNKNOWN; } -bool DFHack::Filesystem::stat (std::string path, STAT_STRUCT &info) +bool Filesystem::stat (std::string path, STAT_STRUCT &info) { return !(bool)(STAT_FUNC(path.c_str(), &info)); } -bool DFHack::Filesystem::exists (std::string path) +bool Filesystem::exists (std::string path) { STAT_STRUCT info; - return (bool)DFHack::Filesystem::stat(path.c_str(), info); + return (bool)Filesystem::stat(path.c_str(), info); } -#include -_filetype DFHack::Filesystem::filetype (std::string path) +_filetype Filesystem::filetype (std::string path) { STAT_STRUCT info; - DFHack::Filesystem::stat(path, info); + Filesystem::stat(path, info); return mode2type(info.st_mode); } -bool DFHack::Filesystem::isfile (std::string path) +bool Filesystem::isfile (std::string path) { - return DFHack::Filesystem::filetype(path) == FILETYPE_FILE; + return Filesystem::filetype(path) == FILETYPE_FILE; } -bool DFHack::Filesystem::isdir (std::string path) +bool Filesystem::isdir (std::string path) { - return DFHack::Filesystem::filetype(path) == FILETYPE_DIRECTORY; + return Filesystem::filetype(path) == FILETYPE_DIRECTORY; +} + +#define DEFINE_STAT_TIME_WRAPPER(attr) \ +int64_t Filesystem::attr (std::string path) \ +{ \ + STAT_STRUCT info; \ + if (!Filesystem::stat(path.c_str(), info)) \ + return -1; \ + return (int64_t)info.st_##attr; \ } + +DEFINE_STAT_TIME_WRAPPER(atime); +DEFINE_STAT_TIME_WRAPPER(ctime); +DEFINE_STAT_TIME_WRAPPER(mtime); + +#undef DEFINE_STAT_TIME_WRAPPER