Add love.filesystem.openFile. Deprecate love.filesystem.newFile.

This commit is contained in:
Alex Szpakowski
2022-04-22 21:05:20 -03:00
parent e167a77994
commit b8ff64e9e3
6 changed files with 69 additions and 44 deletions
+2 -2
View File
@@ -165,9 +165,9 @@ public:
virtual bool unmountFullPath(const char *fullpath) = 0;
/**
* Creates a new file.
* Opens a new File object from the specified path, using the given mode.
**/
virtual File *newFile(const char *filename, File::Mode = File::MODE_CLOSED) const = 0;
virtual File *openFile(const char *filename, File::Mode mode) const = 0;
/**
* Creates a new FileData object. Data will be copied.
+10 -8
View File
@@ -46,13 +46,8 @@ NativeFile::NativeFile(const std::string &filename, Mode mode)
, bufferMode(BUFFER_NONE)
, bufferSize(0)
{
open(mode);
}
NativeFile::~NativeFile()
{
if (mode != MODE_CLOSED)
close();
if (!open(mode))
throw love::Exception("Could not open file at path %s", filename.c_str());
}
NativeFile::NativeFile(const NativeFile &other)
@@ -62,7 +57,14 @@ NativeFile::NativeFile(const NativeFile &other)
, bufferMode(other.bufferMode)
, bufferSize(other.bufferSize)
{
open(other.mode);
if (!open(other.mode))
throw love::Exception("Could not open file at path %s", filename.c_str());
}
NativeFile::~NativeFile()
{
if (mode != MODE_CLOSED)
close();
}
NativeFile *NativeFile::clone()
+4 -2
View File
@@ -48,7 +48,8 @@ File::File(const std::string &filename, Mode mode)
, bufferMode(BUFFER_NONE)
, bufferSize(0)
{
open(mode);
if (!open(mode))
throw love::Exception("Could not open file at path %s", filename.c_str());
}
File::File(const File &other)
@@ -58,7 +59,8 @@ File::File(const File &other)
, bufferMode(other.bufferMode)
, bufferSize(other.bufferSize)
{
open(other.mode);
if (!open(other.mode))
throw love::Exception("Could not open file at path %s", filename.c_str());
}
File::~File()
+1 -1
View File
@@ -496,7 +496,7 @@ bool Filesystem::unmount(Data *data)
return false;
}
love::filesystem::File *Filesystem::newFile(const char *filename, File::Mode mode) const
love::filesystem::File *Filesystem::openFile(const char *filename, File::Mode mode) const
{
return new File(filename, mode);
}
+1 -1
View File
@@ -71,7 +71,7 @@ public:
bool unmount(CommonPath path) override;
bool unmountFullPath(const char *fullpath) override;
love::filesystem::File *newFile(const char *filename, File::Mode mode = File::MODE_CLOSED) const override;
love::filesystem::File *openFile(const char *filename, File::Mode mode) const override;
std::string getFullCommonPath(CommonPath path) override;
const char *getWorkingDirectory() override;
+51 -30
View File
@@ -227,34 +227,53 @@ int w_unmountCommonPath(lua_State *L)
return 1;
}
int w_newFile(lua_State *L)
int w_openFile(lua_State *L)
{
const char *filename = luaL_checkstring(L, 1);
const char *modestr = luaL_checkstring(L, 2);
const char *str = 0;
File::Mode mode = File::MODE_CLOSED;
if (!File::getConstant(modestr, mode))
return luax_enumerror(L, "file open mode", File::getConstants(mode), modestr);
if (lua_isstring(L, 2))
File *t = nullptr;
try
{
str = luaL_checkstring(L, 2);
if (!File::getConstant(str, mode))
return luax_enumerror(L, "file open mode", File::getConstants(mode), str);
t = instance()->openFile(filename, mode);
}
catch (love::Exception &e)
{
return luax_ioError(L, "%s", e.what());
}
File *t = instance()->newFile(filename);
luax_pushtype(L, t);
t->release();
return 1;
}
if (mode != File::MODE_CLOSED)
int w_newFile(lua_State* L)
{
luax_markdeprecated(L, 1, "love.filesystem.newFile", API_FUNCTION, DEPRECATED_RENAMED, "love.filesystem.openFile");
const char* filename = luaL_checkstring(L, 1);
File::Mode mode = File::MODE_CLOSED;
if (!lua_isnoneornil(L, 2))
{
try
{
if (!t->open(mode))
throw love::Exception("Could not open file.");
}
catch (love::Exception &e)
{
t->release();
return luax_ioError(L, "%s", e.what());
}
const char* modestr = luaL_checkstring(L, 2);
if (!File::getConstant(modestr, mode))
return luax_enumerror(L, "file open mode", File::getConstants(mode), modestr);
}
File* t = nullptr;
try
{
t = instance()->openFile(filename, mode);
}
catch (love::Exception& e)
{
return luax_ioError(L, "%s", e.what());
}
luax_pushtype(L, t);
@@ -268,7 +287,14 @@ File *luax_getfile(lua_State *L, int idx)
if (lua_isstring(L, idx))
{
const char *filename = luaL_checkstring(L, idx);
file = instance()->newFile(filename);
try
{
file = instance()->openFile(filename, File::MODE_CLOSED);
}
catch (love::Exception &e)
{
luax_ioError(L, "%s", e.what());
}
}
else
{
@@ -663,16 +689,8 @@ int w_lines(lua_State *L)
{
if (lua_isstring(L, 1))
{
File *file = instance()->newFile(lua_tostring(L, 1));
bool success = false;
luax_catchexcept(L, [&](){ success = file->open(File::MODE_READ); });
if (!success)
{
file->release();
return luaL_error(L, "Could not open file.");
}
File *file = nullptr;
luax_catchexcept(L, [&]() { file = instance()->openFile(lua_tostring(L, 1), File::MODE_READ); });
luax_pushtype(L, file);
file->release();
@@ -965,7 +983,7 @@ static const luaL_Reg functions[] =
{ "unmount", w_unmount },
{ "unmountFullPath", w_unmountFullPath },
{ "unmountCommonPath", w_unmountCommonPath },
{ "newFile", w_newFile },
{ "openFile", w_openFile },
{ "getFullCommonPath", w_getFullCommonPath },
{ "getWorkingDirectory", w_getWorkingDirectory },
{ "getUserDirectory", w_getUserDirectory },
@@ -991,6 +1009,9 @@ static const luaL_Reg functions[] =
{ "getCRequirePath", w_getCRequirePath },
{ "setCRequirePath", w_setCRequirePath },
// Deprecated
{ "newFile", w_newFile },
{ 0, 0 }
};