Added love.filesystem.append (issue #541)

This commit is contained in:
Alex Szpakowski
2013-03-17 03:40:13 -03:00
parent 96229bcb5d
commit 129ae0f4eb
4 changed files with 38 additions and 4 deletions
+12 -1
View File
@@ -319,7 +319,7 @@ Data *Filesystem::read(const char *filename, int64 size) const
return file.read(size);
}
void Filesystem::write(const char *filename, const void *data, int64 size)
void Filesystem::write(const char *filename, const void *data, int64 size) const
{
File file(filename);
@@ -330,6 +330,17 @@ void Filesystem::write(const char *filename, const void *data, int64 size)
throw love::Exception("Data could not be written.");
}
void Filesystem::append(const char *filename, const void *data, int64 size) const
{
File file(filename);
file.open(File::APPEND);
// close() is called in the File destructor.
if (!file.write(data, size))
throw love::Exception("Data could not be written.");
}
int Filesystem::enumerate(lua_State *L)
{
int n = lua_gettop(L);
+9 -1
View File
@@ -244,7 +244,15 @@ public:
* @param data The data to write.
* @param size The size in bytes of the data to write.
**/
void write(const char *filename, const void *data, int64 size);
void write(const char *filename, const void *data, int64 size) const;
/**
* Append data to a file, creating it if it doesn't exist.
* @param filename The name of the file to write to.
* @param data The data to append.
* @param size The size in bytes of the data to append.
**/
void append(const char *filename, const void *data, int64 size) const;
/**
* Check if end-of-file is reached.
@@ -227,7 +227,7 @@ int w_read(lua_State *L)
return 2;
}
int w_write(lua_State *L)
static int w_write_or_append(lua_State *L, File::Mode mode)
{
const char *filename = luaL_checkstring(L, 1);
@@ -250,7 +250,10 @@ int w_write(lua_State *L)
try
{
instance->write(filename, (const void *) input, len);
if (mode == File::APPEND)
instance->append(filename, (const void *) input, len);
else
instance->write(filename, (const void *) input, len);
}
catch (love::Exception &e)
{
@@ -262,6 +265,16 @@ int w_write(lua_State *L)
return 1;
}
int w_write(lua_State *L)
{
return w_write_or_append(L, File::WRITE);
}
int w_append(lua_State *L)
{
return w_write_or_append(L, File::APPEND);
}
int w_enumerate(lua_State *L)
{
return instance->enumerate(L);
@@ -457,6 +470,7 @@ static const luaL_Reg functions[] =
{ "remove", w_remove },
{ "read", w_read },
{ "write", w_write },
{ "append", w_append },
{ "enumerate", w_enumerate },
{ "lines", w_lines },
{ "load", w_load },
@@ -57,6 +57,7 @@ int w_open(lua_State *L);
int w_close(lua_State *L);
int w_read(lua_State *L);
int w_write(lua_State *L);
int w_append(lua_State *L);
int w_eof(lua_State *L);
int w_tell(lua_State *L);
int w_seek(lua_State *L);