Add atime()/ctime()/mtime()

develop
lethosor 2015-01-28 18:18:06 -05:00
parent 4e64d6633d
commit e8c0482fdc
3 changed files with 36 additions and 24 deletions

@ -1985,6 +1985,9 @@ static const LuaWrapper::FunctionReg dfhack_filesystem_module[] = {
WRAPM(Filesystem, exists), WRAPM(Filesystem, exists),
WRAPM(Filesystem, isfile), WRAPM(Filesystem, isfile),
WRAPM(Filesystem, isdir), WRAPM(Filesystem, isdir),
WRAPM(Filesystem, atime),
WRAPM(Filesystem, ctime),
WRAPM(Filesystem, mtime),
{NULL, NULL} {NULL, NULL}
}; };

@ -88,16 +88,6 @@ SOFTWARE.
#include <utime.h> #include <utime.h>
#endif #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 _WIN32
#ifdef __BORLANDC__ #ifdef __BORLANDC__
#define lfs_setmode(L,file,m) ((void)L, setmode(_fileno(file), m)) #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 _filetype filetype (std::string path);
DFHACK_EXPORT bool isfile (std::string path); DFHACK_EXPORT bool isfile (std::string path);
DFHACK_EXPORT bool isdir (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);
} }
} }

@ -49,12 +49,14 @@ SOFTWARE.
#include "modules/Filesystem.h" #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()); return !(bool)::chdir(path.c_str());
} }
std::string DFHack::Filesystem::getcwd () std::string Filesystem::getcwd ()
{ {
char *path; char *path;
char buf[FILENAME_MAX]; char buf[FILENAME_MAX];
@ -68,7 +70,7 @@ std::string DFHack::Filesystem::getcwd ()
return result; return result;
} }
bool DFHack::Filesystem::mkdir (std::string path) bool Filesystem::mkdir (std::string path)
{ {
int fail; int fail;
#ifdef _WIN32 #ifdef _WIN32
@ -80,7 +82,7 @@ bool DFHack::Filesystem::mkdir (std::string path)
return !(bool)fail; return !(bool)fail;
} }
bool DFHack::Filesystem::rmdir (std::string path) bool Filesystem::rmdir (std::string path)
{ {
int fail; int fail;
#ifdef _WIN32 #ifdef _WIN32
@ -114,31 +116,45 @@ _filetype mode2type (mode_t mode) {
return FILETYPE_UNKNOWN; 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)); return !(bool)(STAT_FUNC(path.c_str(), &info));
} }
bool DFHack::Filesystem::exists (std::string path) bool Filesystem::exists (std::string path)
{ {
STAT_STRUCT info; STAT_STRUCT info;
return (bool)DFHack::Filesystem::stat(path.c_str(), info); return (bool)Filesystem::stat(path.c_str(), info);
} }
#include <iostream> _filetype Filesystem::filetype (std::string path)
_filetype DFHack::Filesystem::filetype (std::string path)
{ {
STAT_STRUCT info; STAT_STRUCT info;
DFHack::Filesystem::stat(path, info); Filesystem::stat(path, info);
return mode2type(info.st_mode); 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