1 |
unit Functions; |
2 |
|
3 |
interface |
4 |
|
5 |
uses Classes, Dialogs, SysUtils, StrUtils, Math, Data; |
6 |
|
7 |
function BoolToStr(bool: Boolean): String; |
8 |
function HexToLong(hex: String): LongWord; |
9 |
function Decode_Int(buffer: Tdata): LongWord; |
10 |
function Encode_Int(input: LongWord): Tdata; |
11 |
function Decode_Float(buffer: Tdata): Single; |
12 |
function Encode_Float(input: Single): Tdata; |
13 |
function DataToBin(Data: Tdata): String; |
14 |
function BinToInt(bin: String): Byte; |
15 |
|
16 |
function StringSmaller(string1, string2: String): Boolean; |
17 |
|
18 |
function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String; |
19 |
function FormatFileSize(size: LongWord): String; |
20 |
function CreateHexString(Data: Tdata; HexOnly: Boolean): String; |
21 |
function DecodeHexString(hex: String): Tdata; |
22 |
function GetWinFileName(Name: String): String; |
23 |
function GetExtractPath: String; |
24 |
|
25 |
function Explode(_string: String; delimiter: Char): TStringArray; |
26 |
|
27 |
|
28 |
implementation |
29 |
|
30 |
uses Exporters, OniDataClass; |
31 |
|
32 |
type |
33 |
TValueSwitcher = record |
34 |
case IsFloat: Boolean of |
35 |
True: (ValueFloat: Single); |
36 |
False: (ValueInt: LongWord); |
37 |
end; |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
function BoolToStr(bool: Boolean): String; |
43 |
begin |
44 |
if bool then |
45 |
Result := 'true' |
46 |
else |
47 |
Result := 'false'; |
48 |
end; |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
function 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 |
|
84 |
var |
85 |
i: Byte; |
86 |
begin |
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; |
109 |
end; |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
function Decode_Int(buffer: Tdata): LongWord; |
115 |
begin |
116 |
Result := buffer[0] + buffer[1] * 256 + buffer[2] * 256 * 256 + buffer[3] * 256 * 256 * 256; |
117 |
end; |
118 |
|
119 |
|
120 |
|
121 |
|
122 |
function Encode_Int(input: LongWord): Tdata; |
123 |
begin |
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; |
132 |
end; |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
function Decode_Float(buffer: Tdata): Single; |
138 |
var |
139 |
_valueswitcher: TValueSwitcher; |
140 |
begin |
141 |
_valueswitcher.ValueInt := Decode_Int(buffer); |
142 |
Result := _valueswitcher.ValueFloat; |
143 |
if IsNAN(Result) then |
144 |
Result := 0.0; |
145 |
end; |
146 |
|
147 |
|
148 |
|
149 |
|
150 |
function Encode_Float(input: Single): Tdata; |
151 |
var |
152 |
_valueswitcher: TValueSwitcher; |
153 |
begin |
154 |
_valueswitcher.ValueFloat := input; |
155 |
Result := Encode_Int(_valueswitcher.ValueInt); |
156 |
end; |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
function DataToBin(Data: Tdata): String; |
162 |
var |
163 |
i, j: Byte; |
164 |
singlebyte: Byte; |
165 |
bytepart: String; |
166 |
begin |
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; |
179 |
end; |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
function BinToInt(bin: String): Byte; |
185 |
var |
186 |
Add: Integer; |
187 |
i: Byte; |
188 |
begin |
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; |
201 |
end; |
202 |
|
203 |
|
204 |
|
205 |
|
206 |
function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String; |
207 |
begin |
208 |
Result := AnsiReplaceStr(Format('%' + IntToStr(Width) + 'u', [Value]), ' ', leadingzeros); |
209 |
end; |
210 |
|
211 |
|
212 |
|
213 |
|
214 |
function FormatFileSize(size: LongWord): String; |
215 |
begin |
216 |
if size >= 1000 * 1024 * 1024 then |
217 |
begin |
218 |
Result := FloatToStrF(size / 1024 / 1024 / 1024, ffFixed, 5, 1) + ' GB'; |
219 |
end |
220 |
else |
221 |
begin |
222 |
if size >= 1000 * 1024 then |
223 |
begin |
224 |
Result := FloatToStrF(size / 1024 / 1024, ffFixed, 5, 1) + ' MB'; |
225 |
end |
226 |
else |
227 |
begin |
228 |
if size >= 1000 then |
229 |
begin |
230 |
Result := FloatToStrF(size / 1024, ffFixed, 5, 1) + ' KB'; |
231 |
end |
232 |
else |
233 |
begin |
234 |
Result := IntToStr(size) + ' B'; |
235 |
end; |
236 |
end; |
237 |
end; |
238 |
end; |
239 |
|
240 |
|
241 |
|
242 |
|
243 |
function CreateHexString(Data: Tdata; HexOnly: Boolean): String; |
244 |
var |
245 |
string_build, ascii_version: String; |
246 |
i: LongWord; |
247 |
begin |
248 |
string_build := ''; |
249 |
ascii_version := ''; |
250 |
for i := 0 to High(Data) do |
251 |
begin |
252 |
if not HexOnly then |
253 |
if (i mod 16) = 0 then |
254 |
string_build := string_build + '0x' + IntToHex(i, 6) + ' '; |
255 |
string_build := string_build + IntToHex(Data[i], 2); |
256 |
if not HexOnly then |
257 |
begin |
258 |
if Data[i] >= 32 then |
259 |
ascii_version := ascii_version + Chr(Data[i]) |
260 |
else |
261 |
ascii_version := ascii_version + '.'; |
262 |
if ((i + 1) mod 2) = 0 then |
263 |
string_build := string_build + #32; |
264 |
if ((i + 1) mod 16) = 0 then |
265 |
begin |
266 |
string_build := string_build + #32 + ascii_version + CrLf; |
267 |
ascii_version := ''; |
268 |
end; |
269 |
end; |
270 |
end; |
271 |
Result := string_build; |
272 |
end; |
273 |
|
274 |
|
275 |
|
276 |
|
277 |
function DecodeHexString(hex: String): Tdata; |
278 |
var |
279 |
i: LongWord; |
280 |
begin |
281 |
SetLength(Result, Length(hex) div 2); |
282 |
for i := 0 to Length(Result) do |
283 |
begin |
284 |
Result[i] := 0; |
285 |
case UpCase(hex[1 + i * 2]) of |
286 |
'0'..'9': |
287 |
Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 48) * 16; |
288 |
'A'..'F': |
289 |
Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 55) * 16; |
290 |
end; |
291 |
case UpCase(hex[1 + i * 2 + 1]) of |
292 |
'0'..'9': |
293 |
Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 48); |
294 |
'A'..'F': |
295 |
Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 55); |
296 |
end; |
297 |
end; |
298 |
end; |
299 |
|
300 |
|
301 |
|
302 |
|
303 |
function StringSmaller(string1, string2: String): Boolean; |
304 |
var |
305 |
i: Integer; |
306 |
len: Integer; |
307 |
begin |
308 |
len := Min(Length(string1), Length(string2)); |
309 |
for i := 1 to len do |
310 |
if Ord(string1[i]) <> Ord(string2[i]) then |
311 |
begin |
312 |
Result := Ord(string1[i]) < Ord(string2[i]); |
313 |
Exit; |
314 |
end; |
315 |
Result := Length(string1) < Length(string2); |
316 |
end; |
317 |
|
318 |
|
319 |
|
320 |
function Explode(_string: String; delimiter: Char): TStringArray; |
321 |
var |
322 |
start, len: Word; |
323 |
begin |
324 |
SetLength(Result, 0); |
325 |
start := 1; |
326 |
while PosEx(delimiter, _string, start) > 0 do |
327 |
begin |
328 |
len := PosEx(delimiter, _string, start) - start; |
329 |
SetLength(Result, Length(Result) + 1); |
330 |
Result[High(Result)] := MidStr(_string, start, len); |
331 |
start := start + len + 1; |
332 |
end; |
333 |
SetLength(Result, Length(Result) + 1); |
334 |
Result[High(Result)] := MidStr(_string, start, Length(_string) - start + 1); |
335 |
end; |
336 |
|
337 |
|
338 |
|
339 |
|
340 |
function GetWinFileName(Name: String): String; |
341 |
begin |
342 |
Result := Name; |
343 |
Result := AnsiReplaceStr(Result, '\', '__'); |
344 |
Result := AnsiReplaceStr(Result, '/', '__'); |
345 |
Result := AnsiReplaceStr(Result, '>', '__'); |
346 |
Result := AnsiReplaceStr(Result, '<', '__'); |
347 |
end; |
348 |
|
349 |
|
350 |
|
351 |
|
352 |
function GetExtractPath: String; |
353 |
begin |
354 |
Result := ExtractFilePath(OniDataConnection.FileName) + '\extracted_' + |
355 |
ExtractFileName(OniDataConnection.Filename); |
356 |
end; |
357 |
|
358 |
|
359 |
end. |