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);
|
return OpenStreamReader(.In, ref mIn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool KeyAvailable => In.CanReadNow;
|
||||||
|
|
||||||
public static Result<char8> Read() => In.Read();
|
public static Result<char8> Read() => In.Read();
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ namespace System.IO
|
||||||
return numBytesRead;
|
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;
|
Platform.BfpFileResult result = .Ok;
|
||||||
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);
|
int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);
|
||||||
|
@ -513,7 +513,7 @@ namespace System.IO
|
||||||
return numBytesRead;
|
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)
|
if (mBfpFilePos != mPos)
|
||||||
Try!(SeekUnderlying(mPos));
|
Try!(SeekUnderlying(mPos));
|
||||||
|
|
|
@ -60,6 +60,20 @@ namespace System.IO
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract Result<int> TryRead(Span<uint8> data);
|
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<int> TryWrite(Span<uint8> data);
|
||||||
public abstract Result<void> Close();
|
public abstract Result<void> Close();
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,7 @@ namespace System.IO
|
||||||
|
|
||||||
private int32 mMaxCharsPerBuffer;
|
private int32 mMaxCharsPerBuffer;
|
||||||
private Encoding mEncoding;
|
private Encoding mEncoding;
|
||||||
|
private bool mPendingNewlineCheck;
|
||||||
|
|
||||||
public Stream BaseStream
|
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)
|
public Result<void, FileOpenError> Open(StringView fileName)
|
||||||
{
|
{
|
||||||
Contract.Assert(mStream == null);
|
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;
|
mCharLen = 0;
|
||||||
mCharPos = 0;
|
mCharPos = 0;
|
||||||
|
@ -405,7 +418,7 @@ namespace System.IO
|
||||||
if (mCheckPreamble)
|
if (mCheckPreamble)
|
||||||
{
|
{
|
||||||
//Contract.Assert(bytePos <= _preamble.Length, "possible bug in _compressPreamble. Are two threads using this StreamReader at the same time?");
|
//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))
|
/*switch (mStream.Read(mByteBuffer, mBytePos, mByteBuffer.Length - mBytePos))
|
||||||
{
|
{
|
||||||
case .Ok(var gotLen):
|
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?");
|
//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))
|
/*switch (mStream.Read(mByteBuffer, 0, mByteBuffer.Length))
|
||||||
{
|
{
|
||||||
case .Ok(var byteLen):
|
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);
|
//Console.WriteLine("ReadBuffer called. chars: "+char8Len);
|
||||||
return mCharLen;
|
return mCharLen;
|
||||||
|
@ -551,10 +568,18 @@ namespace System.IO
|
||||||
strBuffer.Append(mCharBuffer.CArray() + mCharPos, i - mCharPos);
|
strBuffer.Append(mCharBuffer.CArray() + mCharPos, i - mCharPos);
|
||||||
|
|
||||||
mCharPos = i + 1;
|
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;
|
return .Ok;
|
||||||
}
|
}
|
||||||
i++;
|
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)
|
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)
|
if (file->mAsyncData == NULL)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue