From 00375bb2ac48dae174889ed9cea8dc8de55e1efd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Fri, 11 Jul 2014 20:33:44 +0200
Subject: 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

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");
-- 
cgit v0.10.2-6-g49f6