-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfile-stream.go
More file actions
113 lines (91 loc) · 3.06 KB
/
file-stream.go
File metadata and controls
113 lines (91 loc) · 3.06 KB
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
// Copyright (c) 2022, Peter Ohler, All rights reserved.
package slip
import (
"os"
"strconv"
)
// FileStreamSymbol is the symbol with a value of "file-stream".
const FileStreamSymbol = Symbol("file-stream")
// FileStream is a *os.File.
type FileStream os.File
// String representation of the Object.
func (obj *FileStream) String() string {
return string(obj.Append([]byte{}))
}
// Append a buffer with a representation of the Object.
func (obj *FileStream) Append(b []byte) []byte {
b = append(b, "#<FILE-STREAM "...)
b = append(b, (*os.File)(obj).Name()...)
b = append(b, " {"...)
b = strconv.AppendInt(b, int64((*os.File)(obj).Fd()), 10)
return append(b, "}>"...)
}
// Simplify the Object into an int64.
func (obj *FileStream) Simplify() any {
return obj.String()
}
// Equal returns true if this Object and the other are equal in value.
func (obj *FileStream) Equal(other Object) bool {
return obj == other
}
// Hierarchy returns the class hierarchy as symbols for the instance.
func (obj *FileStream) Hierarchy() []Symbol {
return []Symbol{FileStreamSymbol, StreamSymbol, TrueSymbol}
}
// StreamType returns 'fileStream.
func (obj *FileStream) StreamType() Symbol {
return FileStreamSymbol
}
// Eval returns self.
func (obj *FileStream) Eval(s *Scope, depth int) Object {
return obj
}
// Write made visible since os.File functions are not automatically visible.
func (obj *FileStream) Write(b []byte) (int, error) {
return (*os.File)(obj).Write(b)
}
// Read made visible since os.File functions are not automatically visible.
func (obj *FileStream) Read(b []byte) (int, error) {
return (*os.File)(obj).Read(b)
}
// Close made visible since os.File functions are not automatically visible.
func (obj *FileStream) Close() error {
return (*os.File)(obj).Close()
}
// IsOpen return true if the stream is open or false if not.
func (obj *FileStream) IsOpen() bool {
_, err := (*os.File)(obj).Write([]byte{})
return err == nil
}
// LastByte returns the last byte written or zero if nothing has been written.
func (obj *FileStream) LastByte() byte {
b := []byte{0}
if pos, err := (*os.File)(obj).Seek(0, 1); err == nil && 0 < pos { // relative to current
_, _ = (*os.File)(obj).ReadAt(b, pos-1)
}
return b[0]
}
// FileLength return the length of a file.
func (obj *FileStream) FileLength() (length Object) {
if fi, err := (*os.File)(obj).Stat(); err == nil {
length = Fixnum(fi.Size())
}
return
}
// Seek moves the pos in buf. This is part of the io.Seeker interface.
func (obj *FileStream) Seek(offset int64, whence int) (n int64, err error) {
return (*os.File)(obj).Seek(offset, whence)
}
// ReadRune returns the next rune in buf from the current position. This is
// part of the io.RuneReader interface.
func (obj *FileStream) ReadRune() (r rune, size int, err error) {
return RuneFromReader(obj)
}
// ReadByte reads a byte.
func (obj *FileStream) ReadByte() (b byte, err error) {
return ByteFromReader(obj)
}
// UnreadRune calls UnreadRune on the input if not closed.
func (obj *FileStream) UnreadRune() error {
panic(StreamErrorNew(NewScope(), 0, obj, "not supported"))
}