From 4952ebd5d92a4d614d15e68b75b437e54243673d Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 8 May 2013 17:47:37 -0300 Subject: [PATCH] love.filesystem.newFile now accepts an optional FileMode parameter ("r", "w", "a") to better mirror io.open --- src/modules/filesystem/physfs/wrap_File.cpp | 27 ++++++++++-------- .../filesystem/physfs/wrap_Filesystem.cpp | 28 +++++++++++++++---- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/modules/filesystem/physfs/wrap_File.cpp b/src/modules/filesystem/physfs/wrap_File.cpp index 5327a8a61..8c4da15c2 100644 --- a/src/modules/filesystem/physfs/wrap_File.cpp +++ b/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(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()); } diff --git a/src/modules/filesystem/physfs/wrap_Filesystem.cpp b/src/modules/filesystem/physfs/wrap_Filesystem.cpp index 0c194eb7d..df4b62860 100644 --- a/src/modules/filesystem/physfs/wrap_Filesystem.cpp +++ b/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; }