Browse Source

Add SeekUnderlying to FileStream

disarray2077 4 years ago
parent
commit
4bd3cc641d
1 changed files with 30 additions and 34 deletions
  1. 30 34
      BeefLibs/corlib/src/IO/FileStream.bf

+ 30 - 34
BeefLibs/corlib/src/IO/FileStream.bf

@@ -253,6 +253,22 @@ namespace System.IO
 			}
 		}
 
+		public override bool CanRead
+		{
+			get
+			{
+				return mFileAccess.HasFlag(FileAccess.Read);
+			}
+		}
+
+		public override bool CanWrite
+		{
+			get
+			{
+				return mFileAccess.HasFlag(FileAccess.Write);
+			}
+		}
+
 		public this()
 		{
 			
@@ -274,22 +290,6 @@ namespace System.IO
 			mFileAccess = access;
 		}
 
-		public override bool CanRead
-		{
-			get
-			{
-				return mFileAccess.HasFlag(FileAccess.Read);
-			}
-		}
-
-		public override bool CanWrite
-		{
-			get
-			{
-				return mFileAccess.HasFlag(FileAccess.Write);
-			}
-		}
-
 		public Result<void, FileOpenError> Create(StringView path, FileAccess access = .ReadWrite, FileShare share = .None, int bufferSize = 4096, FileOptions options = .None, SecurityAttributes* secAttrs = null)
 		{
 			return Open(path, FileMode.Create, access, share, bufferSize, options, secAttrs);
@@ -406,15 +406,20 @@ namespace System.IO
 			mUnderlyingLength = Platform.BfpFile_GetFileSize(mBfpFile);
 		}
 
+		protected Result<void> SeekUnderlying(int64 offset, Platform.BfpFileSeekKind seekKind = .Absolute)
+		{
+			int64 newPos = Platform.BfpFile_Seek(mBfpFile, offset, seekKind);
+			Result<void> result = ((seekKind == .Absolute) && (newPos != offset)) ? .Err : .Ok;
+			if (result case .Ok)
+				mBfpFilePos = newPos;
+			return result;
+		}
+
 		protected override Result<int> TryReadUnderlying(int64 pos, Span<uint8> data)
 		{
 			if (mBfpFilePos != pos)
-			{
-				int64 newPos = Platform.BfpFile_Seek(mBfpFile, pos, .Absolute);
-				if (newPos != pos)
-					return .Err;
-				mBfpFilePos = pos;
-			}
+				Try!(SeekUnderlying(pos));
+
 			Platform.BfpFileResult result = .Ok;
 			int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, -1, &result);
 			if ((result != .Ok) && (result != .PartialData))
@@ -426,12 +431,8 @@ namespace System.IO
 		protected override Result<int> TryWriteUnderlying(int64 pos, Span<uint8> data)
 		{
 			if (mBfpFilePos != pos)
-			{
-				int64 newPos = Platform.BfpFile_Seek(mBfpFile, pos, .Absolute);
-				if (newPos != pos)
-					return .Err;
-				mBfpFilePos = pos;
-			}
+				Try!(SeekUnderlying(pos));
+
 			Platform.BfpFileResult result = .Ok;
 			int numBytesRead = Platform.BfpFile_Write(mBfpFile, data.Ptr, data.Length, -1, &result);
 			if ((result != .Ok) && (result != .PartialData))
@@ -443,12 +444,7 @@ namespace System.IO
 		public Result<int> TryRead(Span<uint8> data, int timeoutMS)
 		{
 			if (mBfpFilePos != mPos)
-			{
-				int64 newPos = Platform.BfpFile_Seek(mBfpFile, mPos, .Absolute);
-				if (newPos != mPos)
-					return .Err;
-				mBfpFilePos = mPos;
-			}
+				Try!(SeekUnderlying(mPos));
 
 			Platform.BfpFileResult result = .Ok;
 			int numBytesRead = Platform.BfpFile_Read(mBfpFile, data.Ptr, data.Length, timeoutMS, &result);