mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-16 09:59:17 +02:00
* Add GetFileContents and GetFileAsString
git-svn-id: trunk@47055 -
This commit is contained in:
parent
549bc49c4a
commit
d5cbe6809b
@ -670,3 +670,77 @@ begin
|
|||||||
Result:= False;
|
Result:= False;
|
||||||
end;
|
end;
|
||||||
{$endif}
|
{$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) : Int64;
|
|||||||
Function FileSetDate (Handle : THandle;Age : Int64) : Longint;
|
Function FileSetDate (Handle : THandle;Age : Int64) : Longint;
|
||||||
Function GetFileHandle(var f : File):THandle;
|
Function GetFileHandle(var f : File):THandle;
|
||||||
Function GetFileHandle(var f : Text):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