Browse Source

Removed the callback argument from love.filesystem.getDirectoryItems (resolves issue #877.)

Cleaned up the getDirectoryItems ccde to no longer call Lua API functions from Filesystem.cpp (now it's all in wrap_Filesystem.cpp.)
Alex Szpakowski 10 years ago
parent
commit
e7f56241ec

+ 1 - 2
src/modules/filesystem/Filesystem.h

@@ -23,7 +23,6 @@
 
 
 // LOVE
 // LOVE
 #include "common/config.h"
 #include "common/config.h"
-#include "common/runtime.h"
 #include "common/Module.h"
 #include "common/Module.h"
 #include "common/int.h"
 #include "common/int.h"
 #include "FileData.h"
 #include "FileData.h"
@@ -208,7 +207,7 @@ public:
 	 * This "native" method returns a table of all
 	 * This "native" method returns a table of all
 	 * files in a given directory.
 	 * files in a given directory.
 	 **/
 	 **/
-	virtual int getDirectoryItems(lua_State *L) = 0;
+	virtual void getDirectoryItems(const char *dir, std::vector<std::string> &items) = 0;
 
 
 	/**
 	/**
 	 * Gets the last modification time of a file, in seconds
 	 * Gets the last modification time of a file, in seconds

+ 2 - 24
src/modules/filesystem/physfs/Filesystem.cpp

@@ -545,36 +545,14 @@ void Filesystem::append(const char *filename, const void *data, int64 size) cons
 		throw love::Exception("Data could not be written.");
 		throw love::Exception("Data could not be written.");
 }
 }
 
 
-int Filesystem::getDirectoryItems(lua_State *L)
+void Filesystem::getDirectoryItems(const char *dir, std::vector<std::string> &items)
 {
 {
-	const char *dir = luaL_checkstring(L, 1);
-	bool hascallback = !lua_isnoneornil(L, 2);
-
-	if (hascallback)
-		luaL_checktype(L, 2, LUA_TFUNCTION);
-
 	char **rc = PHYSFS_enumerateFiles(dir);
 	char **rc = PHYSFS_enumerateFiles(dir);
-	int index = 1;
-
-	lua_newtable(L);
 
 
 	for (char **i = rc; *i != 0; i++)
 	for (char **i = rc; *i != 0; i++)
-	{
-		if (hascallback)
-		{
-			lua_pushvalue(L, 2);
-			lua_pushstring(L, *i);
-			lua_call(L, 1, 0);
-		}
-
-		lua_pushstring(L, *i);
-		lua_rawseti(L, -2, index);
-		index++;
-	}
+		items.push_back(*i);
 
 
 	PHYSFS_freeList(rc);
 	PHYSFS_freeList(rc);
-
-	return 1;
 }
 }
 
 
 int64 Filesystem::getLastModified(const char *filename) const
 int64 Filesystem::getLastModified(const char *filename) const

+ 1 - 1
src/modules/filesystem/physfs/Filesystem.h

@@ -86,7 +86,7 @@ public:
 	void write(const char *filename, const void *data, int64 size) const;
 	void write(const char *filename, const void *data, int64 size) const;
 	void append(const char *filename, const void *data, int64 size) const;
 	void append(const char *filename, const void *data, int64 size) const;
 
 
-	int getDirectoryItems(lua_State *L);
+	void getDirectoryItems(const char *dir, std::vector<std::string> &items);
 
 
 	int64 getLastModified(const char *filename) const;
 	int64 getLastModified(const char *filename) const;
 	int64 getSize(const char *filename) const;
 	int64 getSize(const char *filename) const;

+ 15 - 1
src/modules/filesystem/wrap_Filesystem.cpp

@@ -408,7 +408,21 @@ int w_append(lua_State *L)
 
 
 int w_getDirectoryItems(lua_State *L)
 int w_getDirectoryItems(lua_State *L)
 {
 {
-	return instance()->getDirectoryItems(L);
+	const char *dir = luaL_checkstring(L, 1);
+	std::vector<std::string> items;
+
+	instance()->getDirectoryItems(dir, items);
+
+	lua_createtable(L, (int) items.size(), 0);
+
+	for (int i = 0; i < (int) items.size(); i++)
+	{
+		lua_pushstring(L, items[i].c_str());
+		lua_rawseti(L, -2, i + 1);
+	}
+
+	// Return the table.
+	return 1;
 }
 }
 
 
 int w_lines(lua_State *L)
 int w_lines(lua_State *L)