forked from vadv/gopher-lua-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
68 lines (59 loc) · 1.75 KB
/
api.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
// Package filepath implements golang filepath functionality for lua.
package filepath
import (
"path/filepath"
lua "github.com/yuin/gopher-lua"
)
// Basename lua filepath.basename(path) returns the last element of path
func Basename(L *lua.LState) int {
path := L.CheckString(1)
L.Push(lua.LString(filepath.Base(path)))
return 1
}
// Dir lua filepath.dir(path) returns all but the last element of path, typically the path's directory
func Dir(L *lua.LState) int {
path := L.CheckString(1)
L.Push(lua.LString(filepath.Dir(path)))
return 1
}
// Ext lua filepath.ext(path) returns the file name extension used by path.
func Ext(L *lua.LState) int {
path := L.CheckString(1)
L.Push(lua.LString(filepath.Ext(path)))
return 1
}
// Join lua fileapth.join(path, ...) joins any number of path elements into a single path, adding a Separator if necessary.
func Join(L *lua.LState) int {
path := L.CheckString(1)
for i := 2; i <= L.GetTop(); i++ {
add := L.CheckAny(i).String()
path = filepath.Join(path, add)
}
L.Push(lua.LString(path))
return 1
}
// Separator lua filepath.separator() OS-specific path separator
func Separator(L *lua.LState) int {
L.Push(lua.LString(filepath.Separator))
return 1
}
// ListSeparator lua filepath.list_separator() OS-specific path list separator
func ListSeparator(L *lua.LState) int {
L.Push(lua.LString(filepath.ListSeparator))
return 1
}
// filepath.glob(pattern) returns the names of all files matching pattern or nil if there is no matching file.
func Glob(L *lua.LState) int {
pattern := L.CheckString(1)
files, err := filepath.Glob(pattern)
if err != nil {
L.Push(lua.LNil)
return 1
}
result := L.CreateTable(len(files), 0)
for _, file := range files {
result.Append(lua.LString(file))
}
L.Push(result)
return 1
}