source: oup/rewrite/Global/Functions.pas@ 104

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