浏览代码

love.filesystem.newFile now accepts an optional FileMode parameter ("r", "w", "a") to better mirror io.open

Alex Szpakowski 12 年之前
父节点
当前提交
4952ebd5d9
共有 2 个文件被更改,包括 38 次插入17 次删除
  1. 15 12
      src/modules/filesystem/physfs/wrap_File.cpp
  2. 23 5
      src/modules/filesystem/physfs/wrap_Filesystem.cpp

+ 15 - 12
src/modules/filesystem/physfs/wrap_File.cpp

@@ -53,18 +53,19 @@ int w_File_getSize(lua_State *L)
 int w_File_open(lua_State *L)
 {
 	File *file = luax_checkfile(L, 1);
+	const char *str = luaL_checkstring(L, 2);
 	File::Mode mode;
 
-	if (!File::getConstant(luaL_checkstring(L, 2), mode))
-		return luaL_error(L, "Incorrect file open mode: %s", luaL_checkstring(L, 2));
+	if (!File::getConstant(str, mode))
+		return luaL_error(L, "Incorrect file open mode: %s", str);
 
 	try
 	{
 		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;
@@ -88,9 +89,9 @@ int w_File_read(lua_State *L)
 	{
 		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());
@@ -105,15 +106,16 @@ int w_File_write(lua_State *L)
 	bool result;
 	if (file->getMode() == File::CLOSED)
 		return luaL_error(L, "File is not open.");
+
 	if (lua_isstring(L, 2))
 	{
 		try
 		{
 			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);
 			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
 	{
-		return luaL_error(L, "String or data expected.");
+		return luaL_argerror(L, 2, "string or data expected");
 	}
+
 	lua_pushboolean(L, result);
 	return 1;
 }
@@ -193,7 +196,7 @@ int w_File_lines(lua_State *L)
 			if (!file->open(File::READ))
 				return luaL_error(L, "Could not open file.");
 		}
-		catch(love::Exception &e)
+		catch (love::Exception &e)
 		{
 			return luaL_error(L, "%s", e.what());
 		}

+ 23 - 5
src/modules/filesystem/physfs/wrap_Filesystem.cpp

@@ -90,15 +90,33 @@ int w_setSource(lua_State *L)
 int w_newFile(lua_State *L)
 {
 	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);
 	return 1;
 }