love.filesystem.newFile now accepts an optional FileMode parameter ("r", "w", "a") to better mirror io.open

This commit is contained in:
Alex Szpakowski
2013-05-08 17:47:37 -03:00
parent c7ebb50b70
commit 4952ebd5d9
2 changed files with 38 additions and 17 deletions
@@ -90,15 +90,33 @@ int w_setSource(lua_State *L)
int w_newFile(lua_State *L)
{
const char *filename = luaL_checkstring(L, 1);
File *t;
try
const char *str = 0;
File::Mode mode = File::CLOSED;
if (lua_isstring(L, 2))
{
t = instance->newFile(filename);
str = luaL_checkstring(L, 2);
if (!File::getConstant(str, mode))
return luaL_error(L, "Incorrect file open mode: %s", str);
}
catch(Exception e)
File *t = instance->newFile(filename);
if (mode != File::CLOSED)
{
return luaL_error(L, e.what());
try
{
if (!t->open(mode))
throw love::Exception("Could not open file.");
}
catch (love::Exception &e)
{
t->release();
return luaL_error(L, "%s", e.what());
}
}
luax_newtype(L, "File", FILESYSTEM_FILE_T, (void *)t);
return 1;
}