diff options
author | bptato <nincsnevem662@gmail.com> | 2024-03-24 14:12:27 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-03-24 14:21:01 +0100 |
commit | fb21b1e4f0ee0e55e9556bf1f399d00d5eae26e4 (patch) | |
tree | c532aa98ee6bedf19b840f4ea0c7360a42407fbf /src/io/filestream.nim | |
parent | b27deb7672c53e3ee59f91b7091e83ab28a8318d (diff) | |
download | chawan-fb21b1e4f0ee0e55e9556bf1f399d00d5eae26e4.tar.gz |
io: derive DynStream from RootObj (not Stream)
This way they are no longer compatible, but we no longer need them to be compatible anyway. (This also forces us to throw out the old serialize module, and use packet writers everywhere.)
Diffstat (limited to 'src/io/filestream.nim')
-rw-r--r-- | src/io/filestream.nim | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/io/filestream.nim b/src/io/filestream.nim new file mode 100644 index 00000000..b1b3a296 --- /dev/null +++ b/src/io/filestream.nim @@ -0,0 +1,34 @@ +import io/dynstream + +type + DynFileStream* = ref object of DynStream + file*: File + +method recvData*(s: DynFileStream; buffer: pointer; len: int): int = + let n = s.file.readBuffer(buffer, len) + if n == 0: + if unlikely(s.isend): + raise newException(EOFError, "eof") + s.isend = true + return n + +method sendData*(s: DynFileStream; buffer: pointer; len: int): int = + return s.file.writeBuffer(buffer, len) + +method seek*(s: DynFileStream; off: int) = + s.file.setFilePos(int64(off)) + +method sclose*(s: DynFileStream) = + s.file.close() + +method sflush*(s: DynFileStream) = + s.file.flushFile() + +proc newDynFileStream*(file: File): DynFileStream = + return DynFileStream(file: file, blocking: true) + +proc newDynFileStream*(path: string): DynFileStream = + var file: File + if file.open(path): + return newDynFileStream(path) + return nil |