Browse Source

Fixed bug which caused files opened in write mode to fail.

rude 16 years ago
parent
commit
28b1ee4f16

+ 9 - 1
src/modules/filesystem/physfs/File.cpp

@@ -46,10 +46,15 @@ namespace physfs
 	
 	
 	bool File::open(Mode mode)
 	bool File::open(Mode mode)
 	{
 	{
+		// File must exist if read mode.
+		if((mode == READ))
+			if(!PHYSFS_exists(filename.c_str()))
+				throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
+
 		// Check whether the write directory is set.
 		// Check whether the write directory is set.
 		if((mode == APPEND || mode == WRITE) && (PHYSFS_getWriteDir() == 0))
 		if((mode == APPEND || mode == WRITE) && (PHYSFS_getWriteDir() == 0))
 			if(!hack_setupWriteDirectory())
 			if(!hack_setupWriteDirectory())
-				return false;
+				throw love::Exception("Could not set write directory.");
 
 
 		// File already open?
 		// File already open?
 		if(file != 0)
 		if(file != 0)
@@ -143,6 +148,9 @@ namespace physfs
 
 
 	bool File::write(const void * data, int size)
 	bool File::write(const void * data, int size)
 	{
 	{
+		if(file == 0)
+			throw love::Exception("Could not write to file. File not open.");
+
 		// Try to write.
 		// Try to write.
 		int written = static_cast<int>(PHYSFS_write(file, data, 1, size));
 		int written = static_cast<int>(PHYSFS_write(file, data, 1, size));
 
 

+ 0 - 2
src/modules/filesystem/physfs/Filesystem.cpp

@@ -142,8 +142,6 @@ namespace physfs
 	
 	
 	File * Filesystem::newFile(const char *filename)
 	File * Filesystem::newFile(const char *filename)
 	{
 	{
-		if (!PHYSFS_exists(filename))
-			throw Exception("File %s doesn't exist", filename);
 		return new File(filename);
 		return new File(filename);
 	}
 	}
 
 

+ 49 - 16
src/modules/filesystem/physfs/wrap_File.cpp

@@ -20,6 +20,8 @@
 
 
 #include "wrap_File.h"
 #include "wrap_File.h"
 
 
+#include <common/Exception.h>
+
 namespace love
 namespace love
 {
 {
 namespace filesystem
 namespace filesystem
@@ -42,7 +44,16 @@ namespace physfs
 	{
 	{
 		File * file = luax_checkfile(L, 1);
 		File * file = luax_checkfile(L, 1);
 		int mode = luaL_optint(L, 2, File::READ);
 		int mode = luaL_optint(L, 2, File::READ);
-		lua_pushboolean(L, file->open((File::Mode)mode) ? 1 : 0);
+
+		try
+		{
+			lua_pushboolean(L, file->open((File::Mode)mode) ? 1 : 0);
+		}
+		catch(Exception e)
+		{
+			return luaL_error(L, e.what());
+		}
+
 		return 1;
 		return 1;
 	}
 	}
 
 
@@ -56,7 +67,17 @@ namespace physfs
 	int w_File_read(lua_State * L)
 	int w_File_read(lua_State * L)
 	{
 	{
 		File * file = luax_checkfile(L, 1);
 		File * file = luax_checkfile(L, 1);
-		Data * d = file->read(luaL_optint(L, 2, file->getSize()));
+		Data * d = 0;
+
+		try
+		{
+			d = file->read(luaL_optint(L, 2, file->getSize()));
+		}
+		catch(Exception e)
+		{
+			return luaL_error(L, e.what());
+		}
+
 		lua_pushlstring(L, (const char*) d->getData(), d->getSize());
 		lua_pushlstring(L, (const char*) d->getData(), d->getSize());
 		lua_pushnumber(L, d->getSize());
 		lua_pushnumber(L, d->getSize());
 		d->release();
 		d->release();
@@ -70,9 +91,21 @@ namespace physfs
 		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) )
-			result = file->write(lua_tostring(L, 2), luaL_optint(L, 3, lua_objlen(L, 2)));
+		{
+			try
+			{
+				result = file->write(lua_tostring(L, 2), luaL_optint(L, 3, lua_objlen(L, 2)));
+			}
+			catch(Exception e)
+			{
+				return luaL_error(L, e.what());
+			}
+			
+		}
 		else
 		else
+		{
 			return luaL_error(L, "String expected.");
 			return luaL_error(L, "String expected.");
+		}
 		lua_pushboolean(L, result);
 		lua_pushboolean(L, result);
 		return 1;
 		return 1;
 	}
 	}
@@ -201,21 +234,21 @@ namespace physfs
 		return 0;
 		return 0;
 	}
 	}
 
 
+	static const luaL_Reg functions[] = { 
+			{ "getSize", w_File_getSize },
+			{ "open", w_File_open },
+			{ "close", w_File_close },
+			{ "read", w_File_read },
+			{ "write", w_File_write },
+			{ "eof", w_File_eof },
+			{ "tell", w_File_tell },
+			{ "seek", w_File_seek },
+			{ "lines", w_File_lines },
+			{ 0, 0 }
+	};
+
 	int luaopen_file(lua_State * L)
 	int luaopen_file(lua_State * L)
 	{
 	{
-		static const luaL_Reg functions[] = { 
-				{ "getSize", w_File_getSize },
-				{ "open", w_File_open },
-				{ "close", w_File_close },
-				{ "read", w_File_read },
-				{ "write", w_File_write },
-				{ "eof", w_File_eof },
-				{ "tell", w_File_tell },
-				{ "seek", w_File_seek },
-				{ "lines", w_File_lines },
-				{ 0, 0 }
-		};
-
 		return luax_register_type(L, "File", functions);
 		return luax_register_type(L, "File", functions);
 	}
 	}
 	
 	

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

@@ -166,12 +166,26 @@ namespace physfs
 
 
 	int w_read(lua_State * L)
 	int w_read(lua_State * L)
 	{
 	{
-		return instance->read(L);
+		try
+		{
+			return instance->read(L);
+		}
+		catch(Exception e)
+		{
+			return luaL_error(L, e.what());
+		}
 	}
 	}
 
 
 	int w_write(lua_State * L)
 	int w_write(lua_State * L)
 	{
 	{
-		return instance->write(L);
+		try
+		{
+			return instance->write(L);
+		}
+		catch(Exception e)
+		{
+			return luaL_error(L, e.what());
+		}
 	}	
 	}	
 
 
 	int w_enumerate(lua_State * L)
 	int w_enumerate(lua_State * L)