Page 1 of 1

Realtime filesysten [WINDOWS]

Posted: Wed Oct 23, 2013 10:24 am
by Engineer
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

Re: Realtime filesysten [WINDOWS]

Posted: Wed Oct 23, 2013 10:37 pm
by Ref
Does not play nicely with Love.
Opens and closes multiple windows and sPath requires different inputs depending on function called.

Re: Realtime filesysten [WINDOWS]

Posted: Thu Oct 24, 2013 4:13 pm
by tavuntu
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.

Re: Realtime filesysten [WINDOWS]

Posted: Thu Oct 24, 2013 4:57 pm
by Kingdaro
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.

Re: Realtime filesysten [WINDOWS]

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

Re: Realtime filesysten [WINDOWS]

Posted: Fri Oct 25, 2013 1:06 pm
by Lafolie
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.

Re: Realtime filesysten [WINDOWS]

Posted: Fri Oct 25, 2013 8:27 pm
by jjmafiae
love._os

Re: Realtime filesysten [WINDOWS]

Posted: Fri Oct 25, 2013 9:12 pm
by Ref
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.

Re: Realtime filesysten [WINDOWS]

Posted: Fri Oct 25, 2013 9:18 pm
by slime
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?

Re: Realtime filesysten [WINDOWS]

Posted: Fri Oct 25, 2013 10:09 pm
by Lafolie
I've previously loaded enet in 0.8.0 without problems, I can't see why LuaFileSystem wouldn't work.