diff --git a/src/modules/filesystem/physfs/Filesystem.cpp b/src/modules/filesystem/physfs/Filesystem.cpp index 108d1388e..412f5991b 100644 --- a/src/modules/filesystem/physfs/Filesystem.cpp +++ b/src/modules/filesystem/physfs/Filesystem.cpp @@ -445,7 +445,7 @@ namespace physfs lua_pop(L, 1); luax_newtype(L, "File", FILESYSTEM_FILE_T, file, false); - lua_pushboolean(L, 1); // 1 = autoclose. + lua_pushnumber(L, 1); // 1 = autoclose. } else return luaL_error(L, "Expected filename."); diff --git a/src/modules/filesystem/physfs/wrap_File.cpp b/src/modules/filesystem/physfs/wrap_File.cpp index 437dcc5f4..217acce5a 100644 --- a/src/modules/filesystem/physfs/wrap_File.cpp +++ b/src/modules/filesystem/physfs/wrap_File.cpp @@ -162,7 +162,7 @@ namespace physfs if(luax_istype(L, 1, FILESYSTEM_FILE_T)) { file = luax_checktype(L, 1, "File", FILESYSTEM_FILE_T); - lua_pushboolean(L, 0); // 0 = do not close. + lua_pushnumber(L, 0); // 0 = do not close. } else return luaL_error(L, "Expected file handle."); @@ -171,90 +171,10 @@ namespace physfs if(!file->seek(0)) return luaL_error(L, "File does not appear to be open.\n"); - lua_pushcclosure(L, lines_i, 2); + lua_pushcclosure(L, Filesystem::lines_i, 2); return 1; } - int lines_i(lua_State * L) - { - // We're using a 1k buffer. - const static int bufsize = 1024; - static char buf[bufsize]; - - File * file = luax_checktype(L, lua_upvalueindex(1), "File", FILESYSTEM_FILE_T); - int close = (int)lua_tointeger(L, lua_upvalueindex(2)); - - // Find the next newline. - // pos must be at the start of the line we're trying to find. - int pos = file->tell(); - int newline = -1; - int totalread = 0; - - while(!file->eof()) - { - int current = file->tell(); - int read = file->read(buf, bufsize); - totalread += read; - - if(read < 0) - return luaL_error(L, "Readline failed!"); - - for(int i = 0;i 0) - break; - } - - // Special case for the last "line". - if(newline <= 0 && file->eof() && totalread > 0) - newline = pos + totalread; - - // We've got a newline. - if(newline > 0) - { - // Ok, we've got a line. - int linesize = (newline-pos); - - // Allocate memory for the string. - char * str = new char[linesize]; - - // Read it. - file->seek(pos); - if(file->read(str, linesize) == -1) - return luaL_error(L, "Read error."); - - if(str[linesize-1]=='\r') - linesize -= 1; - - lua_pushlstring(L, str, linesize); - - // Free the memory. Lua has a copy now. - delete[] str; - - // Set the beginning of the next line. - if(!file->eof()) - file->seek(newline+1); - - return 1; - } - - if(close) - { - file->close(); - file->release(); - } - - // else: (newline <= 0) - return 0; - } - static const luaL_Reg functions[] = { { "getSize", w_File_getSize }, { "open", w_File_open }, diff --git a/src/modules/filesystem/physfs/wrap_File.h b/src/modules/filesystem/physfs/wrap_File.h index c012ac343..5ea7c42e9 100644 --- a/src/modules/filesystem/physfs/wrap_File.h +++ b/src/modules/filesystem/physfs/wrap_File.h @@ -23,6 +23,7 @@ // LOVE #include +#include "Filesystem.h" #include "File.h" namespace love @@ -41,7 +42,6 @@ namespace physfs int w_File_tell(lua_State * L); int w_File_seek(lua_State * L); int w_File_lines(lua_State * L); - int lines_i(lua_State * L); int luaopen_file(lua_State * L); } // physfs } // filesystem