1 |
unit Functions; |
2 |
|
3 |
interface |
4 |
|
5 |
uses TypeDefs, Classes; |
6 |
|
7 |
function BoolToStr(bool: Boolean): String; |
8 |
function Decode_Int(buffer: TByteData): LongWord; |
9 |
function Encode_Int(input: LongWord): TByteData; |
10 |
function Decode_Float(buffer: TByteData): Single; |
11 |
function Encode_Float(input: Single): TByteData; |
12 |
function IntToBin(Value: Byte): String; |
13 |
function DataToBin(Data: TByteData): String; |
14 |
function BinToInt(bin: String): Byte; |
15 |
function MakeDatLink(FileID: Integer): Integer; |
16 |
|
17 |
function ReadString(Stream: TStream; Offset: Integer): String; |
18 |
|
19 |
function StringSmaller(string1, string2: String): Boolean; |
20 |
|
21 |
function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String; |
22 |
function FormatFileSize(size: LongWord): String; |
23 |
function CreateHexString(Data: TByteData; HexOnly: Boolean): String; |
24 |
function DecodeHexString(hex: String): TByteData; |
25 |
function GetWinFileName(Name: String): String; |
26 |
//function GetExtractPath: String; |
27 |
|
28 |
function Explode(_string: String; delimiter: Char): TStrings; |
29 |
|
30 |
|
31 |
implementation |
32 |
|
33 |
uses SysUtils, Math, StrUtils; |
34 |
|
35 |
type |
36 |
TValueSwitcher = record |
37 |
case IsFloat: Boolean of |
38 |
True: (ValueFloat: Single); |
39 |
False: (ValueInt: LongWord); |
40 |
end; |
41 |
|
42 |
|
43 |
|
44 |
function BoolToStr(bool: Boolean): String; |
45 |
begin |
46 |
if bool then |
47 |
Result := 'true' |
48 |
else |
49 |
Result := 'false'; |
50 |
end; |
51 |
|
52 |
|
53 |
function Decode_Int(buffer: TByteData): LongWord; |
54 |
begin |
55 |
Result := buffer[0] + buffer[1] * 256 + buffer[2] * 256 * 256 + buffer[3] * 256 * 256 * 256; |
56 |
end; |
57 |
|
58 |
|
59 |
function Encode_Int(input: LongWord): TByteData; |
60 |
begin |
61 |
SetLength(Result, 4); |
62 |
Result[0] := input mod 256; |
63 |
input := input div 256; |
64 |
Result[1] := input mod 256; |
65 |
input := input div 256; |
66 |
Result[2] := input mod 256; |
67 |
input := input div 256; |
68 |
Result[3] := input mod 256; |
69 |
end; |
70 |
|
71 |
|
72 |
function Decode_Float(buffer: TByteData): Single; |
73 |
var |
74 |
_valueswitcher: TValueSwitcher; |
75 |
begin |
76 |
_valueswitcher.ValueInt := Decode_Int(buffer); |
77 |
Result := _valueswitcher.ValueFloat; |
78 |
if IsNAN(Result) then |
79 |
Result := 0.0; |
80 |
end; |
81 |
|
82 |
|
83 |
function Encode_Float(input: Single): TByteData; |
84 |
var |
85 |
_valueswitcher: TValueSwitcher; |
86 |
begin |
87 |
_valueswitcher.ValueFloat := input; |
88 |
Result := Encode_Int(_valueswitcher.ValueInt); |
89 |
end; |
90 |
|
91 |
|
92 |
function IntToBin(Value: Byte): String; |
93 |
var |
94 |
i: Byte; |
95 |
begin |
96 |
Result := ''; |
97 |
for i := 7 downto 0 do |
98 |
Result := Result + IntToStr((Value shr i) and $01); |
99 |
end; |
100 |
|
101 |
|
102 |
function DataToBin(Data: TByteData): String; |
103 |
var |
104 |
i, j: Byte; |
105 |
singlebyte: Byte; |
106 |
bytepart: String; |
107 |
begin |
108 |
SetLength(bytepart, 8); |
109 |
Result := ''; |
110 |
for i := 0 to High(Data) do |
111 |
begin |
112 |
singlebyte := Data[i]; |
113 |
for j := 7 downto 0 do |
114 |
begin |
115 |
bytepart[j + 1] := Char((singlebyte and $01) + 48); |
116 |
singlebyte := singlebyte shr 1; |
117 |
end; |
118 |
Result := Result + bytepart + ' '; |
119 |
end; |
120 |
end; |
121 |
|
122 |
|
123 |
function BinToInt(bin: String): Byte; |
124 |
var |
125 |
Add: Integer; |
126 |
i: Byte; |
127 |
begin |
128 |
Result := 0; |
129 |
if Length(bin) <> 8 then |
130 |
Exit; |
131 |
Add := 1; |
132 |
for i := 8 downto 1 do |
133 |
begin |
134 |
if not (bin[i] in ['0', '1']) then |
135 |
Exit; |
136 |
if bin[i] = '1' then |
137 |
Inc(Result, Add); |
138 |
Add := Add shl 1; |
139 |
end; |
140 |
end; |
141 |
|
142 |
|
143 |
function MakeDatLink(FileID: Integer): Integer; |
144 |
begin |
145 |
Result := FileID * 256 + 1; |
146 |
end; |
147 |
|
148 |
|
149 |
|
150 |
|
151 |
function ReadString(Stream: TStream; Offset: Integer): String; |
152 |
var |
153 |
c: Char; |
154 |
begin |
155 |
if Assigned(Stream) then |
156 |
begin |
157 |
if Offset >= 0 then |
158 |
begin |
159 |
Result := ''; |
160 |
Stream.Seek(Offset, soFromBeginning); |
161 |
repeat |
162 |
Stream.Read(c, 1); |
163 |
if Ord(c) > 0 then |
164 |
Result := Result + c; |
165 |
until Ord(c) = 0; |
166 |
end; |
167 |
end; |
168 |
end; |
169 |
|
170 |
|
171 |
|
172 |
function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String; |
173 |
begin |
174 |
Result := AnsiReplaceStr(Format('%' + IntToStr(Width) + 'u', [Value]), ' ', leadingzeros); |
175 |
end; |
176 |
|
177 |
|
178 |
|
179 |
|
180 |
function FormatFileSize(size: LongWord): String; |
181 |
var |
182 |
floatformat: TFormatSettings; |
183 |
begin |
184 |
floatformat.DecimalSeparator := '.'; |
185 |
if size >= 1000 * 1024 * 1024 then |
186 |
Result := FloatToStrF(size / 1024 / 1024 / 1024, ffFixed, 5, 1, floatformat) + ' GB' |
187 |
else |
188 |
if size >= 1000 * 1024 then |
189 |
Result := FloatToStrF(size / 1024 / 1024, ffFixed, 5, 1, floatformat) + ' MB' |
190 |
else |
191 |
if size >= 1000 then |
192 |
Result := FloatToStrF(size / 1024, ffFixed, 5, 1, floatformat) + ' KB' |
193 |
else |
194 |
Result := IntToStr(size) + ' B'; |
195 |
end; |
196 |
|
197 |
|
198 |
|
199 |
|
200 |
function CreateHexString(Data: TByteData; HexOnly: Boolean): String; |
201 |
var |
202 |
string_build, ascii_version: String; |
203 |
i: Integer; |
204 |
begin |
205 |
string_build := ''; |
206 |
ascii_version := ''; |
207 |
for i := 0 to High(Data) do |
208 |
begin |
209 |
if not HexOnly then |
210 |
if (i mod 16) = 0 then |
211 |
string_build := string_build + '0x' + IntToHex(i, 6) + ' '; |
212 |
string_build := string_build + IntToHex(Data[i], 2); |
213 |
if not HexOnly then |
214 |
begin |
215 |
if Data[i] >= 32 then |
216 |
ascii_version := ascii_version + Chr(Data[i]) |
217 |
else |
218 |
ascii_version := ascii_version + '.'; |
219 |
if ((i + 1) mod 2) = 0 then |
220 |
string_build := string_build + #32; |
221 |
if ((i + 1) mod 16) = 0 then |
222 |
begin |
223 |
string_build := string_build + #32 + ascii_version + CrLf; |
224 |
ascii_version := ''; |
225 |
end; |
226 |
end; |
227 |
end; |
228 |
Result := string_build; |
229 |
end; |
230 |
|
231 |
|
232 |
|
233 |
|
234 |
function DecodeHexString(hex: String): TByteData; |
235 |
var |
236 |
i: Integer; |
237 |
begin |
238 |
SetLength(Result, Length(hex) div 2); |
239 |
for i := 0 to Length(Result) do |
240 |
begin |
241 |
Result[i] := 0; |
242 |
case UpCase(hex[1 + i * 2]) of |
243 |
'0'..'9': |
244 |
Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 48) * 16; |
245 |
'A'..'F': |
246 |
Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 55) * 16; |
247 |
end; |
248 |
case UpCase(hex[1 + i * 2 + 1]) of |
249 |
'0'..'9': |
250 |
Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 48); |
251 |
'A'..'F': |
252 |
Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 55); |
253 |
end; |
254 |
end; |
255 |
end; |
256 |
|
257 |
|
258 |
|
259 |
|
260 |
function StringSmaller(string1, string2: String): Boolean; |
261 |
var |
262 |
i: Integer; |
263 |
len: Integer; |
264 |
begin |
265 |
len := Min(Length(string1), Length(string2)); |
266 |
for i := 1 to len do |
267 |
if Ord(string1[i]) <> Ord(string2[i]) then |
268 |
begin |
269 |
Result := Ord(string1[i]) < Ord(string2[i]); |
270 |
Exit; |
271 |
end; |
272 |
Result := Length(string1) < Length(string2); |
273 |
end; |
274 |
|
275 |
|
276 |
|
277 |
function Explode(_string: String; delimiter: Char): TStrings; |
278 |
var |
279 |
start, len: Word; |
280 |
begin |
281 |
Result := TStringList.Create; |
282 |
start := 1; |
283 |
while PosEx(delimiter, _string, start) > 0 do |
284 |
begin |
285 |
len := PosEx(delimiter, _string, start) - start; |
286 |
Result.Add(MidStr(_string, start, len)); |
287 |
start := start + len + 1; |
288 |
end; |
289 |
Result.Add(MidStr(_string, start, Length(_string) - start + 1)); |
290 |
end; |
291 |
|
292 |
|
293 |
function GetWinFileName(Name: String): String; |
294 |
begin |
295 |
Result := Name; |
296 |
Result := AnsiReplaceStr(Result, '\', '__'); |
297 |
Result := AnsiReplaceStr(Result, '/', '__'); |
298 |
Result := AnsiReplaceStr(Result, '>', '__'); |
299 |
Result := AnsiReplaceStr(Result, '<', '__'); |
300 |
end; |
301 |
|
302 |
|
303 |
end. |