From 7cc95c2b0729ae385552e8580a3c8b4436b506c6 Mon Sep 17 00:00:00 2001 From: moneyl <8206401+Moneyl@users.noreply.github.com> Date: Tue, 16 Mar 2021 19:06:01 -0400 Subject: [PATCH 1/3] Add functions to System.IO.Stream --- BeefLibs/corlib/src/IO/Stream.bf | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/BeefLibs/corlib/src/IO/Stream.bf b/BeefLibs/corlib/src/IO/Stream.bf index dd989abf..f70f49ad 100644 --- a/BeefLibs/corlib/src/IO/Stream.bf +++ b/BeefLibs/corlib/src/IO/Stream.bf @@ -63,6 +63,58 @@ namespace System.IO public abstract Result TryWrite(Span data); public abstract void Close(); + //Read value from stream without changing position. Position won't change even if it returns .Err + public Result Peek() where T : struct + { + T val = ?; + int size = Try!(TryRead(.((uint8*)&val, sizeof(T)))); + Seek(Position - size, .Absolute); + if (size != sizeof(T)) + return .Err; + + return .Ok(val); + } + + //Skip count bytes + public void Skip(int64 count) + { + Seek(Position + count, .Absolute); + } + + //Write count null bytes to stream + public void WriteNullBytes(int64 count) + { + if(count <= 0) + return; + + int64 nullBytesRemaining = count; + int64 emptyData = 0; + while (nullBytesRemaining > 0) + { + int64 writeSize = Math.Min(nullBytesRemaining, sizeof(decltype(emptyData))); + TryWrite(.((uint8*)&emptyData, (int)writeSize)); + nullBytesRemaining -= writeSize; + } + } + + //Read sized string from stream + public Result ReadSizedString(int64 size, String output) + { + if (size <= 0) + return .Err; + + for (int64 i = 0; i < size; i++) + { + Result char = Read(); + if (char == .Err) + return .Err; + + output.Append(char); + } + + return .Ok; + } + public Result Read() where T : struct { T val = ?; From a1e15ecb73695ec0615b5d3be641cd69c1ad812c Mon Sep 17 00:00:00 2001 From: moneyl <8206401+Moneyl@users.noreply.github.com> Date: Tue, 16 Mar 2021 19:39:38 -0400 Subject: [PATCH 2/3] Add ReadNullTerminatedString to System.IO.Stream Function that reads a null terminated string from the stream. --- BeefLibs/corlib/src/IO/Stream.bf | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/BeefLibs/corlib/src/IO/Stream.bf b/BeefLibs/corlib/src/IO/Stream.bf index f70f49ad..8b3c3d0f 100644 --- a/BeefLibs/corlib/src/IO/Stream.bf +++ b/BeefLibs/corlib/src/IO/Stream.bf @@ -115,6 +115,22 @@ namespace System.IO return .Ok; } + //Reads null terminated ASCII string from the stream. Null terminator is read from stream but isn't appended to output string + public Result ReadNullTerminatedString(String output) + { + Result char0; + while(true) + { + char0 = Read(); + if(char0 == .Err) + return .Err; + if(char0.Value == '\0') + return .Ok; + + output.Append(char0.Value); + } + } + public Result Read() where T : struct { T val = ?; From b1dc76ba116eef6ac09d8111d17e53faec05b067 Mon Sep 17 00:00:00 2001 From: moneyl <8206401+Moneyl@users.noreply.github.com> Date: Wed, 17 Mar 2021 10:32:01 -0400 Subject: [PATCH 3/3] PR feedback changes --- BeefLibs/corlib/src/IO/Stream.bf | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/BeefLibs/corlib/src/IO/Stream.bf b/BeefLibs/corlib/src/IO/Stream.bf index 8b3c3d0f..b0c73878 100644 --- a/BeefLibs/corlib/src/IO/Stream.bf +++ b/BeefLibs/corlib/src/IO/Stream.bf @@ -81,24 +81,24 @@ namespace System.IO Seek(Position + count, .Absolute); } - //Write count null bytes to stream - public void WriteNullBytes(int64 count) + //Write count bytes to stream + public void Write(uint8 byte, int64 count) { if(count <= 0) return; int64 nullBytesRemaining = count; - int64 emptyData = 0; + uint8[8] writeData = .(byte, byte, byte, byte, byte, byte, byte, byte); while (nullBytesRemaining > 0) { - int64 writeSize = Math.Min(nullBytesRemaining, sizeof(decltype(emptyData))); - TryWrite(.((uint8*)&emptyData, (int)writeSize)); + int64 writeSize = Math.Min(nullBytesRemaining, writeData.Count * sizeof(uint8)); + TryWrite(.(&writeData[0], (int)writeSize)); nullBytesRemaining -= writeSize; } } //Read sized string from stream - public Result ReadSizedString(int64 size, String output) + public Result ReadStrSized32(int64 size, String output) { if (size <= 0) return .Err; @@ -116,7 +116,7 @@ namespace System.IO } //Reads null terminated ASCII string from the stream. Null terminator is read from stream but isn't appended to output string - public Result ReadNullTerminatedString(String output) + public Result ReadStrC(String output) { Result char0; while(true)