diff options
author | Remko Tronçon <git@el-tramo.be> | 2014-07-11 18:33:44 (GMT) |
---|---|---|
committer | Swift Review <review@swift.im> | 2014-07-14 14:39:03 (GMT) |
commit | 00375bb2ac48dae174889ed9cea8dc8de55e1efd (patch) | |
tree | b4995c82217dd9ac0fbd9d9b9f371f8b39c6d933 | |
parent | 4386fa0e6fa8c361d51ec085aefa2d15a61d399a (diff) | |
download | swift-00375bb2ac48dae174889ed9cea8dc8de55e1efd.zip swift-00375bb2ac48dae174889ed9cea8dc8de55e1efd.tar.bz2 |
Sluift: Add 'fs' module
The 'fs' module provides filesystem functions.
Currently has function to list directory contents and test
whether a path is a file.
Test-Information:
Tested in external script.
Change-Id: I14ba614b0b3bd52f5d9e87a40dc6477d99604d88
-rw-r--r-- | Sluift/sluift.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index 2fd1e50..5e837c1 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -12,6 +12,7 @@ #include <boost/bind.hpp> #include <boost/numeric/conversion/cast.hpp> #include <boost/assign/list_of.hpp> +#include <boost/filesystem.hpp> #include "Watchdog.h" #include <Sluift/Lua/Check.h> @@ -251,6 +252,37 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( return 1; } +/******************************************************************************* + * Filesystem Functions + ******************************************************************************/ + +SLUIFT_LUA_FUNCTION(FS, list) { + boost::filesystem::path dir(std::string(Lua::checkString(L, 1))); + if (!boost::filesystem::exists(dir) || !boost::filesystem::is_directory(dir)) { + lua_pushnil(L); + lua_pushstring(L, "Argument is not an existing directory"); + return 2; + } + + boost::filesystem::directory_iterator i(dir); + std::vector<boost::filesystem::path> items( + i, boost::filesystem::directory_iterator()); + + lua_createtable(L, boost::numeric_cast<int>(items.size()), 0); + for (size_t i = 0; i < items.size(); ++i) { + lua_pushstring(L, items[i].string().c_str()); + lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); + } + Lua::registerTableToString(L, -1); + return 1; +} + +SLUIFT_LUA_FUNCTION(FS, is_file) { + boost::filesystem::path file(std::string(Lua::checkString(L, 1))); + lua_pushboolean(L, boost::filesystem::is_regular_file(file)); + return 1; +} + /******************************************************************************* * JID Functions @@ -410,6 +442,8 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { lua_setfield(L, -2, "idn"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "Crypto"); lua_setfield(L, -2, "crypto"); + Lua::FunctionRegistry::getInstance().createFunctionTable(L, "FS"); + lua_setfield(L, -2, "fs"); #ifdef HAVE_ITUNES Lua::FunctionRegistry::getInstance().createFunctionTable(L, "iTunes"); lua_setfield(L, -2, "itunes"); |