source: oup/current/Code/Functions.pas@ 52

Last change on this file since 52 was 46, checked in by alloc, 18 years ago
File size: 8.5 KB
Line 
1unit Functions;
2
3interface
4
5uses Classes, Dialogs, SysUtils, StrUtils, Math, Data;
6
7type
8 TExportSet = set of (DO_dat, DO_raw, DO_convert, DO_toone);
9
10function BoolToStr(bool: Boolean): String;
11function HexToLong(hex: String): LongWord;
12function Decode_Int(buffer: Tdata): LongWord;
13function Encode_Int(input: LongWord): Tdata;
14function Decode_Float(buffer: Tdata): Single;
15function Encode_Float(input: Single): Tdata;
16function DataToBin(Data: Tdata): String;
17function BinToInt(bin: String): Byte;
18
19function ExportFile(fileid: LongWord; filename: String; settings: TExportSet;
20 path: String): Integer;
21
22function StringSmaller(string1, string2: String): Boolean;
23
24function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String;
25function FormatFileSize(size: LongWord): String;
26function CreateHexString(Data: Tdata; HexOnly: Boolean): String;
27function DecodeHexString(hex: String): Tdata;
28function GetWinFileName(Name: String): String;
29function GetExtractPath: String;
30
31function Explode(_string: String; delimiter: Char): TStringArray;
32
33
34implementation
35
36uses Exporters, OniDataClass;
37
38type
39 TValueSwitcher = record
40 case IsFloat: Boolean of
41 True: (ValueFloat: Single);
42 False: (ValueInt: LongWord);
43 end;
44
45
46
47
48function BoolToStr(bool: Boolean): String;
49begin
50 if bool then
51 Result := 'true'
52 else
53 Result := 'false';
54end;
55
56
57
58
59function HexToLong(hex: String): LongWord;
60
61
62
63
64 function NormalizeHexString(var hex: String): Boolean;
65 var
66 i: Byte;
67 begin
68 if hex[1] = '$' then
69 begin
70 for i := 1 to Length(hex) - 1 do
71 begin
72 hex[i] := hex[i + 1];
73 end;
74 SetLength(hex, Length(hex) - 1);
75 end;
76 if (hex[1] = '0') and (UpCase(hex[2]) = 'X') then
77 begin
78 for i := 1 to Length(hex) - 2 do
79 begin
80 hex[i] := hex[i + 2];
81 end;
82 SetLength(hex, Length(hex) - 2);
83 end;
84 if Length(hex) = 0 then
85 Result := False
86 else
87 Result := True;
88 end;
89
90var
91 i: Byte;
92begin
93 if NormalizeHexString(hex) then
94 begin
95 hex := UpperCase(hex);
96 Result := 0;
97 for i := 1 to Length(hex) do
98 begin
99 Result := Result shl 4;
100 case hex[i] of
101 '0'..'9':
102 Result := Result + Ord(hex[i]) - 48;
103 'A'..'F':
104 Result := Result + Ord(hex[i]) - 55;
105 else
106 Result := 0;
107 Exit;
108 end;
109 end;
110 end
111 else
112 begin
113 Result := 0;
114 end;
115end;
116
117
118
119
120function Decode_Int(buffer: Tdata): LongWord;
121begin
122 Result := buffer[0] + buffer[1] * 256 + buffer[2] * 256 * 256 + buffer[3] * 256 * 256 * 256;
123end;
124
125
126
127
128function Encode_Int(input: LongWord): Tdata;
129begin
130 SetLength(Result, 4);
131 Result[0] := input mod 256;
132 input := input div 256;
133 Result[1] := input mod 256;
134 input := input div 256;
135 Result[2] := input mod 256;
136 input := input div 256;
137 Result[3] := input mod 256;
138end;
139
140
141
142
143function Decode_Float(buffer: Tdata): Single;
144var
145 _valueswitcher: TValueSwitcher;
146begin
147 _valueswitcher.ValueInt := Decode_Int(buffer);
148 Result := _valueswitcher.ValueFloat;
149 if IsNAN(Result) then
150 Result := 0.0;
151end;
152
153
154
155
156function Encode_Float(input: Single): Tdata;
157var
158 _valueswitcher: TValueSwitcher;
159begin
160 _valueswitcher.ValueFloat := input;
161 Result := Encode_Int(_valueswitcher.ValueInt);
162end;
163
164
165
166
167function DataToBin(Data: Tdata): String;
168var
169 i, j: Byte;
170 singlebyte: Byte;
171 bytepart: String;
172begin
173 SetLength(bytepart, 8);
174 Result := '';
175 for i := 0 to High(Data) do
176 begin
177 singlebyte := Data[i];
178 for j := 7 downto 0 do
179 begin
180 bytepart[j + 1] := Char((singlebyte and $01) + 48);
181 singlebyte := singlebyte shr 1;
182 end;
183 Result := Result + bytepart + ' ';
184 end;
185end;
186
187
188
189
190function BinToInt(bin: String): Byte;
191var
192 Add: Integer;
193 i: Byte;
194begin
195 Result := 0;
196 if Length(bin) <> 8 then
197 Exit;
198 Add := 1;
199 for i := 8 downto 1 do
200 begin
201 if not (bin[i] in ['0', '1']) then
202 Exit;
203 if bin[i] = '1' then
204 Inc(Result, Add);
205 Add := Add shl 1;
206 end;
207end;
208
209
210
211
212function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String;
213begin
214 Result := AnsiReplaceStr(Format('%' + IntToStr(Width) + 'u', [Value]), ' ', leadingzeros);
215end;
216
217
218
219
220function FormatFileSize(size: LongWord): String;
221begin
222 if size >= 1000 * 1024 * 1024 then
223 begin
224 Result := FloatToStrF(size / 1024 / 1024 / 1024, ffFixed, 5, 1) + ' GB';
225 end
226 else
227 begin
228 if size >= 1000 * 1024 then
229 begin
230 Result := FloatToStrF(size / 1024 / 1024, ffFixed, 5, 1) + ' MB';
231 end
232 else
233 begin
234 if size >= 1000 then
235 begin
236 Result := FloatToStrF(size / 1024, ffFixed, 5, 1) + ' KB';
237 end
238 else
239 begin
240 Result := IntToStr(size) + ' B';
241 end;
242 end;
243 end;
244end;
245
246
247
248
249function CreateHexString(Data: Tdata; HexOnly: Boolean): String;
250var
251 string_build, ascii_version: String;
252 i: LongWord;
253begin
254 string_build := '';
255 ascii_version := '';
256 for i := 0 to High(Data) do
257 begin
258 if not HexOnly then
259 if (i mod 16) = 0 then
260 string_build := string_build + '0x' + IntToHex(i, 6) + ' ';
261 string_build := string_build + IntToHex(Data[i], 2);
262 if not HexOnly then
263 begin
264 if Data[i] >= 32 then
265 ascii_version := ascii_version + Chr(Data[i])
266 else
267 ascii_version := ascii_version + '.';
268 if ((i + 1) mod 2) = 0 then
269 string_build := string_build + #32;
270 if ((i + 1) mod 16) = 0 then
271 begin
272 string_build := string_build + #32 + ascii_version + CrLf;
273 ascii_version := '';
274 end;
275 end;
276 end;
277 Result := string_build;
278end;
279
280
281
282
283function DecodeHexString(hex: String): Tdata;
284var
285 i: LongWord;
286begin
287 SetLength(Result, Length(hex) div 2);
288 for i := 0 to Length(Result) do
289 begin
290 Result[i] := 0;
291 case UpCase(hex[1 + i * 2]) of
292 '0'..'9':
293 Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 48) * 16;
294 'A'..'F':
295 Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 55) * 16;
296 end;
297 case UpCase(hex[1 + i * 2 + 1]) of
298 '0'..'9':
299 Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 48);
300 'A'..'F':
301 Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 55);
302 end;
303 end;
304end;
305
306
307
308
309function StringSmaller(string1, string2: String): Boolean;
310var
311 i: Integer;
312 len: Integer;
313begin
314 len := Min(Length(string1), Length(string2));
315 for i := 1 to len do
316 if Ord(string1[i]) <> Ord(string2[i]) then
317 begin
318 Result := Ord(string1[i]) < Ord(string2[i]);
319 Exit;
320 end;
321 Result := Length(string1) < Length(string2);
322end;
323
324
325
326
327function ExportFile(fileid: LongWord; filename: String; settings: TExportSet;
328 path: String): Integer;
329var
330 i: Byte;
331 extension: String;
332 rawlist: TRawList;
333begin
334 Result := export_noerror;
335 extension := RightStr(filename, 4);
336 if DO_toone in settings then
337 begin
338 ExportDatFile(fileid, path + '\' + GetWinFileName(filename));
339 end
340 else
341 begin
342 if DO_dat in settings then
343 ExportDatFile(fileid, path + '\' + GetWinFileName(filename));
344 if DO_raw in settings then
345 begin
346 rawlist := OniDataConnection.GetRawList(fileid);
347 if Length(rawlist) > 0 then
348 begin
349 for i := 0 to High(rawlist) do
350 begin
351 ExportRawFile(fileid, rawlist[i].src_offset, path + '\' +
352 GetWinFileName(filename));
353 end;
354 end;
355 end;
356 end;
357end;
358
359
360
361
362function Explode(_string: String; delimiter: Char): TStringArray;
363var
364 start, len: Word;
365begin
366 SetLength(Result, 0);
367 start := 1;
368 while PosEx(delimiter, _string, start) > 0 do
369 begin
370 len := PosEx(delimiter, _string, start) - start;
371 SetLength(Result, Length(Result) + 1);
372 Result[High(Result)] := MidStr(_string, start, len);
373 start := start + len + 1;
374 end;
375 SetLength(Result, Length(Result) + 1);
376 Result[High(Result)] := MidStr(_string, start, Length(_string) - start + 1);
377end;
378
379
380
381
382function GetWinFileName(Name: String): String;
383begin
384 Result := Name;
385 Result := AnsiReplaceStr(Result, '\', '__');
386 Result := AnsiReplaceStr(Result, '/', '__');
387 Result := AnsiReplaceStr(Result, '>', '__');
388 Result := AnsiReplaceStr(Result, '<', '__');
389end;
390
391
392
393
394function GetExtractPath: String;
395begin
396 Result := ExtractFilePath(OniDataConnection.FileName) + '\extracted_' +
397 ExtractFileName(OniDataConnection.Filename);
398end;
399
400
401end.
Note: See TracBrowser for help on using the repository browser.