Commits

Alex Szpakowski committed cb7e512

Added love.filesystem.getSize (issue #541)

Comments (0)

Files changed (4)

src/modules/filesystem/physfs/Filesystem.cpp

 	return 1;
 }
 
+int64 Filesystem::getSize(const char *filename)
+{
+	File f(filename);
+	int64 size = f.getSize();
+	return size;
+}
+
 } // physfs
 } // filesystem
 } // love

src/modules/filesystem/physfs/Filesystem.h

 	int getLastModified(lua_State *L);
 
 	/**
+	 * Gets the size of a file in bytes.
+	 * @param filename The name of the file.
+	 **/
+	int64 getSize(const char *filename);
+
+	/**
 	 * Text file line-reading iterator function used and
 	 * pushed on the Lua stack by love.filesystem.lines
 	 * and File:lines.

src/modules/filesystem/physfs/wrap_Filesystem.cpp

 	return instance->getLastModified(L);
 }
 
+int w_getSize(lua_State *L)
+{
+	const char *filename = luaL_checkstring(L, 1);
+
+	int64 size = -1;
+	try
+	{
+		size = instance->getSize(filename);
+	}
+	catch (love::Exception &e)
+	{
+		// Return nil, errorstring
+		lua_pushnil(L);
+		lua_pushstring(L, e.what());
+		return 2;
+	}
+
+	// Return nil on failure or if size does not fit into a double precision floating-point number.
+	if (size == -1 || size >= 0x20000000000000LL)
+	{
+		lua_pushnil(L);
+		if (size == -1)
+			lua_pushstring(L, "Could not determine file size.");
+		else
+			lua_pushstring(L, "Size too large to fit into a Lua number!");
+		return 2;
+	}
+	else
+	{
+		lua_pushnumber(L, (lua_Number) size);
+		return 1;
+	}
+}
+
 int loader(lua_State *L)
 {
 	const char *filename = lua_tostring(L, -1);
 	{ "lines",  w_lines },
 	{ "load",  w_load },
 	{ "getLastModified", w_getLastModified },
+	{ "getSize", w_getSize },
 	{ "newFileData", w_newFileData },
 	{ 0, 0 }
 };

src/modules/filesystem/physfs/wrap_Filesystem.h

 int w_lines(lua_State *L);
 int w_load(lua_State *L);
 int w_getLastModified(lua_State *L);
+int w_getSize(lua_State *L);
 int loader(lua_State *L);
 int extloader(lua_State *L);
 extern "C" LOVE_EXPORT int luaopen_love_filesystem(lua_State *L);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.