source: oup/releases/0.33a/Code/Functions.pas@ 268

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