mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-20 11:29:27 +02:00
--- Merging r47055 into '.':
U rtl/objpas/sysutils/filutil.inc U rtl/objpas/sysutils/filutilh.inc --- Recording mergeinfo for merge of r47055 into '.': U . # revisions: 47055 r47055 | michael | 2020-10-06 09:59:09 +0200 (Tue, 06 Oct 2020) | 1 line Changed paths: M /trunk/rtl/objpas/sysutils/filutil.inc M /trunk/rtl/objpas/sysutils/filutilh.inc * Add GetFileContents and GetFileAsString git-svn-id: branches/fixes_3_2@47582 -
This commit is contained in:
parent
a92e841636
commit
9d335225d8
@ -670,3 +670,77 @@ begin
|
||||
Result:= False;
|
||||
end;
|
||||
{$endif}
|
||||
|
||||
Function GetFileContents(Const aFileName : RawByteString) : TBytes;
|
||||
|
||||
Var
|
||||
H : Thandle;
|
||||
|
||||
begin
|
||||
H:=FileOpen(aFileName,fmOpenRead or fmShareDenyWrite);
|
||||
if H<0 then
|
||||
Raise EFileNotFoundException.Create(SFileNotFound);
|
||||
try
|
||||
Result:=GetFileContents(H);
|
||||
finally
|
||||
FileClose(H);
|
||||
end;
|
||||
end;
|
||||
|
||||
Function GetFileContents(Const aFileName : UnicodeString) : TBytes;
|
||||
|
||||
Var
|
||||
H : Thandle;
|
||||
|
||||
begin
|
||||
H:=FileOpen(aFileName,fmOpenRead or fmShareDenyWrite);
|
||||
if H<0 then
|
||||
Raise EFileNotFoundException.Create(SFileNotFound);
|
||||
try
|
||||
Result:=GetFileContents(H);
|
||||
finally
|
||||
FileClose(H);
|
||||
end;
|
||||
end;
|
||||
|
||||
Function GetFileContents(Const aHandle : THandle) : TBytes;
|
||||
|
||||
Var
|
||||
aLen,aOffset,aRead : Int64;
|
||||
aBuf : PByte;
|
||||
|
||||
begin
|
||||
aLen:=FileSeek(aHandle,0,fsFromEnd);
|
||||
FileSeek(aHandle,0,fsFromBeginning);
|
||||
SetLength(Result,aLen);
|
||||
aOffset:=0;
|
||||
Repeat
|
||||
aBuf:=@Result[aOffset];
|
||||
aRead:=FileRead(aHandle,aBuf^,aLen-aOffset);
|
||||
aOffset:=aOffset+aRead;
|
||||
Until (aOffset>=aLen) or (aRead<=0);
|
||||
if aRead<0 then
|
||||
RaiseLastOSError;
|
||||
end;
|
||||
|
||||
Function GetFileAsString(Const aFileName : RawByteString; aEncoding : TEncoding) : RawByteString;
|
||||
|
||||
begin
|
||||
Result:=aEncoding.GetAnsiString(GetFileContents(aFileName));
|
||||
end;
|
||||
|
||||
Function GetFileAsString(Const aFileName : RawByteString) : RawByteString;
|
||||
|
||||
begin
|
||||
Result:=GetFileAsString(aFileName,TEncoding.SystemEncoding);
|
||||
end;
|
||||
|
||||
Function GetFileAsString(Const aFileName : UnicodeString) : UnicodeString;
|
||||
begin
|
||||
Result:=GetFileAsString(aFileName, TEncoding.Unicode);
|
||||
end;
|
||||
|
||||
Function GetFileAsString(Const aFileName : UnicodeString; aEncoding : TEncoding) : UnicodeString;
|
||||
begin
|
||||
Result:=aEncoding.GetString(GetFileContents(aFileName))
|
||||
end;
|
||||
|
@ -229,3 +229,12 @@ Function FileGetDate (Handle : THandle) : Longint;
|
||||
Function FileSetDate (Handle : THandle;Age : Longint) : Longint;
|
||||
Function GetFileHandle(var f : File):THandle;
|
||||
Function GetFileHandle(var f : Text):THandle;
|
||||
|
||||
Function GetFileContents(Const aFileName : RawByteString) : TBytes;
|
||||
Function GetFileContents(Const aFileName : UnicodeString) : TBytes;
|
||||
Function GetFileContents(Const aHandle : THandle) : TBytes;
|
||||
Function GetFileAsString(Const aFileName : RawByteString) : RawByteString;
|
||||
Function GetFileAsString(Const aFileName : RawByteString; aEncoding : TEncoding) : RawByteString;
|
||||
Function GetFileAsString(Const aFileName : UnicodeString) : UnicodeString;
|
||||
Function GetFileAsString(Const aFileName : UnicodeString; aEncoding : TEncoding) : UnicodeString;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user