Cleaned up love.filesystem.load and love.filesystem.getLastModified code

This commit is contained in:
Alex Szpakowski
2013-03-17 13:43:07 -03:00
parent a2cb35f4e3
commit 2838208338
3 changed files with 50 additions and 61 deletions
+5 -47
View File
@@ -471,56 +471,14 @@ int Filesystem::lines_i(lua_State *L)
return 0;
}
int Filesystem::load(lua_State *L)
int64 Filesystem::getLastModified(const char *filename) const
{
// Need only one arg.
luax_assert_argc(L, 1, 1);
// Must be string.
if (!lua_isstring(L, -1))
return luaL_error(L, "The argument must be a string.");
std::string filename = lua_tostring(L, -1);
// The file must exist.
if (!exists(filename.c_str()))
return luaL_error(L, "File %s does not exist.", filename.c_str());
// Create the file.
File *file = newFile(filename.c_str());
// Get the data from the file.
Data *data = file->read();
int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
data->release();
file->release();
// Load the chunk, but don't run it.
switch (status)
{
case LUA_ERRMEM:
return luaL_error(L, "Memory allocation error: %s\n", lua_tostring(L, -1));
case LUA_ERRSYNTAX:
return luaL_error(L, "Syntax error: %s\n", lua_tostring(L, -1));
default: // success
return 1;
}
}
int Filesystem::getLastModified(lua_State *L)
{
const char *filename = luaL_checkstring(L, 1);
PHYSFS_sint64 time = PHYSFS_getLastModTime(filename);
if (time == -1)
{
lua_pushnil(L);
lua_pushstring(L, "Could not determine file modification date.");
return 2;
}
lua_pushnumber(L, static_cast<lua_Number>(time));
return 1;
throw love::Exception("Could not determine file modification date.");
return time;
}
int64 Filesystem::getSize(const char *filename) const
+5 -8
View File
@@ -236,7 +236,7 @@ public:
* @param filename The name of the file to read from.
* @param size The size in bytes of the data to read.
**/
Data *read(const char *filename, int64 size) const;
Data *read(const char *filename, int64 size = File::ALL) const;
/**
* Write data to a file.
@@ -279,14 +279,11 @@ public:
int enumerate(lua_State *L);
/**
* Loads a file without running it. The loaded
* chunk is returned as a function.
* @param filename The filename of the file to load.
* @return A function.
* Gets the last modification time of a file, in seconds
* since the Unix epoch.
* @param filename The name of the file.
**/
int load(lua_State *L);
int getLastModified(lua_State *L);
int64 getLastModified(const char *filename) const;
/**
* Gets the size of a file in bytes.
@@ -307,19 +307,53 @@ int w_lines(lua_State *L)
int w_load(lua_State *L)
{
std::string filename = std::string(luaL_checkstring(L, 1));
Data *data = 0;
try
{
return instance->load(L);
data = instance->read(filename.c_str());
}
catch(love::Exception &e)
catch (love::Exception &e)
{
return luaL_error(L, e.what());
return luaL_error(L, "%s", e.what());
}
int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
data->release();
// Load the chunk, but don't run it.
switch (status)
{
case LUA_ERRMEM:
return luaL_error(L, "Memory allocation error: %s\n", lua_tostring(L, -1));
case LUA_ERRSYNTAX:
return luaL_error(L, "Syntax error: %s\n", lua_tostring(L, -1));
default: // success
return 1;
}
}
int w_getLastModified(lua_State *L)
{
return instance->getLastModified(L);
const char *filename = luaL_checkstring(L, 1);
int64 time = 0;
try
{
time = instance->getLastModified(filename);
}
catch (love::Exception &e)
{
lua_pushnil(L);
lua_pushstring(L, e.what());
return 2;
}
lua_pushnumber(L, static_cast<lua_Number>(time));
return 1;
}
int w_getSize(lua_State *L)
@@ -370,7 +404,7 @@ int loader(lua_State *L)
lua_pop(L, 1);
lua_pushstring(L, tmp.c_str());
// Ok, load it.
return instance->load(L);
return w_load(L);
}
tmp = filename;
@@ -391,7 +425,7 @@ int loader(lua_State *L)
lua_pop(L, 1);
lua_pushstring(L, tmp.c_str());
// Ok, load it.
return instance->load(L);
return w_load(L);
}
}