mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
Added love.filesystem.getSize (issue #541)
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user