Browse Source

Add a variant of love.filesystem.getInfo which filters based on file type. getInfo(filename, "directory") will only return info for non-symlinked directories, for example.

Alex Szpakowski 7 years ago
parent
commit
0cefb12c13

+ 1 - 1
changes.txt

@@ -93,7 +93,7 @@ Released: N/A
   * Renamed all get[Object]List functions to get[Object]s.
 
   * Removed the default source type for love.audio.newSource.
-  * Removed variant of love.filesystem.newFileData which takes base64 data, use love.math.decode instead.
+  * Removed variant of love.filesystem.newFileData which takes base64 data, use love.data.decode instead.
   * Removed the no-argument variant of Text:set, use Text:clear instead.
   * Removed love.graphics.getCompressedImageFormats, use love.graphics.getImageFormats instead.
   * Removed the 'void effects(...)' pixel shader entry point. Use the new 'void effect()' instead.

+ 5 - 0
src/modules/filesystem/Filesystem.cpp

@@ -130,6 +130,11 @@ bool Filesystem::getConstant(FileType in, const char *&out)
 	return fileTypes.find(in, out);
 }
 
+std::vector<std::string> Filesystem::getConstants(FileType)
+{
+	return fileTypes.getNames();
+}
+
 StringMap<Filesystem::FileType, Filesystem::FILETYPE_MAX_ENUM>::Entry Filesystem::fileTypeEntries[] =
 {
 	{ "file",      FILETYPE_FILE      },

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

@@ -265,6 +265,7 @@ public:
 
 	static bool getConstant(const char *in, FileType &out);
 	static bool getConstant(FileType in, const char *&out);
+	static std::vector<std::string> getConstants(FileType);
 
 private:
 

+ 19 - 2
src/modules/filesystem/wrap_Filesystem.cpp

@@ -395,14 +395,31 @@ int w_getInfo(lua_State *L)
 	const char *filepath = luaL_checkstring(L, 1);
 	Filesystem::Info info = {};
 
+	int startidx = 2;
+	Filesystem::FileType filtertype = Filesystem::FILETYPE_MAX_ENUM;
+	if (lua_isstring(L, startidx))
+	{
+		const char *typestr = luaL_checkstring(L, startidx);
+		if (!Filesystem::getConstant(typestr, filtertype))
+			return luax_enumerror(L, "file type", Filesystem::getConstants(filtertype), typestr);
+
+		startidx++;
+	}
+
 	if (instance()->getInfo(filepath, info))
 	{
+		if (filtertype != Filesystem::FILETYPE_MAX_ENUM && info.type != filtertype)
+		{
+			lua_pushnil(L);
+			return 1;
+		}
+
 		const char *typestr = nullptr;
 		if (!Filesystem::getConstant(info.type, typestr))
 			return luaL_error(L, "Unknown file type.");
 
-		if (lua_istable(L, 2))
-			lua_pushvalue(L, 2);
+		if (lua_istable(L, startidx))
+			lua_pushvalue(L, startidx);
 		else
 			lua_createtable(L, 0, 3);