mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Remove duplicate lines_i, finally fix leak (issue #271)
This commit is contained in:
@@ -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.");
|
||||
|
||||
@@ -162,7 +162,7 @@ namespace physfs
|
||||
if(luax_istype(L, 1, FILESYSTEM_FILE_T))
|
||||
{
|
||||
file = luax_checktype<File>(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<File>(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<read;i++)
|
||||
{
|
||||
if(buf[i] == '\n')
|
||||
{
|
||||
newline = current+i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(newline > 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 },
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
// LOVE
|
||||
#include <common/runtime.h>
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user