Repository navigation
Expand file tree
/
Copy pathIO.lua
More file actions
85 lines (73 loc) · 1.75 KB
/
Copy pathIO.lua
File metadata and controls
85 lines (73 loc) · 1.75 KB
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
77
78
79
80
81
82
83
84
85
local IO = {}
local string_byte = string.byte
local string_len = string.len
local string_sub = string.sub
function IO.exists(path)
local file = io.open(path, "rb")
if file then
io.close(file)
return true
end
return false
end
function IO.readfile(path)
local file = io.open(path, "rb")
if file then
local content = file:read("*a")
io.close(file)
return content
end
return nil
end
function IO.writefile(path, content, mode)
mode = mode or "w+b"
local file = io.open(path, mode)
if file then
if file:write(content) == nil then
return false
end
io.close(file)
return true
else
return false
end
end
function IO.pathinfo(path)
local pos = string_len(path)
local extpos = pos + 1
while pos > 0 do
local b = string_byte(path, pos)
if b == 46 then -- 46 = char "."
extpos = pos
elseif b == 47 then -- 47 = char "/"
break
end
pos = pos - 1
end
local dirname = string_sub(path, 1, pos)
local filename = string_sub(path, pos + 1)
extpos = extpos - pos
local basename = string_sub(filename, 1, extpos - 1)
local extname = string_sub(filename, extpos)
return {
dirname = dirname,
filename = filename,
basename = basename,
extname = extname
}
end
function IO.filesize(path)
local size = false
local file = io.open(path, "r")
if file then
local current = file:seek()
size = file:seek("end")
file:seek("set", current)
io.close(file)
end
return size
end
function IO.mkdir(model, data_dir)
os.execute('mkdir -m' .. model .. ' ' .. data_dir)
end
return IO