file: A compact file library
Posted: Wed Nov 24, 2010 7:17 am
The file library!
The file library is a compact set of 3 functions for file manipulation.
This is separate from the built-in file system in that it handles in the same directory as the .love.
The library are useful if you want to load text or save text quickly, without the hassles of the love filesystem and its appdata nonsense.
The functions
file.Write(path, text)
Writes text to a file, overwrites existing text unless true is passed for the append argument.
file.Read(path, lines)
Returns the text of the given file, or a table containing the separate lines if lines is passed as true.
file.Exists(path)
Returns true if the given file exists.
The full library:
Have fun ignoring love.
This should work on any computer, it may require Lua for Windows, but shouldn't. (the io library is a main part of lua, as far as I know.)
The file library is a compact set of 3 functions for file manipulation.
This is separate from the built-in file system in that it handles in the same directory as the .love.
The library are useful if you want to load text or save text quickly, without the hassles of the love filesystem and its appdata nonsense.
The functions
file.Write(path, text)
Writes text to a file, overwrites existing text unless true is passed for the append argument.
file.Read(path, lines)
Returns the text of the given file, or a table containing the separate lines if lines is passed as true.
file.Exists(path)
Returns true if the given file exists.
The full library:
Code: Select all
file = {}
function file.Write(p, t, a)
local fyle, err = io.open(p, "w")
if err then error(err) end
if not a then
fyle:write(tostring(t))
else
fyle:write(file.Read(p)..tostring(t))
end
fyle:close()
end
function file.Exists(p)
local file, err = io.open(p, "r")
if err and string.find(err, "No such file or directory") then return false end
file:close()
return true
end
function file.Read(p, l)
if not file.Exists(p) then return end
local file, err = io.open(p, "r")
local r
if not l then
r = file:read()
else
r = {}
while true do
local line = file:read("*line")
if not line then break end
table.insert(r, line)
end
end
file:close()
return r
end
This should work on any computer, it may require Lua for Windows, but shouldn't. (the io library is a main part of lua, as far as I know.)