Realtime filesysten [WINDOWS]
Posted: 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:
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
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
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