Page 1 of 2

file: A compact file library

Posted: Wed Nov 24, 2010 7:17 am
by ZenX2
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:

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
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.)

Re: file: A compact file library

Posted: Wed Nov 24, 2010 7:28 am
by TechnoCat
ZenX2 wrote:The file library!

The functions

file.Write(path, text)

file.Read(path, lines)

file.Exists(path)
The LOVE filesystem module!

The functions

love.filesystem.write

love.filesystem.read and love.filesystem.lines

love.filesystem.exists

And many more!
Also comes with the bonus of reading files both in your save directory and in your love file, transparently to the programmer!

Re: file: A compact file library

Posted: Wed Nov 24, 2010 8:20 am
by nevon
But... What... Why...?

Re: file: A compact file library

Posted: Wed Nov 24, 2010 8:27 am
by kikito
ZenX2 wrote: ...

Code: Select all


	local fyle, err = io.open(p, "w")
...
	local file, err = io.open(p, "r")
...
	local file, err = io.open(p, "r")
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.)
I might be wrong, but I think io.open is explicitly forbidden in LÖVE. In addition, LÖVE comes with its own Lua implementation, and ignore any other Luas that might be installed in your computer. So the code above should never work inside a .love file (unless you did something like implementing io.open natively and providing a dll ... thus breaking compatibility)

Re: file: A compact file library

Posted: Wed Nov 24, 2010 8:36 am
by nevon
kikito wrote:I might be wrong, but I think io.open is explicitly forbidden in LÖVE.
No. I'm pretty sure io.open is available. It might not be in SELÖVE though.

Re: file: A compact file library

Posted: Wed Nov 24, 2010 6:03 pm
by bartbes
ZenX2 wrote:ithout the hassles of the love filesystem and its appdata nonsense.
Have fun ignoring love.
I disapprove.
ZenX2 wrote: 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.)
It does.

Re: file: A compact file library

Posted: Wed Nov 24, 2010 6:30 pm
by zac352
kikito wrote:
ZenX2 wrote: ...

Code: Select all


	local fyle, err = io.open(p, "w")
...
	local file, err = io.open(p, "r")
...
	local file, err = io.open(p, "r")
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.)
I might be wrong, but I think io.open is explicitly forbidden in LÖVE. In addition, LÖVE comes with its own Lua implementation, and ignore any other Luas that might be installed in your computer. So the code above should never work inside a .love file (unless you did something like implementing io.open natively and providing a dll ... thus breaking compatibility)
Well, love does have the io library.
I use this occasionally:

Code: Select all

io.stdin:read()

Re: file: A compact file library

Posted: Wed Nov 24, 2010 9:17 pm
by ZenX2
This is mostly for development and not saving, it gives you the ability to have quick input/output.

Re: file: A compact file library

Posted: Wed Nov 24, 2010 11:59 pm
by Robin
nevon wrote:I'm pretty sure io.open is available. It might not be in SELÖVE though.
It most certainly is not in SELÖVE. The io library provides access to everything the current user has access to, which could very seriously be abused.

Re: file: A compact file library

Posted: Thu Nov 25, 2010 12:11 am
by zac352
Robin wrote:
nevon wrote:I'm pretty sure io.open is available. It might not be in SELÖVE though.
It most certainly is not in SELÖVE. The io library provides access to everything the current user has access to, which could very seriously be abused.
So you're saying that if I run "sudo gedit /some/locked/file", then open another tab and run "love somegame.love", io.open can open /some/locked/file? I don't think so.