-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
155 lines (147 loc) · 3.71 KB
/
file.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
package builtin_libs
import (
"bufio"
"errors"
"io"
"io/ioutil"
"os"
. "github.com/zgg-lang/zgg-go/runtime"
)
var (
fileFileReaderClass ValueType
)
func libFile(*Context) ValueObject {
lib := NewObject()
lib.SetMember("readBytes", NewNativeFunction("file.readBytes", func(c *Context, this Value, args []Value) Value {
if len(args) != 1 {
c.OnRuntimeError("file.readBytes requires 1 argument")
return nil
}
filename := c.MustStr(args[0])
bytes, err := ioutil.ReadFile(filename)
if err != nil {
c.OnRuntimeError("read file error: %s", err.Error())
return nil
}
return NewBytes(bytes)
}), nil)
lib.SetMember("readString", NewNativeFunction("file.readString", func(c *Context, this Value, args []Value) Value {
if len(args) != 1 {
c.OnRuntimeError("file.readString requires 1 argument")
return nil
}
filename := c.MustStr(args[0])
bytes, err := ioutil.ReadFile(filename)
if err != nil {
c.OnRuntimeError("read file error: %s", err.Error())
return nil
}
return NewStr(string(bytes))
}), nil)
lib.SetMember("readLines", NewNativeFunction("file.readLines", func(c *Context, this Value, args []Value) Value {
if len(args) != 1 {
c.OnRuntimeError("file.readLines requires 1 argument")
return nil
}
filename := c.MustStr(args[0])
file, err := os.Open(filename)
if err != nil {
c.OnRuntimeError("open file error: %s", err.Error())
return nil
}
defer file.Close()
rd := bufio.NewReader(file)
rv := NewArray()
for {
line, err := rd.ReadString('\n')
if err == nil {
rv.PushBack(NewStr(line))
} else if errors.Is(err, io.EOF) {
rv.PushBack(NewStr(line))
break
} else {
c.OnRuntimeError("read lines error %s", err)
}
}
return rv
}), nil)
lib.SetMember("rewrite", NewNativeFunction("file.rewrite", func(c *Context, this Value, args []Value) Value {
if len(args) < 1 {
c.OnRuntimeError("file.rewrite requires at lease 1 argument")
return nil
}
filename := c.MustStr(args[0])
file, err := os.Create(filename)
if err != nil {
c.OnRuntimeError("create file error: %s", err.Error())
return nil
}
defer file.Close()
total := 0
for i := 1; i < len(args); i++ {
var bs []byte
switch v := args[i].(type) {
case ValueBytes:
bs = v.Value()
default:
bs = []byte(v.ToString(c))
}
for written := 0; written < len(bs); {
n, err := file.Write(bs[written:])
if err != nil {
c.OnRuntimeError("write file error: %s", err)
return nil
}
written += n
}
total += len(bs)
}
return NewInt(int64(total))
}), nil)
lib.SetMember("append", NewNativeFunction("file.append", func(c *Context, this Value, args []Value) Value {
if len(args) < 1 {
c.OnRuntimeError("file.append requires at lease 1 argument")
return nil
}
filename := c.MustStr(args[0])
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
c.OnRuntimeError("create file error: %s", err.Error())
return nil
}
defer file.Close()
total := 0
for i := 1; i < len(args); i++ {
var bs []byte
switch v := args[i].(type) {
case ValueBytes:
bs = v.Value()
default:
bs = []byte(v.ToString(c))
}
for written := 0; written < len(bs); {
n, err := file.Write(bs[written:])
if err != nil {
c.OnRuntimeError("write file error: %s", err)
return nil
}
written += n
}
total += len(bs)
}
return NewInt(int64(total))
}), nil)
return lib
}
func initFileClass() {
fileFileReaderClass = NewClassBuilder("FileReader").
Constructor(func(c *Context, this ValueObject, args []Value) {
this.SetMember("_file", args[0], c)
}).
Method("nextLine", func(c *Context, this ValueObject, args []Value) Value {
return nil
}).
Build()
}
func init() {
}