Added binary stream reader and writer
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 22 Jan 2021 22:07:22 +0000 (16:07 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 22 Jan 2021 22:07:22 +0000 (16:07 -0600)
core/io/binary.onyx [new file with mode: 0644]
core/io/stream.onyx
core/std/wasi.onyx

diff --git a/core/io/binary.onyx b/core/io/binary.onyx
new file mode 100644 (file)
index 0000000..dbdd992
--- /dev/null
@@ -0,0 +1,137 @@
+package core.io
+
+BinaryWriter :: struct {
+    stream: ^Stream;
+}
+
+binary_writer_make :: (s: ^Stream) -> BinaryWriter {
+    assert(s.vtable != null, "Stream vtable was not setup correctly.");
+
+    return BinaryWriter.{ s };
+}
+
+binary_write_byte :: (use bw: ^BinaryWriter, byte: u8) {
+    stream_write_byte(stream, byte);
+}
+
+binary_write_i16 :: (use bw: ^BinaryWriter, i: u16) {
+    v := i; 
+    bytes := *cast(^[2] u8) ^v;
+
+    stream_write(stream, ~~ bytes);
+}
+
+binary_write_i32 :: (use bw: ^BinaryWriter, i: u32) {
+    v := i; 
+    bytes := *cast(^[4] u8) ^v;
+
+    stream_write(stream, ~~ bytes);
+}
+
+binary_write_i64 :: (use bw: ^BinaryWriter, i: u64) {
+    v := i; 
+    bytes := *cast(^[8] u8) ^v;
+
+    stream_write(stream, ~~ bytes);
+}
+
+binary_write_f32 :: (use bw: ^BinaryWriter, f: f32) {
+    v := f; 
+    bytes := *cast(^[4] u8) ^v;
+
+    stream_write(stream, ~~ bytes);
+}
+
+binary_write_f64 :: (use bw: ^BinaryWriter, f: f64) {
+    v := f; 
+    bytes := *cast(^[8] u8) ^v;
+
+    stream_write(stream, ~~ bytes);
+}
+
+binary_write_slice :: (use bw: ^BinaryWriter, sl: [] $T, output_size := false) {
+    if output_size do binary_write_i32(bw, sl.count);
+
+    bytes := <[] u8>.{
+        data = cast(^u8) ^sl.data[0],
+        count = sizeof T * sl.count,
+    };
+
+    stream_write(stream, bytes);
+}
+
+
+
+
+
+BinaryReader :: struct {
+    stream: ^Stream;
+}
+
+binary_reader_make :: (s: ^Stream) -> BinaryReader {
+    assert(s.vtable != null, "Stream vtable was not setup correctly.");
+
+    return BinaryReader.{ s };
+}
+
+binary_read_byte :: (use br: ^BinaryReader) -> u8 {
+    _, byte := stream_read_byte(stream);
+    return byte;
+}
+
+binary_read_i16 :: (use br: ^BinaryReader) -> u16 {
+    buf: [2] u8;
+    _, bytes_read := stream_read(stream, ~~ buf);
+
+    return *(cast(^u16) buf);
+}
+
+binary_read_i32 :: (use br: ^BinaryReader) -> u32 {
+    buf: [4] u8;
+    _, bytes_read := stream_read(stream, ~~ buf);
+
+    return *(cast(^u32) buf);
+}
+
+binary_read_i64 :: (use br: ^BinaryReader) -> u64 {
+    buf: [8] u8;
+    _, bytes_read := stream_read(stream, ~~ buf);
+
+    return *(cast(^u64) buf);
+}
+
+binary_read_f32 :: (use br: ^BinaryReader) -> f32 {
+    buf: [4] u8;
+    _, bytes_read := stream_read(stream, ~~ buf);
+
+    return *(cast(^f32) buf);
+}
+
+binary_read_f64 :: (use br: ^BinaryReader) -> f64 {
+    buf: [8] u8;
+    _, bytes_read := stream_read(stream, ~~ buf);
+
+    return *(cast(^f64) buf);
+}
+
+binary_read_slice :: (use br: ^BinaryReader,
+    $T: type_expr,
+    size := 0, read_size := false,
+    allocator := context.allocator) -> [] T {
+    if size == 0 && read_size {
+        size = binary_read_i32(br);
+    }
+
+    sl := memory.make_slice(u8, size * sizeof T);
+    _, bytes_read := stream_read(stream, sl);
+
+    return <[] T>.{ data = cast(^T) sl.data, count = size };
+}
+
+// This should be possible
+// binary_read :: (use br: ^BinaryReader, $T: type_expr) -> T {
+//     buf: [sizeof T] u8;
+//     _, bytes_read := stream_read(stream, ~~ buf);
+// 
+//     return *(cast(^T) buf);
+// }
\ No newline at end of file
index 0faed374d05ab20d312ab23858bd54d0b775851e..480f7a0d4b7d9f0a0c899ef5352c75ae9b1d324d 100644 (file)
@@ -8,22 +8,22 @@ Stream :: struct {
 
 #private
 Stream_Vtable :: struct {
-    seek         : proc (s: ^Stream, to: i32, whence: SeekFrom) -> Error;
-    tell         : proc (s: ^Stream) -> (Error, u32);
+    seek         : (s: ^Stream, to: i32, whence: SeekFrom) -> Error;
+    tell         : (s: ^Stream) -> (Error, u32);
 
-    read         : proc (s: ^Stream, buffer: [] u8) -> (Error, u32);
-    read_at      : proc (s: ^Stream, at: u32, buffer: [] u8) -> (Error, u32);
-    read_byte    : proc (s: ^Stream) -> (Error, u8);
-    unread_byte  : proc (s: ^Stream) -> Error;
+    read         : (s: ^Stream, buffer: [] u8) -> (Error, u32);
+    read_at      : (s: ^Stream, at: u32, buffer: [] u8) -> (Error, u32);
+    read_byte    : (s: ^Stream) -> (Error, u8);
+    unread_byte  : (s: ^Stream) -> Error;
 
-    write        : proc (s: ^Stream, buffer: [] u8) -> (Error, u32);
-    write_at     : proc (s: ^Stream, at: u32, buffer: [] u8) -> (Error, u32);
-    write_byte   : proc (s: ^Stream, byte: u8) -> Error;
+    write        : (s: ^Stream, buffer: [] u8) -> (Error, u32);
+    write_at     : (s: ^Stream, at: u32, buffer: [] u8) -> (Error, u32);
+    write_byte   : (s: ^Stream, byte: u8) -> Error;
 
-    close        : proc (s: ^Stream) -> Error;
-    flush        : proc (s: ^Stream) -> Error;
+    close        : (s: ^Stream) -> Error;
+    flush        : (s: ^Stream) -> Error;
 
-    size         : proc (s: ^Stream) -> i32;
+    size         : (s: ^Stream) -> i32;
 }
 
 SeekFrom :: enum {
index 7e5167c13919b94c4e8308984965d24c91fae52e..5b0db8ba8efe2ab6f5bf0c1c7bac9c5931e0c26e 100644 (file)
@@ -21,6 +21,7 @@ package core
 #load "core/io/stream"
 #load "core/io/reader"
 #load "core/io/writer"
+#load "core/io/binary"
 
 #load "core/sys/wasi"