From: Brendan Hansen Date: Fri, 22 Jan 2021 22:07:22 +0000 (-0600) Subject: Added binary stream reader and writer X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=a672d390c766948640076ea805cce248c0705ac9;p=onyx.git Added binary stream reader and writer --- diff --git a/core/io/binary.onyx b/core/io/binary.onyx new file mode 100644 index 00000000..dbdd9925 --- /dev/null +++ b/core/io/binary.onyx @@ -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 diff --git a/core/io/stream.onyx b/core/io/stream.onyx index 0faed374..480f7a0d 100644 --- a/core/io/stream.onyx +++ b/core/io/stream.onyx @@ -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 { diff --git a/core/std/wasi.onyx b/core/std/wasi.onyx index 7e5167c1..5b0db8ba 100644 --- a/core/std/wasi.onyx +++ b/core/std/wasi.onyx @@ -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"