-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathapi.go
59 lines (53 loc) · 1.38 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
// Package ioutil implements golang package ioutil functionality for lua.
package ioutil
import (
lio "github.com/vadv/gopher-lua-libs/io"
"io"
"io/ioutil"
lua "github.com/yuin/gopher-lua"
)
// ReadFile lua ioutil.read_file(filepath) reads the file named by filename and returns the contents, returns (string,error)
func ReadFile(L *lua.LState) int {
filename := L.CheckString(1)
data, err := ioutil.ReadFile(filename)
if err == nil {
L.Push(lua.LString(data))
return 1
} else {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
}
// WriteFile lua ioutil.write_file(filepath, data) reads the file named by filename and returns the contents, returns (string,error)
func WriteFile(L *lua.LState) int {
filename := L.CheckString(1)
data := L.CheckString(2)
err := ioutil.WriteFile(filename, []byte(data), 0644)
if err != nil {
L.Push(lua.LString(err.Error()))
return 1
}
return 0
}
func Copy(L *lua.LState) int {
writer := lio.CheckIOWriter(L, 1)
reader := lio.CheckIOReader(L, 2)
L.Pop(L.GetTop())
if _, err := io.Copy(writer, reader); err != nil {
L.Push(lua.LString(err.Error()))
return 1
}
return 0
}
func CopyN(L *lua.LState) int {
writer := lio.CheckIOWriter(L, 1)
reader := lio.CheckIOReader(L, 2)
n := L.CheckInt64(3)
L.Pop(L.GetTop())
if _, err := io.CopyN(writer, reader, n); err != nil {
L.Push(lua.LString(err.Error()))
return 1
}
return 0
}