source: oup/current/Global/Functions.pas@ 201

Last change on this file since 201 was 113, checked in by alloc, 18 years ago
File size: 6.5 KB
Line 
1unit Functions;
2
3interface
4
5uses TypeDefs, Classes;
6
7function BoolToStr(bool: Boolean): String;
8function Decode_Int(buffer: TByteData): LongWord;
9function Encode_Int(input: LongWord): TByteData;
10function Decode_Float(buffer: TByteData): Single;
11function Encode_Float(input: Single): TByteData;
12function IntToBin(Value: Byte): String;
13function DataToBin(Data: TByteData): String;
14function BinToInt(bin: String): Byte;
15function MakeDatLink(FileID: Integer): Integer;
16
17function StringSmaller(string1, string2: String): Boolean;
18
19function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String;
20function FormatFileSize(size: LongWord): String;
21function CreateHexString(Data: TByteData; HexOnly: Boolean): String;
22function DecodeHexString(hex: String): TByteData;
23function GetWinFileName(Name: String): String;
24//function GetExtractPath: String;
25
26function Explode(_string: String; delimiter: Char): TStrings;
27
28
29implementation
30
31uses SysUtils, Math, StrUtils;
32
33type
34 TValueSwitcher = record
35 case IsFloat: Boolean of
36 True: (ValueFloat: Single);
37 False: (ValueInt: LongWord);
38 end;
39
40
41
42function BoolToStr(bool: Boolean): String;
43begin
44 if bool then
45 Result := 'true'
46 else
47 Result := 'false';
48end;
49
50
51function Decode_Int(buffer: TByteData): LongWord;
52begin
53 Result := buffer[0] + buffer[1] * 256 + buffer[2] * 256 * 256 + buffer[3] * 256 * 256 * 256;
54end;
55
56
57function Encode_Int(input: LongWord): TByteData;
58begin
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;
67end;
68
69
70function Decode_Float(buffer: TByteData): Single;
71var
72 _valueswitcher: TValueSwitcher;
73begin
74 _valueswitcher.ValueInt := Decode_Int(buffer);
75 Result := _valueswitcher.ValueFloat;
76 if IsNAN(Result) then
77 Result := 0.0;
78end;
79
80
81function Encode_Float(input: Single): TByteData;
82var
83 _valueswitcher: TValueSwitcher;
84begin
85 _valueswitcher.ValueFloat := input;
86 Result := Encode_Int(_valueswitcher.ValueInt);
87end;
88
89
90function IntToBin(Value: Byte): String;
91var
92 i: Byte;
93begin
94 Result := '';
95 for i := 7 downto 0 do
96 Result := Result + IntToStr((Value shr i) and $01);
97end;
98
99
100function DataToBin(Data: TByteData): String;
101var
102 i, j: Byte;
103 singlebyte: Byte;
104 bytepart: String;
105begin
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;
118end;
119
120
121function BinToInt(bin: String): Byte;
122var
123 Add: Integer;
124 i: Byte;
125begin
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;
138end;
139
140
141function MakeDatLink(FileID: Integer): Integer;
142begin
143 Result := FileID * 256 + 1;
144end;
145
146
147
148function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String;
149begin
150 Result := AnsiReplaceStr(Format('%' + IntToStr(Width) + 'u', [Value]), ' ', leadingzeros);
151end;
152
153
154
155
156function FormatFileSize(size: LongWord): String;
157var
158 floatformat: TFormatSettings;
159begin
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';
171end;
172
173
174
175
176function CreateHexString(Data: TByteData; HexOnly: Boolean): String;
177var
178 string_build, ascii_version: String;
179 i: Integer;
180begin
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;
205end;
206
207
208
209
210function DecodeHexString(hex: String): TByteData;
211var
212 i: Integer;
213begin
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;
231end;
232
233
234
235
236function StringSmaller(string1, string2: String): Boolean;
237var
238 i: Integer;
239 len: Integer;
240begin
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);
249end;
250
251
252
253function Explode(_string: String; delimiter: Char): TStrings;
254var
255 start, len: Word;
256begin
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));
266end;
267
268
269function GetWinFileName(Name: String): String;
270begin
271 Result := Name;
272 Result := AnsiReplaceStr(Result, '\', '__');
273 Result := AnsiReplaceStr(Result, '/', '__');
274 Result := AnsiReplaceStr(Result, '>', '__');
275 Result := AnsiReplaceStr(Result, '<', '__');
276end;
277
278
279end.
Note: See TracBrowser for help on using the repository browser.