Browse Source

Added an optional callback function argument to love.filesystem.getDirectoryItems. Callback function takes a single argument containing a filepath string.

For example: love.filesystem.getDirectoryItems("", print)
Alex Szpakowski 11 years ago
parent
commit
aba17607aa
1 changed files with 11 additions and 0 deletions
  1. 11 0
      src/modules/filesystem/physfs/Filesystem.cpp

+ 11 - 0
src/modules/filesystem/physfs/Filesystem.cpp

@@ -477,6 +477,10 @@ void Filesystem::append(const char *filename, const void *data, int64 size) cons
 int Filesystem::getDirectoryItems(lua_State *L)
 int Filesystem::getDirectoryItems(lua_State *L)
 {
 {
 	const char *dir = luaL_checkstring(L, 1);
 	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;
 	int index = 1;
@@ -485,6 +489,13 @@ int Filesystem::getDirectoryItems(lua_State *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_pushstring(L, *i);
 		lua_rawseti(L, -2, index);
 		lua_rawseti(L, -2, index);
 		index++;
 		index++;