-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCLngStrArr.pas
219 lines (189 loc) · 7.46 KB
/
CLngStrArr.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
unit CLngStrArr;
{
DESCRIPTION: Working with arrays of language strings
AUTHOR: Alexander Shostak (aka Berserker aka EtherniDee aka BerSoft)
}
(***) interface (***)
uses SysUtils, Math, Utils, StrLib, CLang, CBinString;
const
LNGSTRARR_SIGNATURE = 'LAR';
type
TLangName = packed record
Name: array [0..CLang.LANGNAME_LEN - 1] of AnsiChar;
_Align4: array [1..(4 - CLang.LANGNAME_LEN mod 4)] of byte;
end; // .record TLangName
PLngStrArrExtHeader = ^TLngStrArrExtHeader;
TLngStrArrExtHeader = packed record
NumBinStrings: integer;
LangName: TLangName; // !Assert LangName is unique in parent structure
Unicode: LONGBOOL; // !Assert Unicode = Parent.Unicode
end; // .record TLngStrArrExtHeader
PLngStrArr = ^TLngStrArr;
TLngStrArr = packed record (* FORMAT *)
Header: CLang.TLngStructHeader;
ExtHeader: TLngStrArrExtHeader;
(*
BinStrings: array ExtHeader.NumBinStrings of TBinString;
*)
BinStrings: Utils.TEmptyRec;
end; // .record TLngStrArr
TLngStrArrReader = class
(***) protected (***)
fConnected: boolean;
(* Un *) fLngStrArr: PLngStrArr;
fStructMemoryBlockSize: integer;
fCurrBinStringInd: integer;
(* Un *) fCurrBinString: CBinString.PBinString;
function GetUnicode: boolean;
function GetLangName: string;
function GetStructSize: integer;
function GetNumBinStrings: integer;
(***) public (***)
procedure Connect (LngStrArr: PLngStrArr; StructMemoryBlockSize: integer);
procedure Disconnect;
function Validate (out Error: string): boolean;
function SeekBinString (SeekBinStringInd: integer): boolean;
function ReadBinString ((* n *) var BinStringReader: CBinString.TBinStringReader): boolean;
constructor Create;
property Connected: boolean read fConnected;
property LngStrArr: PLngStrArr read fLngStrArr;
property StructMemoryBlockSize: integer read fStructMemoryBlockSize;
property LangName: string read GetLangName;
property StructSize: integer read GetStructSize;
property NumBinStrings: integer read GetNumBinStrings;
property Unicode: boolean read GetUnicode;
end; // .class TLngStrArrReader
(***) implementation (***)
constructor TLngStrArrReader.Create;
begin
Self.fConnected := FALSE;
end; // .constructor TLngStrArrReader.Create
procedure TLngStrArrReader.Connect (LngStrArr: PLngStrArr; StructMemoryBlockSize: integer);
begin
{!} Assert((LngStrArr <> nil) or (StructMemoryBlockSize = 0));
{!} Assert(StructMemoryBlockSize >= 0);
Self.fConnected := TRUE;
Self.fLngStrArr := LngStrArr;
Self.fStructMemoryBlockSize := StructMemoryBlockSize;
Self.fCurrBinStringInd := 0;
Self.fCurrBinString := nil;
end;
procedure TLngStrArrReader.Disconnect;
begin
Self.fConnected := FALSE;
end;
function TLngStrArrReader.Validate (out Error: string): boolean;
var
NumBinStrings: integer;
Unicode: boolean;
RealStructSize: integer;
(* U *) BinString: CBinString.PBinString;
(* O *) BinStringReader: CBinString.TBinStringReader;
i: integer;
function ValidateNumBinStringsField: boolean;
begin
NumBinStrings := Self.NumBinStrings;
result := (NumBinStrings >= 0) and ((NumBinStrings * sizeof(TBinString) + sizeof(TLngStrArr)) <= Self.StructMemoryBlockSize);
if not result then begin
Error := 'Invalid NumBinStrings field: ' + SysUtils.IntToStr(NumBinStrings);
end;
end;
function ValidateLangNameField: boolean;
var
LangName: string;
begin
LangName := Self.LangName;
result := CLang.IsValidLangName(LangName);
if not result then begin
Error := 'Invalid LangName field: ' + LangName;
end;
end; // .function ValidateLangNameField
begin
{!} Assert(Self.Connected);
{!} Assert(Error = '');
RealStructSize := -1;
BinString := nil;
BinStringReader := CBinString.TBinStringReader.Create;
result := CLang.ValidateLngStructHeader(@Self.LngStrArr.Header, Self.StructMemoryBlockSize, sizeof(TLngStrArr), LNGSTRARR_SIGNATURE, Error);
// * * * * * //
result := result and
ValidateNumBinStringsField and
ValidateLangNameField;
if result then begin
Unicode := Self.Unicode;
RealStructSize := sizeof(TLngStrArr);
if NumBinStrings > 0 then begin
i := 0;
BinString := @Self.LngStrArr.BinStrings;
while result and (i < NumBinStrings) do begin
BinStringReader.Connect(BinString, Self.StructMemoryBlockSize - RealStructSize, Unicode);
result := BinStringReader.Validate(Error);
if result then begin
RealStructSize := RealStructSize + BinStringReader.StructSize;
Inc(integer(BinString), BinStringReader.StructSize);
end;
Inc(i);
end;
end; // .if
end; // .if
result := result and CLang.ValidateStructSize(Self.LngStrArr.Header.StructSize, RealStructSize, Error);
// * * * * * //
SysUtils.FreeAndNil(BinStringReader);
end; // .function TLngStrArrReader.Validate
function TLngStrArrReader.GetUnicode: boolean;
begin
{!} Assert(Self.Connected);
result := Self.LngStrArr.ExtHeader.Unicode;
end;
function TLngStrArrReader.GetLangName: string;
begin
{!} Assert(Self.Connected);
result := StrLib.BytesToAnsiString(@Self.LngStrArr.ExtHeader.LangName.Name[0], CLang.LANGNAME_LEN);
end;
function TLngStrArrReader.GetStructSize: integer;
begin
{!} Assert(Self.Connected);
result := Self.LngStrArr.Header.StructSize;
end;
function TLngStrArrReader.GetNumBinStrings: integer;
begin
{!} Assert(Self.Connected);
result := Self.LngStrArr.ExtHeader.NumBinStrings;
end;
function TLngStrArrReader.SeekBinString (SeekBinStringInd: integer): boolean;
var
(* on *) BinStringReader: CBinString.TBinStringReader;
begin
{!} Assert(Self.Connected);
{!} Assert(SeekBinStringInd >= 0);
BinStringReader := nil;
// * * * * * //
result := SeekBinStringInd < Self.NumBinStrings;
if result then begin
if Self.fCurrBinStringInd > SeekBinStringInd then begin
Self.fCurrBinStringInd := 0;
end;
while Self.fCurrBinStringInd < SeekBinStringInd do begin
Self.ReadBinString(BinStringReader);
end;
end;
// * * * * * //
SysUtils.FreeAndNil(BinStringReader);
end; // .function TLngStrArrReader.SeekBinString
function TLngStrArrReader.ReadBinString ((* n *) var BinStringReader: CBinString.TBinStringReader): boolean;
begin
{!} Assert(Self.Connected);
result := Self.fCurrBinStringInd < Self.NumBinStrings;
if result then begin
if BinStringReader = nil then begin
BinStringReader := CBinString.TBinStringReader.Create;
end;
if Self.fCurrBinStringInd = 0 then begin
Self.fCurrBinString := @Self.LngStrArr.BinStrings;
end;
BinStringReader.Connect(Self.fCurrBinString, Self.StructMemoryBlockSize - (integer(Self.fCurrBinString) - integer(Self.LngStrArr)), Self.Unicode);
Inc(integer(Self.fCurrBinString), BinStringReader.StructSize);
Inc(Self.fCurrBinStringInd);
end; // .if
end; // .function TLngStrArrReader.ReadBinString
end.