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

Last change on this file since 368 was 321, checked in by alloc, 16 years ago
File size: 6.9 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 ReadString(Stream: TStream; Offset: Integer): String;
18
19function StringSmaller(string1, string2: String): Boolean;
20
21function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String;
22function FormatFileSize(size: LongWord): String;
23function CreateHexString(Data: TByteData; HexOnly: Boolean): String;
24function DecodeHexString(hex: String): TByteData;
25function GetWinFileName(Name: String): String;
26//function GetExtractPath: String;
27
28function Explode(_string: String; delimiter: Char): TStrings;
29
30
31implementation
32
33uses SysUtils, Math, StrUtils;
34
35type
36 TValueSwitcher = record
37 case IsFloat: Boolean of
38 True: (ValueFloat: Single);
39 False: (ValueInt: LongWord);
40 end;
41
42
43
44function BoolToStr(bool: Boolean): String;
45begin
46 if bool then
47 Result := 'true'
48 else
49 Result := 'false';
50end;
51
52
53function Decode_Int(buffer: TByteData): LongWord;
54begin
55 Result := buffer[0] + buffer[1] * 256 + buffer[2] * 256 * 256 + buffer[3] * 256 * 256 * 256;
56end;
57
58
59function Encode_Int(input: LongWord): TByteData;
60begin
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;
69end;
70
71
72function Decode_Float(buffer: TByteData): Single;
73var
74 _valueswitcher: TValueSwitcher;
75begin
76 _valueswitcher.ValueInt := Decode_Int(buffer);
77 Result := _valueswitcher.ValueFloat;
78 if IsNAN(Result) then
79 Result := 0.0;
80end;
81
82
83function Encode_Float(input: Single): TByteData;
84var
85 _valueswitcher: TValueSwitcher;
86begin
87 _valueswitcher.ValueFloat := input;
88 Result := Encode_Int(_valueswitcher.ValueInt);
89end;
90
91
92function IntToBin(Value: Byte): String;
93var
94 i: Byte;
95begin
96 Result := '';
97 for i := 7 downto 0 do
98 Result := Result + IntToStr((Value shr i) and $01);
99end;
100
101
102function DataToBin(Data: TByteData): String;
103var
104 i, j: Byte;
105 singlebyte: Byte;
106 bytepart: String;
107begin
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;
120end;
121
122
123function BinToInt(bin: String): Byte;
124var
125 Add: Integer;
126 i: Byte;
127begin
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;
140end;
141
142
143function MakeDatLink(FileID: Integer): Integer;
144begin
145 Result := FileID * 256 + 1;
146end;
147
148
149
150
151function ReadString(Stream: TStream; Offset: Integer): String;
152var
153 c: Char;
154begin
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;
168end;
169
170
171
172function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String;
173begin
174 Result := AnsiReplaceStr(Format('%' + IntToStr(Width) + 'u', [Value]), ' ', leadingzeros);
175end;
176
177
178
179
180function FormatFileSize(size: LongWord): String;
181var
182 floatformat: TFormatSettings;
183begin
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';
195end;
196
197
198
199
200function CreateHexString(Data: TByteData; HexOnly: Boolean): String;
201var
202 string_build, ascii_version: String;
203 i: Integer;
204begin
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;
229end;
230
231
232
233
234function DecodeHexString(hex: String): TByteData;
235var
236 i: Integer;
237begin
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;
255end;
256
257
258
259
260function StringSmaller(string1, string2: String): Boolean;
261var
262 i: Integer;
263 len: Integer;
264begin
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);
273end;
274
275
276
277function Explode(_string: String; delimiter: Char): TStrings;
278var
279 start, len: Word;
280begin
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));
290end;
291
292
293function GetWinFileName(Name: String): String;
294begin
295 Result := Name;
296 Result := AnsiReplaceStr(Result, '\', '__');
297 Result := AnsiReplaceStr(Result, '/', '__');
298 Result := AnsiReplaceStr(Result, '>', '__');
299 Result := AnsiReplaceStr(Result, '<', '__');
300end;
301
302
303end.
Note: See TracBrowser for help on using the repository browser.