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

Last change on this file since 247 was 245, checked in by alloc, 17 years ago
File size: 6.9 KB
RevLine 
[97]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;
[105]12function IntToBin(Value: Byte): String;
[97]13function DataToBin(Data: TByteData): String;
14function BinToInt(bin: String): Byte;
[113]15function MakeDatLink(FileID: Integer): Integer;
[97]16
[245]17function ReadString(Stream: TStream; Offset: Integer): String;
18
[97]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
[105]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;
[97]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
[113]143function MakeDatLink(FileID: Integer): Integer;
144begin
145 Result := FileID * 256 + 1;
146end;
[97]147
[113]148
149
[245]150
151function ReadString(Stream: TStream; Offset: Integer): String;
152var
153 i: Integer;
154 c: Char;
155begin
156 if Assigned(Stream) then
157 begin
158 if Offset >= 0 then
159 begin
160 Result := '';
161 Stream.Seek(Offset, soFromBeginning);
162 repeat
163 Stream.Read(c, 1);
164 if Ord(c) > 0 then
165 Result := Result + c;
166 until Ord(c) = 0;
167 end;
168 end;
169end;
170
171
172
[97]173function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String;
174begin
175 Result := AnsiReplaceStr(Format('%' + IntToStr(Width) + 'u', [Value]), ' ', leadingzeros);
176end;
177
178
179
180
181function FormatFileSize(size: LongWord): String;
182var
183 floatformat: TFormatSettings;
184begin
185 floatformat.DecimalSeparator := '.';
186 if size >= 1000 * 1024 * 1024 then
187 Result := FloatToStrF(size / 1024 / 1024 / 1024, ffFixed, 5, 1, floatformat) + ' GB'
188 else
189 if size >= 1000 * 1024 then
190 Result := FloatToStrF(size / 1024 / 1024, ffFixed, 5, 1, floatformat) + ' MB'
191 else
192 if size >= 1000 then
193 Result := FloatToStrF(size / 1024, ffFixed, 5, 1, floatformat) + ' KB'
194 else
195 Result := IntToStr(size) + ' B';
196end;
197
198
199
200
201function CreateHexString(Data: TByteData; HexOnly: Boolean): String;
202var
203 string_build, ascii_version: String;
[111]204 i: Integer;
[97]205begin
206 string_build := '';
207 ascii_version := '';
208 for i := 0 to High(Data) do
209 begin
210 if not HexOnly then
211 if (i mod 16) = 0 then
212 string_build := string_build + '0x' + IntToHex(i, 6) + ' ';
213 string_build := string_build + IntToHex(Data[i], 2);
214 if not HexOnly then
215 begin
216 if Data[i] >= 32 then
217 ascii_version := ascii_version + Chr(Data[i])
218 else
219 ascii_version := ascii_version + '.';
220 if ((i + 1) mod 2) = 0 then
221 string_build := string_build + #32;
222 if ((i + 1) mod 16) = 0 then
223 begin
224 string_build := string_build + #32 + ascii_version + CrLf;
225 ascii_version := '';
226 end;
227 end;
228 end;
229 Result := string_build;
230end;
231
232
233
234
235function DecodeHexString(hex: String): TByteData;
236var
[111]237 i: Integer;
[97]238begin
239 SetLength(Result, Length(hex) div 2);
240 for i := 0 to Length(Result) do
241 begin
242 Result[i] := 0;
243 case UpCase(hex[1 + i * 2]) of
244 '0'..'9':
245 Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 48) * 16;
246 'A'..'F':
247 Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 55) * 16;
248 end;
249 case UpCase(hex[1 + i * 2 + 1]) of
250 '0'..'9':
251 Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 48);
252 'A'..'F':
253 Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 55);
254 end;
255 end;
256end;
257
258
259
260
261function StringSmaller(string1, string2: String): Boolean;
262var
263 i: Integer;
264 len: Integer;
265begin
266 len := Min(Length(string1), Length(string2));
267 for i := 1 to len do
268 if Ord(string1[i]) <> Ord(string2[i]) then
269 begin
270 Result := Ord(string1[i]) < Ord(string2[i]);
271 Exit;
272 end;
273 Result := Length(string1) < Length(string2);
274end;
275
276
277
278function Explode(_string: String; delimiter: Char): TStrings;
279var
280 start, len: Word;
281begin
282 Result := TStringList.Create;
283 start := 1;
284 while PosEx(delimiter, _string, start) > 0 do
285 begin
286 len := PosEx(delimiter, _string, start) - start;
287 Result.Add(MidStr(_string, start, len));
288 start := start + len + 1;
289 end;
290 Result.Add(MidStr(_string, start, Length(_string) - start + 1));
291end;
292
293
294function GetWinFileName(Name: String): String;
295begin
296 Result := Name;
297 Result := AnsiReplaceStr(Result, '\', '__');
298 Result := AnsiReplaceStr(Result, '/', '__');
299 Result := AnsiReplaceStr(Result, '>', '__');
300 Result := AnsiReplaceStr(Result, '<', '__');
301end;
302
303
304end.
Note: See TracBrowser for help on using the repository browser.