source: oup/current/Code/Functions.pas @ 74

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