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