Added love.filesystem.getSize (issue #541)

This commit is contained in:
Alex Szpakowski
2013-03-17 01:56:24 -03:00
parent e8c6a3f77c
commit 8d4f35b86b
4 changed files with 49 additions and 0 deletions
@@ -605,6 +605,13 @@ int Filesystem::getLastModified(lua_State *L)
return 1;
}
int64 Filesystem::getSize(const char *filename)
{
File f(filename);
int64 size = f.getSize();
return size;
}
} // physfs
} // filesystem
} // love
@@ -281,6 +281,12 @@ public:
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
@@ -268,6 +268,40 @@ int w_getLastModified(lua_State *L)
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);
@@ -395,6 +429,7 @@ static const luaL_Reg functions[] =
{ "lines", w_lines },
{ "load", w_load },
{ "getLastModified", w_getLastModified },
{ "getSize", w_getSize },
{ "newFileData", w_newFileData },
{ 0, 0 }
};
@@ -64,6 +64,7 @@ int w_enumerate(lua_State *L);
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);