-
Notifications
You must be signed in to change notification settings - Fork 4
/
appendutils.pas
138 lines (120 loc) · 2.94 KB
/
appendutils.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
unit AppendUtils;
{$mode delphi}{$H+}
interface
uses
Classes, Windows, SysUtils;
type
TBytes = Array of Byte;
function SafeReadFile(FileName: String): AnsiString;
function ReadEOF(FileName, Delimit1, Delimit2: String): String;
function WriteEOF(FileName, Res, Delimit1, Delimit2: String): Boolean;
function AlterResource(AExeFile, AResName: string; const AResContent: TBytes): Boolean;
function WriteADS(FileName, StreamName, Data: String): Boolean;
function ReadADS(FileName, StreamName: String): String;
function StringToBytes(Str: String): TBytes;
implementation
function SafeReadFile(FileName: String): AnsiString;
var
F :File;
Buffer :AnsiString;
Size :Integer;
ReadBytes :Integer;
DefaultFileMode:Byte;
begin
Result := '';
DefaultFileMode := FileMode;
FileMode := 0;
AssignFile(F, FileName);
Reset(F, 1);
if (IOResult = 0) then
begin
Size := FileSize(F);
while (Size > 1024) do
begin
SetLength(Buffer, 1024);
BlockRead(F, Buffer[1], 1024, ReadBytes);
Result := Result + Buffer;
Dec(Size, ReadBytes);
end;
SetLength(Buffer, Size);
BlockRead(F, Buffer[1], Size);
Result := Result + Buffer;
CloseFile(F);
end;
FileMode := DefaultFileMode;
end;
function ReadEOF(FileName, Delimit1, Delimit2: String): String;
var
Buffer :AnsiString;
begin
Buffer := SafeReadFile(FileName);
Delete(Buffer, 1, Pos(Delimit1, Buffer)+Length(Delimit1) - 1);
Result:=LeftStr(Buffer, Pos(Delimit2, Buffer) - 1);
end;
function WriteEOF(FileName, Res, Delimit1, Delimit2: String): Boolean;
var
F: TextFile;
begin
Result:=True;
try
AssignFile(F,FileName);
Append(F);
Writeln(F,Delimit1+Res+Delimit2);
CloseFile(F);
except
Result:=False;
end;
end;
function StringToBytes(Str: String): TBytes;
Begin
Result:=TEncoding.UTF8.GetBytes(Str);
end;
function AlterResource(AExeFile, AResName: string; const AResContent: TBytes): Boolean;
var
vResHandle: THandle;
begin
Result:=True;
AExeFile := ExpandFileName(AExeFile);
vResHandle := BeginUpdateResource(PChar(AExeFile), False);
if vResHandle=0 then Result:=False else Begin
try
Result:=UpdateResource(vResHandle, RT_RCDATA,
PChar(UpperCase(AResName)), LANG_NEUTRAL, AResContent, Length(AResContent));
finally
EndUpdateResource(vResHandle, False);
end;
end;
end;
function WriteADS(FileName, StreamName, Data: String): Boolean;
var F: Text;
Begin
Result:=True;
try
AssignFile(F,FileName+':'+StreamName);
Rewrite(F);
Write(F, Data);
CloseFile(F);
except
Result:=False;
end;
end;
function ReadADS(FileName, StreamName: String): String;
var F: Text;
Full, P: String;
Begin
Full:='';
try
AssignFile(F,FileName+':'+StreamName);
Reset(F);
While Not(EOF(F)) do Begin
Readln(F, P);
Full+=P+sLineBreak;
end;
//You should remove the last LineBreak
CloseFile(F);
except
Full:='';
end;
Result:=Full;
end;
end.