1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-19 00:20:25 +02:00

Fixed Console.Readline, added Console.KeyAvailable

This commit is contained in:
Brian Fiete 2023-04-17 15:16:19 -07:00
parent e71cb928a6
commit 037cb39240
5 changed files with 92 additions and 10 deletions

View file

@ -50,6 +50,7 @@ namespace System.IO
private int32 mMaxCharsPerBuffer;
private Encoding mEncoding;
private bool mPendingNewlineCheck;
public Stream BaseStream
{
@ -167,6 +168,18 @@ namespace System.IO
}
}
public bool CanReadNow
{
get
{
if (mCharPos < mCharLen)
return true;
if (ReadBuffer(true) case .Ok(let count))
return count > 0;
return false;
}
}
public Result<void, FileOpenError> Open(StringView fileName)
{
Contract.Assert(mStream == null);
@ -393,7 +406,7 @@ namespace System.IO
}
}
protected virtual Result<int> ReadBuffer()
protected virtual Result<int> ReadBuffer(bool zeroWait = false)
{
mCharLen = 0;
mCharPos = 0;
@ -405,7 +418,7 @@ namespace System.IO
if (mCheckPreamble)
{
//Contract.Assert(bytePos <= _preamble.Length, "possible bug in _compressPreamble. Are two threads using this StreamReader at the same time?");
int len = Try!(mStream.TryRead(.(mByteBuffer, mBytePos, mByteBuffer.Count - mBytePos)));
int len = Try!(mStream.TryRead(.(mByteBuffer, mBytePos, mByteBuffer.Count - mBytePos), zeroWait ? 0 : -1));
/*switch (mStream.Read(mByteBuffer, mBytePos, mByteBuffer.Length - mBytePos))
{
case .Ok(var gotLen):
@ -437,7 +450,7 @@ namespace System.IO
{
//Contract.Assert(bytePos == 0, "bytePos can be non zero only when we are trying to _checkPreamble. Are two threads using this StreamReader at the same time?");
mByteLen = Try!(mStream.TryRead(.(mByteBuffer, 0, mByteBuffer.Count)));
mByteLen = Try!(mStream.TryRead(.(mByteBuffer, 0, mByteBuffer.Count), zeroWait ? 0 : -1));
/*switch (mStream.Read(mByteBuffer, 0, mByteBuffer.Length))
{
case .Ok(var byteLen):
@ -488,9 +501,13 @@ namespace System.IO
}
}
if ((mPendingNewlineCheck) && (mCharPos < mCharLen))
{
if (mCharBuffer[mCharPos] == '\n') mCharPos++;
mPendingNewlineCheck = false;
}
}
while (mCharLen == 0);
while (mCharLen == mCharPos);
//Console.WriteLine("ReadBuffer called. chars: "+char8Len);
return mCharLen;
@ -551,10 +568,18 @@ namespace System.IO
strBuffer.Append(mCharBuffer.CArray() + mCharPos, i - mCharPos);
mCharPos = i + 1;
if (ch == '\r' && (mCharPos < mCharLen || Try!(ReadBuffer()) > 0))
if (ch == '\r')
{
if (mCharBuffer[mCharPos] == '\n') mCharPos++;
if (mCharPos < mCharLen)
{
if (mCharBuffer[mCharPos] == '\n') mCharPos++;
}
else
{
mPendingNewlineCheck = true;
}
}
return .Ok;
}
i++;