-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.Bytes.pas
105 lines (78 loc) · 2.12 KB
/
JPL.Bytes.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
unit JPL.Bytes;
{
Jacek Pazera
https://www.pazera-software.com
https://github.com/jackdp
}
{$I .\..\jp.inc}
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
interface
uses
{$IFDEF MSWINDOWS}Windows,{$ENDIF}
SysUtils;
function GetFileContentAsBytes(const FileName: string; var Bytes: TBytes): Boolean;
{$IFDEF DCC}
function FileTruncate(Handle: THandle; Size: Int64): Boolean;
{$ENDIF}
function SaveBytesToFile(const FileName: string; const Bytes: TBytes): Boolean;
procedure ConcatTBytes(const Bytes1, Bytes2: TBytes; var OutBytes: TBytes);
implementation
function GetFileContentAsBytes(const FileName: string; var Bytes: TBytes): Boolean;
var
hFile: THandle;
Buffer: array[0..511] of Byte;
xRead, BufNo, BufLen: integer;
begin
Result := False;
SetLength(Bytes, 0);
if not FileExists(FileName) then Exit;
hFile := FileOpen(FileName, fmOpenRead);
if hFile = THandle(-1) then Exit;
try
BufLen := Length(Buffer);
BufNo := 0;
while True do
begin
xRead := FileRead(hFile, Buffer, BufLen);
if xRead <= 0 then Break;
SetLength(Bytes, Length(Bytes) + xRead);
Move(Buffer[0], Bytes[BufNo * BufLen], xRead);
Inc(BufNo);
end;
finally
FileClose(hFile);
end;
Result := True;
end;
{$IFDEF DCC}
function FileTruncate(Handle: THandle; Size: Int64): Boolean;
begin
if FileSeek(Handle, Size, FILE_BEGIN) = Size then Result := SetEndOfFile(Handle)
else Result := False;
end;
{$ENDIF}
function SaveBytesToFile(const FileName: string; const Bytes: TBytes): Boolean;
var
hFile: THandle;
begin
Result := False;
if not FileExists(FileName) then hFile := FileCreate(FileName)
else hFile := FileOpen(FileName, fmOpenWrite);
if hFile = THandle(-1) then Exit;
try
if not FileTruncate(hFile, 0) then Exit;
FileWrite(hFile, Bytes[0], Length(Bytes));
finally
FileClose(hFile);
end;
Result := True;
end;
procedure ConcatTBytes(const Bytes1, Bytes2: TBytes; var OutBytes: TBytes);
begin
SetLength(OutBytes, Length(Bytes1) + Length(Bytes2));
Move(Bytes1[0], OutBytes[0], Length(Bytes1));
Move(Bytes2[0], OutBytes[Length(Bytes1)], Length(Bytes2));
end;
end.