Browse Source

Added love.filesystem.append (issue #541)

Alex Szpakowski 12 years ago
parent
commit
129ae0f4eb

+ 12 - 1
src/modules/filesystem/physfs/Filesystem.cpp

@@ -319,7 +319,7 @@ Data *Filesystem::read(const char *filename, int64 size) const
 	return file.read(size);
 	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);
 	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.");
 		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 Filesystem::enumerate(lua_State *L)
 {
 {
 	int n = lua_gettop(L);
 	int n = lua_gettop(L);

+ 9 - 1
src/modules/filesystem/physfs/Filesystem.h

@@ -244,7 +244,15 @@ public:
 	 * @param data The data to write.
 	 * @param data The data to write.
 	 * @param size The size in bytes of 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.
 	 * Check if end-of-file is reached.

+ 16 - 2
src/modules/filesystem/physfs/wrap_Filesystem.cpp

@@ -227,7 +227,7 @@ int w_read(lua_State *L)
 	return 2;
 	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);
 	const char *filename = luaL_checkstring(L, 1);
 
 
@@ -250,7 +250,10 @@ int w_write(lua_State *L)
 
 
 	try
 	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)
 	catch (love::Exception &e)
 	{
 	{
@@ -262,6 +265,16 @@ int w_write(lua_State *L)
 	return 1;
 	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)
 int w_enumerate(lua_State *L)
 {
 {
 	return instance->enumerate(L);
 	return instance->enumerate(L);
@@ -457,6 +470,7 @@ static const luaL_Reg functions[] =
 	{ "remove",  w_remove },
 	{ "remove",  w_remove },
 	{ "read",  w_read },
 	{ "read",  w_read },
 	{ "write",  w_write },
 	{ "write",  w_write },
+	{ "append", w_append },
 	{ "enumerate",  w_enumerate },
 	{ "enumerate",  w_enumerate },
 	{ "lines",  w_lines },
 	{ "lines",  w_lines },
 	{ "load",  w_load },
 	{ "load",  w_load },

+ 1 - 0
src/modules/filesystem/physfs/wrap_Filesystem.h

@@ -57,6 +57,7 @@ int w_open(lua_State *L);
 int w_close(lua_State *L);
 int w_close(lua_State *L);
 int w_read(lua_State *L);
 int w_read(lua_State *L);
 int w_write(lua_State *L);
 int w_write(lua_State *L);
+int w_append(lua_State *L);
 int w_eof(lua_State *L);
 int w_eof(lua_State *L);
 int w_tell(lua_State *L);
 int w_tell(lua_State *L);
 int w_seek(lua_State *L);
 int w_seek(lua_State *L);