Add Filesystem::listdir()

develop
lethosor 2015-01-28 19:15:58 -05:00
parent e8c0482fdc
commit bebceffa5f
7 changed files with 52 additions and 34 deletions

@ -45,6 +45,7 @@ using namespace std;
#include "PluginManager.h" #include "PluginManager.h"
#include "ModuleFactory.h" #include "ModuleFactory.h"
#include "modules/EventManager.h" #include "modules/EventManager.h"
#include "modules/Filesystem.h"
#include "modules/Gui.h" #include "modules/Gui.h"
#include "modules/World.h" #include "modules/World.h"
#include "modules/Graphic.h" #include "modules/Graphic.h"
@ -252,7 +253,7 @@ static std::string getScriptHelp(std::string path, std::string helpprefix)
static void listScripts(PluginManager *plug_mgr, std::map<string,string> &pset, std::string path, bool all, std::string prefix = "") static void listScripts(PluginManager *plug_mgr, std::map<string,string> &pset, std::string path, bool all, std::string prefix = "")
{ {
std::vector<string> files; std::vector<string> files;
getdir(path, files); Filesystem::listdir(path, files);
for (size_t i = 0; i < files.size(); i++) for (size_t i = 0; i < files.size(); i++)
{ {

@ -1991,6 +1991,26 @@ static const LuaWrapper::FunctionReg dfhack_filesystem_module[] = {
{NULL, NULL} {NULL, NULL}
}; };
static int filesystem_listdir(lua_State *L)
{
luaL_checktype(L,1,LUA_TSTRING);
std::string dir=lua_tostring(L,1);
std::vector<std::string> files;
DFHack::Filesystem::listdir(dir, files);
lua_newtable(L);
for(int i=0;i<files.size();i++)
{
lua_pushinteger(L,i+1);
lua_pushstring(L,files[i].c_str());
lua_settable(L,-3);
}
return 1;
}
static const luaL_Reg dfhack_filesystem_funcs[] = {
{"listdir", filesystem_listdir},
{NULL, NULL}
};
/***** Internal module *****/ /***** Internal module *****/
@ -2283,21 +2303,6 @@ static int internal_diffscan(lua_State *L)
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;
} }
static int internal_getDir(lua_State *L)
{
luaL_checktype(L,1,LUA_TSTRING);
std::string dir=lua_tostring(L,1);
std::vector<std::string> files;
DFHack::getdir(dir,files);
lua_newtable(L);
for(int i=0;i<files.size();i++)
{
lua_pushinteger(L,i+1);
lua_pushstring(L,files[i].c_str());
lua_settable(L,-3);
}
return 1;
}
static int internal_runCommand(lua_State *L) static int internal_runCommand(lua_State *L)
{ {
@ -2384,7 +2389,7 @@ static const luaL_Reg dfhack_internal_funcs[] = {
{ "memcmp", internal_memcmp }, { "memcmp", internal_memcmp },
{ "memscan", internal_memscan }, { "memscan", internal_memscan },
{ "diffscan", internal_diffscan }, { "diffscan", internal_diffscan },
{ "getDir", internal_getDir }, { "getDir", filesystem_listdir },
{ "runCommand", internal_runCommand }, { "runCommand", internal_runCommand },
{ "getModifiers", internal_getModifiers }, { "getModifiers", internal_getModifiers },
{ NULL, NULL } { NULL, NULL }
@ -2412,6 +2417,6 @@ void OpenDFHackApi(lua_State *state)
OpenModule(state, "buildings", dfhack_buildings_module, dfhack_buildings_funcs); OpenModule(state, "buildings", dfhack_buildings_module, dfhack_buildings_funcs);
OpenModule(state, "constructions", dfhack_constructions_module); OpenModule(state, "constructions", dfhack_constructions_module);
OpenModule(state, "screen", dfhack_screen_module, dfhack_screen_funcs); OpenModule(state, "screen", dfhack_screen_module, dfhack_screen_funcs);
OpenModule(state, "filesystem", dfhack_filesystem_module); OpenModule(state, "filesystem", dfhack_filesystem_module, dfhack_filesystem_funcs);
OpenModule(state, "internal", dfhack_internal_module, dfhack_internal_funcs); OpenModule(state, "internal", dfhack_internal_module, dfhack_internal_funcs);
} }

@ -23,6 +23,7 @@ distribution.
*/ */
#include "modules/EventManager.h" #include "modules/EventManager.h"
#include "modules/Filesystem.h"
#include "Internal.h" #include "Internal.h"
#include "Core.h" #include "Core.h"
#include "MemAccess.h" #include "MemAccess.h"
@ -727,7 +728,7 @@ void PluginManager::init(Core * core)
const string searchstr = ".plug.dll"; const string searchstr = ".plug.dll";
#endif #endif
vector <string> filez; vector <string> filez;
getdir(path, filez); Filesystem::listdir(path, filez);
for(size_t i = 0; i < filez.size();i++) for(size_t i = 0; i < filez.size();i++)
{ {
if(hasEnding(filez[i],searchstr)) if(hasEnding(filez[i],searchstr))

@ -28,6 +28,8 @@ distribution.
#include "Error.h" #include "Error.h"
#include "Types.h" #include "Types.h"
#include "modules/Filesystem.h"
#ifndef LINUX_BUILD #ifndef LINUX_BUILD
#include <Windows.h> #include <Windows.h>
#include "wdirent.h" #include "wdirent.h"
@ -47,17 +49,7 @@ distribution.
int DFHack::getdir(std::string dir, std::vector<std::string> &files) int DFHack::getdir(std::string dir, std::vector<std::string> &files)
{ {
DIR *dp; return DFHack::Filesystem::listdir(dir, files);
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL)
{
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
files.push_back(std::string(dirp->d_name));
}
closedir(dp);
return 0;
} }
bool DFHack::hasEnding (std::string const &fullString, std::string const &ending) bool DFHack::hasEnding (std::string const &fullString, std::string const &ending)

@ -47,6 +47,7 @@ SOFTWARE.
#pragma once #pragma once
#include "Export.h" #include "Export.h"
#include <vector>
#ifndef _WIN32 #ifndef _WIN32
#ifndef _AIX #ifndef _AIX
@ -80,6 +81,7 @@ SOFTWARE.
#include <sys/utime.h> #include <sys/utime.h>
#endif #endif
#include <fcntl.h> #include <fcntl.h>
#include "wdirent.h"
#else #else
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
@ -157,5 +159,6 @@ namespace DFHack {
DFHACK_EXPORT int64_t atime (std::string path); DFHACK_EXPORT int64_t atime (std::string path);
DFHACK_EXPORT int64_t ctime (std::string path); DFHACK_EXPORT int64_t ctime (std::string path);
DFHACK_EXPORT int64_t mtime (std::string path); DFHACK_EXPORT int64_t mtime (std::string path);
DFHACK_EXPORT int listdir (std::string dir, std::vector<std::string> &files);
} }
} }

@ -124,7 +124,7 @@ bool Filesystem::stat (std::string path, STAT_STRUCT &info)
bool Filesystem::exists (std::string path) bool Filesystem::exists (std::string path)
{ {
STAT_STRUCT info; STAT_STRUCT info;
return (bool)Filesystem::stat(path.c_str(), info); return (bool)Filesystem::stat(path, info);
} }
_filetype Filesystem::filetype (std::string path) _filetype Filesystem::filetype (std::string path)
@ -148,7 +148,7 @@ bool Filesystem::isdir (std::string path)
int64_t Filesystem::attr (std::string path) \ int64_t Filesystem::attr (std::string path) \
{ \ { \
STAT_STRUCT info; \ STAT_STRUCT info; \
if (!Filesystem::stat(path.c_str(), info)) \ if (!Filesystem::stat(path, info)) \
return -1; \ return -1; \
return (int64_t)info.st_##attr; \ return (int64_t)info.st_##attr; \
} }
@ -158,3 +158,19 @@ DEFINE_STAT_TIME_WRAPPER(ctime);
DEFINE_STAT_TIME_WRAPPER(mtime); DEFINE_STAT_TIME_WRAPPER(mtime);
#undef DEFINE_STAT_TIME_WRAPPER #undef DEFINE_STAT_TIME_WRAPPER
int Filesystem::listdir (std::string dir, std::vector<std::string> &files)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL)
{
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
files.push_back(std::string(dirp->d_name));
}
closedir(dp);
return 0;
}