about summary refs log tree commit diff stats
path: root/src/io/filestream.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-24 14:12:27 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-24 14:21:01 +0100
commitfb21b1e4f0ee0e55e9556bf1f399d00d5eae26e4 (patch)
treec532aa98ee6bedf19b840f4ea0c7360a42407fbf /src/io/filestream.nim
parentb27deb7672c53e3ee59f91b7091e83ab28a8318d (diff)
downloadchawan-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.nim34
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