source: branches/DirectWeb/UCommon.pas

Last change on this file was 87, checked in by george, 15 years ago
  • Přidáno: Podpora pro ukládání serverových proměnných HTTP sezení.
  • Upraveno: U funkce Explode lze nyní omezit počet výsledných částí řetězce.
File size: 3.2 KB
Line 
1unit UCommon;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 {$IFDEF Windows}
9 Windows,
10 ShFolder,
11 {$ENDIF} SysUtils;
12
13type
14 TArrayOfByte = array of Byte;
15 TArrayOfString = array of string;
16
17function IntToBin(Data: Cardinal; Count: Byte): string;
18function TryHexToInt(Data: string; var Value: Integer): Boolean;
19function TryBinToInt(Data: string; var Value: Integer): Boolean;
20{$IFDEF Windows}
21function GetSpecialFolderPath(Folder: Integer): string;
22{$ENDIF}
23function BCDToInt(Value: Byte): Byte;
24function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
25function Explode(Separator: Char; Data: string; SlicesCount: Integer = -1): TArrayOfString;
26
27implementation
28
29function BCDToInt(Value: Byte): Byte;
30begin
31 Result := (Value shr 4) * 10 + (Value and 15);
32end;
33
34{$IFDEF Windows}
35function GetSpecialFolderPath(Folder: Integer): string;
36const
37 SHGFP_TYPE_CURRENT = 0;
38var
39 Path: array[0..MAX_PATH] of Char;
40begin
41 if SUCCEEDED(SHGetFolderPath(0, Folder, 0, SHGFP_TYPE_CURRENT, @path[0])) then
42 Result := path
43 else
44 Result := '';
45end;
46{$ENDIF}
47
48function IntToBin(Data: Cardinal; Count: Byte): string;
49var
50 I: Integer;
51begin
52 Result := '';
53 for I := 0 to Count - 1 do
54 Result := IntToStr((Data shr I) and 1) + Result;
55end;
56
57function IntToHex(Data: Cardinal; Count: Byte): string;
58const
59 Chars: array[0..15] of Char = '0123456789ABCDEF';
60var
61 I: Integer;
62begin
63 Result := '';
64 for I := 0 to Count - 1 do
65 Result := Result + Chars[(Data shr (I * 4)) and 15];
66end;
67
68function TryHexToInt(Data: string; var Value: Integer): Boolean;
69var
70 I: Integer;
71begin
72 Data := UpperCase(Data);
73 Result := True;
74 Value := 0;
75 for I := 0 to Length(Data) - 1 do begin
76 if (Data[I + 1] >= '0') and (Data[I + 1] <= '9') then
77 Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1) * 4)
78 else if (Data[I + 1] >= 'A') and (Data[I + 1] <= 'F') then
79 Value := Value or (Ord(Data[I + 1]) - Ord('A') + 10) shl ((Length(Data) - I - 1) * 4)
80 else Result := False;
81 end;
82end;
83
84function TryBinToInt(Data: string; var Value: Integer): Boolean;
85var
86 I: Integer;
87begin
88 Result := True;
89 Value := 0;
90 for I := 0 to Length(Data) - 1 do begin
91 if (Data[I + 1] >= '0') and (Data[I + 1] <= '1') then
92 Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1))
93 else Result := False;
94 end;
95end;
96
97function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
98var
99 I: Integer;
100begin
101 if Length(Data1) = Length(Data2) then begin
102 Result := True;
103 for I := 0 to Length(Data1) - 1 do begin
104 if Data1[I] <> Data2[I] then begin
105 Result := False;
106 Break;
107 end
108 end;
109 end else Result := False;
110end;
111
112function Explode(Separator: char; Data: string; SlicesCount: Integer = -1): TArrayOfString;
113begin
114 SetLength(Result, 0);
115 while (Pos(Separator, Data) > 0) and
116 ((Length(Result) < (SlicesCount - 1)) or (SlicesCount = -1)) do begin
117 SetLength(Result, Length(Result) + 1);
118 Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1);
119 Delete(Data, 1, Pos(Separator, Data));
120 end;
121 SetLength(Result, Length(Result) + 1);
122 Result[High(Result)] := Data;
123end;
124
125end.
Note: See TracBrowser for help on using the repository browser.