mirror of
https://github.com/love2d/love.git
synced 2026-08-20 12:40:20 +02:00
love.filesystem.newFile now accepts an optional FileMode parameter ("r", "w", "a") to better mirror io.open
This commit is contained in:
@@ -53,18 +53,19 @@ int w_File_getSize(lua_State *L)
|
|||||||
int w_File_open(lua_State *L)
|
int w_File_open(lua_State *L)
|
||||||
{
|
{
|
||||||
File *file = luax_checkfile(L, 1);
|
File *file = luax_checkfile(L, 1);
|
||||||
|
const char *str = luaL_checkstring(L, 2);
|
||||||
File::Mode mode;
|
File::Mode mode;
|
||||||
|
|
||||||
if (!File::getConstant(luaL_checkstring(L, 2), mode))
|
if (!File::getConstant(str, mode))
|
||||||
return luaL_error(L, "Incorrect file open mode: %s", luaL_checkstring(L, 2));
|
return luaL_error(L, "Incorrect file open mode: %s", str);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
lua_pushboolean(L, file->open(mode) ? 1 : 0);
|
lua_pushboolean(L, file->open(mode) ? 1 : 0);
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch (love::Exception &e)
|
||||||
{
|
{
|
||||||
return luaL_error(L, e.what());
|
return luaL_error(L, "%s", e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@@ -88,9 +89,9 @@ int w_File_read(lua_State *L)
|
|||||||
{
|
{
|
||||||
d = file->read(size);
|
d = file->read(size);
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch (love::Exception &e)
|
||||||
{
|
{
|
||||||
return luaL_error(L, e.what());
|
return luaL_error(L, "%s", e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_pushlstring(L, (const char *) d->getData(), d->getSize());
|
lua_pushlstring(L, (const char *) d->getData(), d->getSize());
|
||||||
@@ -105,15 +106,16 @@ int w_File_write(lua_State *L)
|
|||||||
bool result;
|
bool result;
|
||||||
if (file->getMode() == File::CLOSED)
|
if (file->getMode() == File::CLOSED)
|
||||||
return luaL_error(L, "File is not open.");
|
return luaL_error(L, "File is not open.");
|
||||||
|
|
||||||
if (lua_isstring(L, 2))
|
if (lua_isstring(L, 2))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
result = file->write(lua_tostring(L, 2), luaL_optint(L, 3, lua_objlen(L, 2)));
|
result = file->write(lua_tostring(L, 2), luaL_optint(L, 3, lua_objlen(L, 2)));
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch (love::Exception &e)
|
||||||
{
|
{
|
||||||
return luaL_error(L, e.what());
|
return luaL_error(L, "%s", e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -124,15 +126,16 @@ int w_File_write(lua_State *L)
|
|||||||
love::Data *data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
|
love::Data *data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
|
||||||
result = file->write(data, luaL_optint(L, 3, data->getSize()));
|
result = file->write(data, luaL_optint(L, 3, data->getSize()));
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch (love::Exception &e)
|
||||||
{
|
{
|
||||||
return luaL_error(L, e.what());
|
return luaL_error(L, "%s", e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return luaL_error(L, "String or data expected.");
|
return luaL_argerror(L, 2, "string or data expected");
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_pushboolean(L, result);
|
lua_pushboolean(L, result);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -193,7 +196,7 @@ int w_File_lines(lua_State *L)
|
|||||||
if (!file->open(File::READ))
|
if (!file->open(File::READ))
|
||||||
return luaL_error(L, "Could not open file.");
|
return luaL_error(L, "Could not open file.");
|
||||||
}
|
}
|
||||||
catch(love::Exception &e)
|
catch (love::Exception &e)
|
||||||
{
|
{
|
||||||
return luaL_error(L, "%s", e.what());
|
return luaL_error(L, "%s", e.what());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,15 +90,33 @@ int w_setSource(lua_State *L)
|
|||||||
int w_newFile(lua_State *L)
|
int w_newFile(lua_State *L)
|
||||||
{
|
{
|
||||||
const char *filename = luaL_checkstring(L, 1);
|
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);
|
luax_newtype(L, "File", FILESYSTEM_FILE_T, (void *)t);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user