-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.go
56 lines (51 loc) · 1.38 KB
/
io.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
package idris_go_rts
import . "os"
import . "bufio"
//-------------------------------------------------------------------------------------------------
// Various IO functions
//-------------------------------------------------------------------------------------------------
func FileOpen(name string, mode string) *File {
flags := 0
for _, char := range mode { // TODO: these need some work
switch char {
case 'r': flags |= O_RDONLY
case 'w': flags |= O_RDWR|O_TRUNC|O_CREATE
case 'a': flags |= O_APPEND|O_CREATE
case '+': flags |= O_RDWR
}
if flags & (O_RDWR|O_APPEND) != 0 {
flags &^= O_RDONLY
}
}
file, _ := OpenFile(name, flags, 0644)
return file
}
func FileReadLine(file *File) string {
// Save off current seek position
offset, error := file.Seek(0, SEEK_CUR)
if error == nil {
reader := NewReader(file)
line, error := reader.ReadString('\n')
if error == nil {
// Set seek position, since it's no longer correct
file.Seek(offset + int64(len(line)), SEEK_SET)
return line
}
}
return ""
}
func FileEOF(file *File) int {
info, error := file.Stat()
if error == nil {
size := info.Size()
offset, error := file.Seek(0, SEEK_CUR)
if error == nil {
if offset == size {
file.Seek(offset + 1, SEEK_SET)
} else if offset > size {
return 1
}
}
}
return 0
}