-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.lua
76 lines (68 loc) · 1.56 KB
/
fs.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
local fs = {}
local lfs = nil
local separator = package.config:match("^([^\n]*)")
if love ~= nil then
elseif pcall(function () lfs = require('lfs') end) then
else
error("LFS or Love are required to access the filesystem.")
end
function fs.currentDir ()
if love ~= nil then
return "/"
elseif lfs ~= nil then
return lfs.currentdir()
end
end
local function normalizePath (path)
if love ~= nil then
local normalizedPath = path:gsub("\\", "/")
return normalizedPath
end
return path
end
function fs.isDirectory (path)
if love ~= nil then
return love.filesystem.isDirectory(normalizePath(path))
elseif lfs ~= nil then
local stat = lfs.attributes(path)
if stat ~= nil then
return stat.mode == "directory"
end
return false
end
end
function fs.isFile (path)
if love ~= nil then
return love.filesystem.isFile(normalizePath(path))
elseif lfs ~= nil then
local stat = lfs.attributes(path)
if stat ~= nil then
return stat.mode == "file"
end
return false
end
end
function fs.read (path)
if love ~= nil then
return love.filesystem.read(normalizePath(path))
else
local file = io.open(path, "rb")
if file then
return file:read("*a")
end
end
end
function fs.load (path)
if love ~= nil then
return love.filesystem.load(normalizePath(path))
else
if loadstring ~= nil then
--Lua 5.1
return assert(loadstring(assert(fs.read(path)), path))
elseif load ~= nil then
--Lua 5.2+
return assert(load(assert(fs.read(path)), path))
end
end
end
return fs