mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-08 19:48:20 +02:00
Fixed Console.Readline, added Console.KeyAvailable
This commit is contained in:
parent
e71cb928a6
commit
037cb39240
5 changed files with 92 additions and 10 deletions
|
@ -260,6 +260,8 @@ namespace System
|
|||
return OpenStreamReader(.In, ref mIn);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool KeyAvailable => In.CanReadNow;
|
||||
|
||||
public static Result<char8> Read() => In.Read();
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ namespace System.IO
|
|||
return numBytesRead;
|
||||
}
|
||||
|
||||
public virtual Result<int, FileError> TryRead(Span<uint8> data, int timeoutMS)
|
||||
public override Result<int, FileError> TryRead(Span<uint8> data, int timeoutMS)
|
||||
{
|
||||
Platform.BfpFileResult result = .Ok;
|
||||
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);
|
||||
|
@ -513,7 +513,7 @@ namespace System.IO
|
|||
return numBytesRead;
|
||||
}
|
||||
|
||||
public Result<int, FileError> TryRead(Span<uint8> data, int timeoutMS)
|
||||
public override Result<int, FileError> TryRead(Span<uint8> data, int timeoutMS)
|
||||
{
|
||||
if (mBfpFilePos != mPos)
|
||||
Try!(SeekUnderlying(mPos));
|
||||
|
|
|
@ -60,6 +60,20 @@ namespace System.IO
|
|||
}
|
||||
|
||||
public abstract Result<int> TryRead(Span<uint8> data);
|
||||
public virtual Result<int, FileError> TryRead(Span<uint8> data, int timeout)
|
||||
{
|
||||
if (timeout == -1)
|
||||
{
|
||||
switch (TryRead(data))
|
||||
{
|
||||
case .Ok(var i):
|
||||
return i;
|
||||
case .Err:
|
||||
return .Err(.ReadError(.Unknown));
|
||||
}
|
||||
}
|
||||
return .Err(.ReadError(.Unknown));
|
||||
}
|
||||
public abstract Result<int> TryWrite(Span<uint8> data);
|
||||
public abstract Result<void> Close();
|
||||
|
||||
|
|
|
@ -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++;
|
||||
|
|
|
@ -3043,7 +3043,48 @@ static void WINAPI OverlappedReadComplete(DWORD dwErrorCode, DWORD dwNumberOfByt
|
|||
|
||||
BFP_EXPORT intptr BFP_CALLTYPE BfpFile_Read(BfpFile* file, void* buffer, intptr size, int timeoutMS, BfpFileResult* outResult)
|
||||
{
|
||||
if (timeoutMS != -1)
|
||||
bool forceNormalRead = false;
|
||||
|
||||
if ((file->mIsStd) && (file->mHandle == GetStdHandle(STD_INPUT_HANDLE)))
|
||||
{
|
||||
INPUT_RECORD record;
|
||||
DWORD numRead;
|
||||
while (true)
|
||||
{
|
||||
if (timeoutMS != -1)
|
||||
{
|
||||
if (!GetNumberOfConsoleInputEvents(file->mHandle, &numRead))
|
||||
{
|
||||
forceNormalRead = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (numRead == 0)
|
||||
{
|
||||
OUTRESULT(BfpFileResult_Timeout);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ReadConsoleInput(file->mHandle, &record, 1, &numRead))
|
||||
{
|
||||
forceNormalRead = true;
|
||||
break;
|
||||
}
|
||||
if (numRead > 0)
|
||||
{
|
||||
if ((record.Event.KeyEvent.bKeyDown) && (record.Event.KeyEvent.uChar.AsciiChar != 0))
|
||||
{
|
||||
memset(buffer, record.Event.KeyEvent.uChar.AsciiChar, 1);
|
||||
OUTRESULT(BfpFileResult_Ok);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ((timeoutMS != -1) && (!forceNormalRead))
|
||||
{
|
||||
if (file->mAsyncData == NULL)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue