Facepunch / garrysmod-requests

Feature requests for Garry's Mod
83 stars 24 forks source link

file.Lines iterator #1101

Open ajloveslily14 opened 6 years ago

ajloveslily14 commented 6 years ago

In 'vanilla' lua there's an iterator for lines in files eg:

local f = io.open("file.txt","wr")
for line in f:lines() do
    print(line)
end

Currently in glua there is no such iterator and you have to make your own, with something like lines = string.Explode("\n",file) and then iterate through that table, which does work however can be cumbersome.

meepen commented 6 years ago

i'd honestly love for the file. functions to just wrap over the default io. code i don't think it'd ever happen but it'd be great

ajloveslily14 commented 6 years ago

Well, it would have to be modified anyways to not allow files being read from anywhere.

thegrb93 commented 6 years ago
local f = file.Open("file.txt","wr", "DATA")
while true do
    local line = f:ReadLine()
    if line == nil then break end
end
f:Close()

I'm assuming ReadLine returns nil at eof, but not sure.

ajloveslily14 commented 6 years ago

Does ReadLine automatically skip to the next line?

thegrb93 commented 6 years ago

yea. any of the Read* functions advance the file position.

thegrb93 commented 6 years ago

Theres a buffer limit on the ReadLine function though, check the wiki for details.

ajloveslily14 commented 6 years ago

I see. That's nice though I don't really like reinventing wheels heh

GitSparTV commented 5 years ago

One more example:

for line in f.ReadLine,f do

end