-
Notifications
You must be signed in to change notification settings - Fork 2
/
luapath.go
167 lines (135 loc) · 3.04 KB
/
luapath.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"os"
"path"
"path/filepath"
"github.com/slotopol/bot/util"
lua "github.com/yuin/gopher-lua"
)
// RegPath is the module loader function.
func RegPath(ls *lua.LState) {
var mod = ls.RegisterModule("path", pathfuncs).(*lua.LTable)
mod.RawSetString("sep", lua.LString("/"))
}
var pathfuncs = map[string]lua.LGFunction{
"toslash": pathtoslash,
"clean": pathclean,
"volume": pathvolume,
"dir": pathdir,
"base": pathbase,
"name": pathname,
"ext": pathext,
"split": pathsplit,
"match": pathmatch,
"join": pathjoin,
"glob": pathglob,
"enum": pathenum,
"envfmt": pathenvfmt,
}
func pathtoslash(ls *lua.LState) int {
var fpath = ls.CheckString(1)
ls.Push(lua.LString(util.ToSlash(fpath)))
return 1
}
func pathclean(ls *lua.LState) int {
var fpath = ls.CheckString(1)
var path = path.Clean(fpath)
ls.Push(lua.LString(path))
return 1
}
func pathvolume(ls *lua.LState) int {
var fpath = ls.CheckString(1)
var vol = filepath.VolumeName(fpath)
ls.Push(lua.LString(vol))
return 1
}
func pathdir(ls *lua.LState) int {
var fpath = ls.CheckString(1)
var dir = path.Dir(fpath)
ls.Push(lua.LString(dir))
return 1
}
func pathbase(ls *lua.LState) int {
var fpath = ls.CheckString(1)
var base = path.Base(fpath)
ls.Push(lua.LString(base))
return 1
}
func pathname(ls *lua.LState) int {
var fpath = ls.CheckString(1)
var name = util.PathName(fpath)
ls.Push(lua.LString(name))
return 1
}
func pathext(ls *lua.LState) int {
var fpath = ls.CheckString(1)
var ext = path.Ext(fpath)
ls.Push(lua.LString(ext))
return 1
}
func pathsplit(ls *lua.LState) int {
var fpath = ls.CheckString(1)
var dir, file = path.Split(fpath)
ls.Push(lua.LString(dir))
ls.Push(lua.LString(file))
return 2
}
func pathmatch(ls *lua.LState) int {
var name = ls.CheckString(1)
var pattern = ls.CheckString(2)
var matched, err = path.Match(name, pattern)
if err != nil {
ls.RaiseError(err.Error())
return 0
}
ls.Push(lua.LBool(matched))
return 1
}
func pathjoin(ls *lua.LState) int {
var elem = make([]string, ls.GetTop())
for i := range elem {
elem[i] = ls.CheckString(i + 1)
}
var dir = path.Join(elem...)
ls.Push(lua.LString(dir))
return 1
}
func pathglob(ls *lua.LState) int {
var pattern = ls.CheckString(1)
var matches, err = filepath.Glob(pattern)
if err != nil {
ls.RaiseError(err.Error())
return 0
}
for _, dir := range matches {
ls.Push(lua.LString(util.ToSlash(dir)))
}
return len(matches)
}
func pathenum(ls *lua.LState) int {
var dirname = ls.CheckString(1)
var n = ls.OptInt(2, -1)
var dir, err = os.Open(dirname)
if err != nil {
ls.RaiseError(err.Error())
return 0
}
defer dir.Close()
var names []string
if names, err = dir.Readdirnames(n); err != nil {
ls.RaiseError(err.Error())
return 0
}
var tb = ls.CreateTable(len(names), 0)
for i, name := range names {
tb.RawSetInt(i+1, lua.LString(name))
}
ls.Push(tb)
return 1
}
func pathenvfmt(ls *lua.LState) int {
var fpath = ls.CheckString(1)
ls.Push(lua.LString(util.Envfmt(fpath, nil)))
return 1
}
// The End.