mirror of
https://github.com/love2d/love.git
synced 2026-08-17 19:23:38 +02:00
Improve love.filesystem.lines
This commit is contained in:
@@ -445,109 +445,103 @@ namespace physfs
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Filesystem::lines(lua_State * L)
|
|
||||||
{
|
|
||||||
File * file;
|
|
||||||
|
|
||||||
if (lua_isstring(L, 1))
|
|
||||||
{
|
|
||||||
file = newFile(lua_tostring(L, 1));
|
|
||||||
if (!file->open(File::READ))
|
|
||||||
return luaL_error(L, "Could not open file %s.\n", lua_tostring(L, 1));
|
|
||||||
lua_pop(L, 1);
|
|
||||||
|
|
||||||
luax_newtype(L, "File", FILESYSTEM_FILE_T, file, false);
|
|
||||||
lua_pushnumber(L, 1); // 1 = autoclose.
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return luaL_error(L, "Expected filename.");
|
|
||||||
|
|
||||||
// Reset the file position.
|
|
||||||
if (!file->seek(0))
|
|
||||||
return luaL_error(L, "File does not appear to be open.\n");
|
|
||||||
|
|
||||||
lua_pushcclosure(L, lines_i, 2);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Filesystem::lines_i(lua_State * L)
|
int Filesystem::lines_i(lua_State * L)
|
||||||
{
|
{
|
||||||
// We're using a 1k buffer.
|
const int bufsize = 1024;
|
||||||
const static int bufsize = 8;
|
char buf[bufsize];
|
||||||
static char buf[bufsize];
|
int linesize = 0;
|
||||||
|
bool newline = false;
|
||||||
|
|
||||||
File * file = luax_checktype<File>(L, lua_upvalueindex(1), "File", FILESYSTEM_FILE_T);
|
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.
|
// Only accept read mode at this point.
|
||||||
// pos must be at the start of the line we're trying to find.
|
if (file->getMode() != File::READ)
|
||||||
|
return luaL_error(L, "File needs to stay in read mode.");
|
||||||
|
|
||||||
int64 pos = file->tell();
|
int64 pos = file->tell();
|
||||||
int newline = -1;
|
int64 userpos = -1;
|
||||||
int totalread = 0;
|
|
||||||
|
|
||||||
while (!file->eof())
|
if (lua_isnoneornil(L, lua_upvalueindex(2)) == 0)
|
||||||
{
|
{
|
||||||
int64 current = file->tell();
|
// User may have changed the file position.
|
||||||
int64 read = file->read(buf, bufsize);
|
userpos = pos;
|
||||||
totalread += (int) read; //TODO: Support integer-overflowing files/lines
|
pos = (int64) lua_tonumber(L, lua_upvalueindex(2));
|
||||||
|
if (userpos != pos)
|
||||||
|
file->seek(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!newline && !file->eof())
|
||||||
|
{
|
||||||
|
// This 64-bit to 32-bit integer cast should be safe as it never exceeds bufsize.
|
||||||
|
int read = (int) file->read(buf, bufsize);
|
||||||
if (read < 0)
|
if (read < 0)
|
||||||
return luaL_error(L, "Readline failed!");
|
return luaL_error(L, "Could not read from file.");
|
||||||
|
|
||||||
for (int i = 0;i<read;i++)
|
linesize += read;
|
||||||
|
|
||||||
|
for (int i = 0; i < read; i++)
|
||||||
{
|
{
|
||||||
if (buf[i] == '\n')
|
if (buf[i] == '\n')
|
||||||
{
|
{
|
||||||
newline = (int) current+i; // TODO: See above
|
linesize -= read - i;
|
||||||
|
newline = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newline > 0)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special case for the last "line".
|
if (newline || (file->eof() && linesize > 0))
|
||||||
if (newline <= 0 && file->eof() && totalread > 0)
|
|
||||||
newline = (int) pos + totalread; // TODO: See above
|
|
||||||
|
|
||||||
// We've got a newline.
|
|
||||||
if (newline > 0)
|
|
||||||
{
|
{
|
||||||
// Ok, we've got a line.
|
if (linesize < bufsize)
|
||||||
int linesize = (newline-(int) pos); // TODO: See above
|
{
|
||||||
|
// We have the line in the buffer on the stack. No 'new' and 'read' needed.
|
||||||
|
lua_pushlstring(L, buf, linesize > 0 && buf[linesize - 1] == '\r' ? linesize - 1 : linesize);
|
||||||
|
if (userpos < 0)
|
||||||
|
file->seek(pos + linesize + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char * str;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
str = new char[linesize + 1];
|
||||||
|
}
|
||||||
|
catch (std::bad_alloc &)
|
||||||
|
{
|
||||||
|
return luaL_error(L, "Out of memory");
|
||||||
|
}
|
||||||
|
file->seek(pos);
|
||||||
|
|
||||||
// Allocate memory for the string.
|
// Read the \n anyway and save us a call to seek.
|
||||||
char * str = new char[linesize];
|
if (file->read(str, linesize + 1) == -1)
|
||||||
|
{
|
||||||
|
delete [] str;
|
||||||
|
return luaL_error(L, "Could not read from file.");
|
||||||
|
}
|
||||||
|
|
||||||
// Read it.
|
lua_pushlstring(L, str, str[linesize - 1] == '\r' ? linesize - 1 : linesize);
|
||||||
file->seek(pos);
|
delete [] str;
|
||||||
if (file->read(str, linesize) == -1)
|
}
|
||||||
return luaL_error(L, "Read error.");
|
|
||||||
|
|
||||||
if (str[linesize-1]=='\r')
|
if (userpos >= 0)
|
||||||
linesize -= 1;
|
{
|
||||||
|
// Save new position in upvalue.
|
||||||
lua_pushlstring(L, str, linesize);
|
lua_pushnumber(L, (lua_Number) (pos + linesize + 1));
|
||||||
|
lua_replace(L, lua_upvalueindex(2));
|
||||||
// Free the memory. Lua has a copy now.
|
file->seek(userpos);
|
||||||
delete[] str;
|
}
|
||||||
|
|
||||||
// Set the beginning of the next line.
|
|
||||||
if (!file->eof())
|
|
||||||
file->seek(newline+1);
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (close)
|
// EOF reached.
|
||||||
{
|
if (userpos >= 0 && luax_toboolean(L, lua_upvalueindex(3)))
|
||||||
|
file->seek(userpos);
|
||||||
|
else
|
||||||
file->close();
|
file->close();
|
||||||
file->release();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Filesystem::load(lua_State * L)
|
int Filesystem::load(lua_State * L)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -270,17 +270,6 @@ namespace physfs
|
|||||||
**/
|
**/
|
||||||
int enumerate(lua_State * L);
|
int enumerate(lua_State * L);
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an iterator which iterates over
|
|
||||||
* lines in files.
|
|
||||||
**/
|
|
||||||
int lines(lua_State * L);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The line iterator function.
|
|
||||||
**/
|
|
||||||
static int lines_i(lua_State * L);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a file without running it. The loaded
|
* Loads a file without running it. The loaded
|
||||||
* chunk is returned as a function.
|
* chunk is returned as a function.
|
||||||
@@ -291,6 +280,13 @@ namespace physfs
|
|||||||
|
|
||||||
int getLastModified(lua_State * L);
|
int getLastModified(lua_State * L);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Text file line-reading iterator function used and
|
||||||
|
* pushed on the Lua stack by love.filesystem.lines
|
||||||
|
* and File:lines.
|
||||||
|
**/
|
||||||
|
static int lines_i(lua_State * L);
|
||||||
|
|
||||||
}; // Filesystem
|
}; // Filesystem
|
||||||
|
|
||||||
} // physfs
|
} // physfs
|
||||||
|
|||||||
@@ -39,13 +39,13 @@ namespace physfs
|
|||||||
{
|
{
|
||||||
File * t = luax_checkfile(L, 1);
|
File * t = luax_checkfile(L, 1);
|
||||||
int64 size = t->getSize();
|
int64 size = t->getSize();
|
||||||
|
|
||||||
// Push nil on failure or if size does not fit into a double precision floating-point number.
|
// Push nil on failure or if size does not fit into a double precision floating-point number.
|
||||||
if (size == -1 || size >= 0x20000000000000LL)
|
if (size == -1 || size >= 0x20000000000000LL)
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
else
|
else
|
||||||
lua_pushnumber(L, (lua_Number)size);
|
lua_pushnumber(L, (lua_Number)size);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +80,7 @@ namespace physfs
|
|||||||
{
|
{
|
||||||
File * file = luax_checkfile(L, 1);
|
File * file = luax_checkfile(L, 1);
|
||||||
Data * d = 0;
|
Data * d = 0;
|
||||||
|
|
||||||
int64 size = (int64)luaL_optnumber(L, 2, (lua_Number) file->getSize());
|
int64 size = (int64)luaL_optnumber(L, 2, (lua_Number) file->getSize());
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -159,7 +159,7 @@ namespace physfs
|
|||||||
{
|
{
|
||||||
File * file = luax_checkfile(L, 1);
|
File * file = luax_checkfile(L, 1);
|
||||||
lua_Number pos = luaL_checknumber(L, 2);
|
lua_Number pos = luaL_checknumber(L, 2);
|
||||||
|
|
||||||
// Push false on negative and precision-problematic numbers.
|
// Push false on negative and precision-problematic numbers.
|
||||||
// Better fail than seek to an unknown position.
|
// Better fail than seek to an unknown position.
|
||||||
if (pos < 0.0 || pos >= 9007199254740992.0)
|
if (pos < 0.0 || pos >= 9007199254740992.0)
|
||||||
@@ -169,8 +169,6 @@ namespace physfs
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//yes, the following two are copy-pasted and slightly edited
|
|
||||||
|
|
||||||
int w_File_lines(lua_State * L)
|
int w_File_lines(lua_State * L)
|
||||||
{
|
{
|
||||||
File * file;
|
File * file;
|
||||||
@@ -178,30 +176,43 @@ namespace physfs
|
|||||||
if (luax_istype(L, 1, FILESYSTEM_FILE_T))
|
if (luax_istype(L, 1, FILESYSTEM_FILE_T))
|
||||||
{
|
{
|
||||||
file = luax_checktype<File>(L, 1, "File", FILESYSTEM_FILE_T);
|
file = luax_checktype<File>(L, 1, "File", FILESYSTEM_FILE_T);
|
||||||
lua_pushnumber(L, 0); // 0 = do not close.
|
lua_pushnumber(L, 0); // File position.
|
||||||
|
luax_pushboolean(L, file->getMode() != File::CLOSED); // Save current file mode.
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return luaL_error(L, "Expected file handle.");
|
return luaL_error(L, "Expected File.");
|
||||||
|
|
||||||
// Reset the file position.
|
if (file->getMode() != File::READ)
|
||||||
if (!file->seek(0))
|
{
|
||||||
return luaL_error(L, "File does not appear to be open.\n");
|
if (file->getMode() != File::CLOSED)
|
||||||
|
file->close();
|
||||||
|
|
||||||
lua_pushcclosure(L, Filesystem::lines_i, 2);
|
try
|
||||||
|
{
|
||||||
|
if (!file->open(File::READ))
|
||||||
|
return luaL_error(L, "Could not open file.");
|
||||||
|
}
|
||||||
|
catch (love::Exception & e)
|
||||||
|
{
|
||||||
|
return luaL_error(L, "%s", e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushcclosure(L, Filesystem::lines_i, 3);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const luaL_Reg functions[] = {
|
static const luaL_Reg functions[] = {
|
||||||
{ "getSize", w_File_getSize },
|
{ "getSize", w_File_getSize },
|
||||||
{ "open", w_File_open },
|
{ "open", w_File_open },
|
||||||
{ "close", w_File_close },
|
{ "close", w_File_close },
|
||||||
{ "read", w_File_read },
|
{ "read", w_File_read },
|
||||||
{ "write", w_File_write },
|
{ "write", w_File_write },
|
||||||
{ "eof", w_File_eof },
|
{ "eof", w_File_eof },
|
||||||
{ "tell", w_File_tell },
|
{ "tell", w_File_tell },
|
||||||
{ "seek", w_File_seek },
|
{ "seek", w_File_seek },
|
||||||
{ "lines", w_File_lines },
|
{ "lines", w_File_lines },
|
||||||
{ 0, 0 }
|
{ 0, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
extern "C" int luaopen_file(lua_State * L)
|
extern "C" int luaopen_file(lua_State * L)
|
||||||
|
|||||||
@@ -221,14 +221,27 @@ namespace physfs
|
|||||||
|
|
||||||
int w_lines(lua_State * L)
|
int w_lines(lua_State * L)
|
||||||
{
|
{
|
||||||
try
|
File * file;
|
||||||
|
|
||||||
|
if(lua_isstring(L, 1))
|
||||||
{
|
{
|
||||||
return instance->lines(L);
|
file = instance->newFile(lua_tostring(L, 1));
|
||||||
}
|
try
|
||||||
catch (Exception &e)
|
{
|
||||||
{
|
if (!file->open(File::READ))
|
||||||
return luaL_error(L, e.what());
|
return luaL_error(L, "Could not open file.");
|
||||||
|
}
|
||||||
|
catch (love::Exception & e)
|
||||||
|
{
|
||||||
|
return luaL_error(L, "%s", e.what());
|
||||||
|
}
|
||||||
|
luax_newtype(L, "File", FILESYSTEM_FILE_T, file);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
return luaL_error(L, "Expected filename.");
|
||||||
|
|
||||||
|
lua_pushcclosure(L, Filesystem::lines_i, 1);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int w_load(lua_State * L)
|
int w_load(lua_State * L)
|
||||||
|
|||||||
Reference in New Issue
Block a user