Realtime filesysten [WINDOWS]

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Engineer
Prole
Posts: 13
Joined: Mon Aug 05, 2013 9:30 pm

Realtime filesysten [WINDOWS]

Post by Engineer » Wed Oct 23, 2013 10:24 am

Hi,

Maybe you recognise me from the "support and development" subforum, where I asked about the love.filesystem.* . It turns out that those are only relative to the game/save directory, I needed something to make files externally so I came up with those utilities to check if something exists.. etc.

Here is the code:

Code: Select all

 _G.customFilesystem = {}

function customFilesystem.isDirectory( sPath )
	assert( type( sPath ) == "string", "String expected, got " .. type( sPath ), 2 )
	
	local response = os.execute( "cd " .. sPath )
	if response == 1 then
		return false
	end
	return true
end

function customFilesystem.isFile( sPath )
	assert( type( sPath ) == "string", "String expected, got " .. type( sPath ), 2 )

	local file = io.open( sPath, "r" )
	if not file then
		return false
	end
	return true
end

function customFilesystem.exists( sPath )
	assert( type( sPath ) == "string", "String expected, got " .. type( sPath ), 2 )

	if customFilesystem.isFile( sPath ) or customFilesystem.isDirectory( sPath ) then
		return true
	end
	return false
end

function customFilesystem.enumerate( sDirectory )
	assert( type( sDirectory ) == "string", "String expected, got " .. type( sDirectory ), 2 )

	local t = {}
    for filename in io.popen('dir "'..directory..'" /b'):lines() do
        table.insert( t, filename )
    end
    return t
end

function customFilesystem.mkdir( sPath )
	assert( customFilesystem.exists( sPath ), "Filepath already exists.", 2 )

	local response = os.execute( "mkdir " .. sPath )
	if response == 1 then
		return false
	end
	return true
end

function customFilesystem.lines( sPath )
	assert( type( sPath ) == "string", "String expected, got " .. type( sPath ), 2 )

	if not customFilesystem.isFile( sPath ) then
		error( "File does not exist.", 2 )
	end

	local lines = {}
	for line in io.lines( sPath ) do
		table.insert( lines, line )
	end

	local i = 0
	return function()
		i = i + 1
		return lines[ i ]
	end
end

function customFilesystem.remove( sPath )
	assert( type( sPath ) == "string", "String expected, got " .. type( sPath ), 2 )

	if not customFilesystem.exists( sPath ) then
		error( "File does not exist!", 2 )
	end

	local command = "rm"
	if customFilesystem.isDirectory( sPath ) then
		command = command .. "dir"
	end

	local response = os.execute( command .. " " .. sPath )
	if response == 1 then
		return false
	end
	return true
end
WINDOWS only as of now. If I figure out a way to check operating systems, I might implement other OS's.
To open a file, read or write, use the io library.

I thought I would share this, just because. Ignore this if this is useless.

Thanks, Engineer

User avatar
Ref
Party member
Posts: 690
Joined: Wed May 02, 2012 11:05 pm

Re: Realtime filesysten [WINDOWS]

Post by Ref » Wed Oct 23, 2013 10:37 pm

Does not play nicely with Love.
Opens and closes multiple windows and sPath requires different inputs depending on function called.

User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: Realtime filesysten [WINDOWS]

Post by tavuntu » Thu Oct 24, 2013 4:13 pm

I see what you're trying to do and, believe me, I don't want to discourage you, but love.filesystem is more than enough for IO operations in löve :)
Regards.

User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Realtime filesysten [WINDOWS]

Post by Kingdaro » Thu Oct 24, 2013 4:57 pm

tavuntu wrote:I see what you're trying to do and, believe me, I don't want to discourage you, but love.filesystem is more than enough for IO operations in löve :)
Regards.
I agree. LÖVE is mainly a game engine, and most games have no need to create files outside a specified directory, created to stay out of your way except only when you need to access it. If you're making a utility program that edits files outside the save directory, you probably shouldn't be using LÖVE.

linux-man
Citizen
Posts: 67
Joined: Thu Apr 28, 2011 4:51 pm

Re: Realtime filesysten [WINDOWS]

Post by linux-man » Thu Oct 24, 2013 6:33 pm


User avatar
Lafolie
Inner party member
Posts: 805
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Realtime filesysten [WINDOWS]

Post by Lafolie » Fri Oct 25, 2013 1:06 pm

I've previously used LuaFileSystem with much success, it's easy enough to use. My implentation was in pure Lua, but it should work with Love.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.

jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Realtime filesysten [WINDOWS]

Post by jjmafiae » Fri Oct 25, 2013 8:27 pm

love._os

User avatar
Ref
Party member
Posts: 690
Joined: Wed May 02, 2012 11:05 pm

Re: Realtime filesysten [WINDOWS]

Post by Ref » Fri Oct 25, 2013 9:12 pm

Lafolie wrote:I've previously used LuaFileSystem with much success, it's easy enough to use. My implentation was in pure Lua, but it should work with Love.
Works fine for Lua but now you run into the problem that Love has with .dll's.
Don't know if 0.9 fixes this or not.
Best to stay with love.filesystem for now.

User avatar
slime
Solid Snayke
Posts: 2918
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Realtime filesysten [WINDOWS]

Post by slime » Fri Oct 25, 2013 9:18 pm

Ref wrote:Works fine for Lua but now you run into the problem that Love has with .dll's.
Don't know if 0.9 fixes this or not.
What problem?

User avatar
Lafolie
Inner party member
Posts: 805
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Realtime filesysten [WINDOWS]

Post by Lafolie » Fri Oct 25, 2013 10:09 pm

I've previously loaded enet in 0.8.0 without problems, I can't see why LuaFileSystem wouldn't work.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests