file: A compact file library

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
ZenX2
Citizen
Posts: 89
Joined: Wed Nov 17, 2010 5:35 am

file: A compact file library

Post 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.)
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: file: A compact file library

Post 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!
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: file: A compact file library

Post by nevon »

But... What... Why...?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: file: A compact file library

Post 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)
When I write def I mean function.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: file: A compact file library

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: file: A compact file library

Post 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.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: file: A compact file library

Post 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()
Hello, I am not dead.
User avatar
ZenX2
Citizen
Posts: 89
Joined: Wed Nov 17, 2010 5:35 am

Re: file: A compact file library

Post by ZenX2 »

This is mostly for development and not saving, it gives you the ability to have quick input/output.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: file: A compact file library

Post 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.
Help us help you: attach a .love.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: file: A compact file library

Post 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.
Hello, I am not dead.
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests