diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-09-29 12:28:56 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-09-29 12:28:56 (GMT) |
| commit | e8418a8ca01c6d27b9e0dd891a9fa55d8c38bb57 (patch) | |
| tree | c51ba8586da383b609b577758e491a19063ec4f4 /src/cat/LuaScriptInterface.cpp | |
| parent | f96cb74a08c669b34add4ee8ae75044f51a5c24b (diff) | |
| download | powder-e8418a8ca01c6d27b9e0dd891a9fa55d8c38bb57.zip powder-e8418a8ca01c6d27b9e0dd891a9fa55d8c38bb57.tar.gz | |
Lua FileSystem API
Diffstat (limited to 'src/cat/LuaScriptInterface.cpp')
| -rw-r--r-- | src/cat/LuaScriptInterface.cpp | 255 |
1 files changed, 253 insertions, 2 deletions
diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 625c684..dda8d95 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -10,6 +10,7 @@ #include <vector> #include <algorithm> #include <locale> +#include <fstream> #include "Config.h" #include "Format.h" #include "LuaLuna.h" @@ -38,12 +39,15 @@ #include "LuaSlider.h" #include "LuaProgressBar.h" +extern "C" +{ #ifdef WIN #include <direct.h> -#else -#include <sys/stat.h> #endif +#include <sys/stat.h> +#include <dirent.h> #include <time.h> +} GameModel * luacon_model; Simulation * luacon_sim; @@ -93,6 +97,7 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m): initElementsAPI(); initVirtualMachineAPI(); initGraphicsAPI(); + initFileSystemAPI(); //Old TPT API int i = 0, j; @@ -1350,6 +1355,252 @@ int LuaScriptInterface::graphics_fillRect(lua_State * l) return 0; } +void LuaScriptInterface::initFileSystemAPI() +{ + //Methods + struct luaL_reg fileSystemAPIMethods [] = { + {"list", fileSystem_list}, + {"exists", fileSystem_exists}, + {"isFile", fileSystem_isFile}, + {"isDirectory", fileSystem_isDirectory}, + {"makeDirectory", fileSystem_makeDirectory}, + {"removeDirectory", fileSystem_removeDirectory}, + {"removeFile", fileSystem_removeFile}, + {"move", fileSystem_move}, + {"copy", fileSystem_copy}, + {NULL, NULL} + }; + luaL_register(l, "fileSystem", fileSystemAPIMethods); + + //elem shortcut + lua_getglobal(l, "fileSystem"); + lua_setglobal(l, "fs"); + + int fileSystemAPI = lua_gettop(l); +} + +int LuaScriptInterface::fileSystem_list(lua_State * l) +{ + const char * directoryName = lua_tostring(l, 1); + + int index = 1; + lua_newtable(l); + + DIR * directory; + struct dirent * entry; + + directory = opendir(directoryName); + if (directory != NULL) + { + while (entry = readdir(directory)) + { + if(strncmp(entry->d_name, "..", 3) && strncmp(entry->d_name, ".", 2)) + { + lua_pushstring(l, entry->d_name); + lua_rawseti(l, -2, index++); + } + } + closedir(directory); + } + else + { + lua_pushnil(l); + } + + return 1; +} + +int LuaScriptInterface::fileSystem_exists(lua_State * l) +{ + const char * filename = lua_tostring(l, 1); + + bool exists = false; +#ifdef WIN + struct _stat s; + if(_stat(filename, &s) == 0) +#else + struct stat s; + if(stat(filename, &s) == 0) +#endif + { + if(s.st_mode & S_IFDIR) + { + exists = true; + } + else if(s.st_mode & S_IFREG) + { + exists = true; + } + else + { + exists = true; + } + } + else + { + exists = false; + } + + lua_pushboolean(l, exists); + return 1; +} + +int LuaScriptInterface::fileSystem_isFile(lua_State * l) +{ + const char * filename = lua_tostring(l, 1); + + bool exists = false; +#ifdef WIN + struct _stat s; + if(_stat(filename, &s) == 0) +#else + struct stat s; + if(stat(filename, &s) == 0) +#endif + { + if(s.st_mode & S_IFDIR) + { + exists = true; + } + else if(s.st_mode & S_IFREG) + { + exists = false; + } + else + { + exists = false; + } + } + else + { + exists = false; + } + + lua_pushboolean(l, exists); + return 1; +} + +int LuaScriptInterface::fileSystem_isDirectory(lua_State * l) +{ + const char * filename = lua_tostring(l, 1); + + bool exists = false; +#ifdef WIN + struct _stat s; + if(_stat(filename, &s) == 0) +#else + struct stat s; + if(stat(filename, &s) == 0) +#endif + { + if(s.st_mode & S_IFDIR) + { + exists = false; + } + else if(s.st_mode & S_IFREG) + { + exists = true; + } + else + { + exists = false; + } + } + else + { + exists = false; + } + + lua_pushboolean(l, exists); + return 1; +} + +int LuaScriptInterface::fileSystem_makeDirectory(lua_State * l) +{ + const char * filename = lua_tostring(l, 1); + + int ret = 0; +#ifdef WIN + ret = _mkdir(filename); +#else + ret = mkdir(filename, 0755); +#endif + lua_pushboolean(l, ret == 0); + return 1; +} + +int LuaScriptInterface::fileSystem_removeDirectory(lua_State * l) +{ + const char * filename = lua_tostring(l, 1); + + int ret = 0; +#ifdef WIN + ret = _rmdir(filename); +#else + ret = rmdir(filename); +#endif + lua_pushboolean(l, ret == 0); + return 1; +} + +int LuaScriptInterface::fileSystem_removeFile(lua_State * l) +{ + const char * filename = lua_tostring(l, 1); + + int ret = 0; +#ifdef WIN + ret = _unlink(filename); +#else + ret = unlink(filename); +#endif + lua_pushboolean(l, ret == 0); + return 1; +} + +int LuaScriptInterface::fileSystem_move(lua_State * l) +{ + const char * filename = lua_tostring(l, 1); + const char * newFilename = lua_tostring(l, 2); + int ret = 0; + + ret = rename(filename, newFilename); + + lua_pushboolean(l, ret == 0); + return 1; +} + +int LuaScriptInterface::fileSystem_copy(lua_State * l) +{ + const char * filename = lua_tostring(l, 1); + const char * newFilename = lua_tostring(l, 2); + int ret = 0; + + try + { + std::ifstream source(filename, std::ios::binary); + std::ofstream dest(newFilename, std::ios::binary); + source.exceptions(std::ifstream::failbit | std::ifstream::badbit); + dest.exceptions(std::ifstream::failbit | std::ifstream::badbit); + + std::istreambuf_iterator<char> begin_source(source); + std::istreambuf_iterator<char> end_source; + std::ostreambuf_iterator<char> begin_dest(dest); + std::copy(begin_source, end_source, begin_dest); + + source.close(); + dest.close(); + + ret = 0; + } + catch (std::exception & e) + { + ret = 1; + } + + lua_pushboolean(l, ret == 0); + return 1; +} + bool LuaScriptInterface::OnBrushChanged(int brushType, int rx, int ry) { |
