2008-04-23 23:26:50 +03:00
|
|
|
{==============================================================================|
|
2008-04-24 09:42:13 +03:00
|
|
|
| Project : Delphree - Synapse | 001.007.000 |
|
2008-04-23 23:26:50 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| Content: support procedures and functions |
|
|
|
|
|==============================================================================|
|
|
|
|
| The contents of this file are subject to the Mozilla Public License Ver. 1.0 |
|
|
|
|
| (the "License"); you may not use this file except in compliance with the |
|
|
|
|
| License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
|
|
|
|
| |
|
|
|
|
| Software distributed under the License is distributed on an "AS IS" basis, |
|
|
|
|
| WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
|
|
|
|
| the specific language governing rights and limitations under the License. |
|
|
|
|
|==============================================================================|
|
|
|
|
| The Original Code is Synapse Delphi Library. |
|
|
|
|
|==============================================================================|
|
|
|
|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
|
2008-04-24 09:42:13 +03:00
|
|
|
| Portions created by Lukas Gebauer are Copyright (c) 1999,2000,2001. |
|
2008-04-23 23:32:54 +03:00
|
|
|
| Portions created by Hernan Sanchez are Copyright (c) 2000. |
|
2008-04-23 23:26:50 +03:00
|
|
|
| All Rights Reserved. |
|
|
|
|
|==============================================================================|
|
|
|
|
| Contributor(s): |
|
2008-04-23 23:32:54 +03:00
|
|
|
| Hernan Sanchez (hernan.sanchez@iname.com) |
|
2008-04-23 23:26:50 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| History: see HISTORY.HTM from distribution package |
|
2008-04-23 23:48:39 +03:00
|
|
|
| (Found at URL: http://www.ararat.cz/synapse/) |
|
2008-04-23 23:26:50 +03:00
|
|
|
|==============================================================================}
|
|
|
|
|
|
|
|
unit SynaUtil;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2008-04-23 23:51:17 +03:00
|
|
|
sysutils, classes, windows;
|
2008-04-23 23:26:50 +03:00
|
|
|
|
|
|
|
function timezone:string;
|
|
|
|
function Rfc822DateTime(t:TDateTime):String;
|
2008-04-23 23:28:55 +03:00
|
|
|
function CodeInt(Value:word):string;
|
|
|
|
function DeCodeInt(Value:string;Index:integer):word;
|
2008-04-23 23:26:50 +03:00
|
|
|
function IsIP(Value:string):Boolean;
|
|
|
|
function ReverseIP(Value:string):string;
|
|
|
|
procedure Dump (Buffer:string;DumpFile:string);
|
2008-04-23 23:48:39 +03:00
|
|
|
function SeparateLeft(value,delimiter:string):string;
|
|
|
|
function SeparateRight(value,delimiter:string):string;
|
|
|
|
function getparameter(value,parameter:string):string;
|
|
|
|
function GetEmailAddr(value:string):string;
|
|
|
|
function GetEmailDesc(value:string):string;
|
2008-04-23 23:49:43 +03:00
|
|
|
function StrToHex(value:string):string;
|
2008-04-24 09:40:58 +03:00
|
|
|
function IntToBin(value:integer;digits:byte):string;
|
|
|
|
function BinToInt(value:string):integer;
|
2008-04-24 09:42:13 +03:00
|
|
|
procedure ParseURL(URL:string; var Prot,User,Pass,Host,Port,Path,Para:string);
|
2008-04-23 23:26:50 +03:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
{==============================================================================}
|
2008-04-23 23:26:50 +03:00
|
|
|
{timezone}
|
|
|
|
function timezone:string;
|
|
|
|
var
|
|
|
|
zoneinfo:TTimeZoneInformation;
|
|
|
|
bias:integer;
|
|
|
|
h,m:integer;
|
|
|
|
begin
|
2008-04-23 23:46:58 +03:00
|
|
|
case GetTimeZoneInformation(Zoneinfo) of
|
|
|
|
2: bias:=zoneinfo.bias+zoneinfo.DaylightBias;
|
|
|
|
1: bias:=zoneinfo.bias+zoneinfo.StandardBias;
|
|
|
|
else
|
|
|
|
bias:=zoneinfo.bias;
|
|
|
|
end;
|
2008-04-23 23:26:50 +03:00
|
|
|
if bias<=0 then result:='+'
|
|
|
|
else result:='-';
|
|
|
|
bias:=abs(bias);
|
|
|
|
h:=bias div 60;
|
|
|
|
m:=bias mod 60;
|
|
|
|
result:=result+format('%.2d%.2d',[h,m]);
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-23 23:26:50 +03:00
|
|
|
{Rfc822DateTime}
|
|
|
|
function Rfc822DateTime(t:TDateTime):String;
|
|
|
|
var
|
|
|
|
I: Integer;
|
|
|
|
SaveDayNames: array[1..7] of string;
|
|
|
|
SaveMonthNames: array[1..12] of string;
|
|
|
|
const
|
|
|
|
MyDayNames: array[1..7] of string =
|
|
|
|
('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
|
|
|
|
MyMonthNames: array[1..12] of string =
|
|
|
|
('Jan', 'Feb', 'Mar', 'Apr',
|
|
|
|
'May', 'Jun', 'Jul', 'Aug',
|
|
|
|
'Sep', 'Oct', 'Nov', 'Dec');
|
|
|
|
begin
|
|
|
|
if ShortDayNames[1] = MyDayNames[1]
|
|
|
|
then Result := FormatDateTime('ddd, d mmm yyyy hh:mm:ss', t)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
for I := Low(ShortDayNames) to High(ShortDayNames) do
|
|
|
|
begin
|
|
|
|
SaveDayNames[I] := ShortDayNames[I];
|
|
|
|
ShortDayNames[I] := MyDayNames[I];
|
|
|
|
end;
|
|
|
|
for I := Low(ShortMonthNames) to High(ShortMonthNames) do
|
|
|
|
begin
|
|
|
|
SaveMonthNames[I] := ShortMonthNames[I];
|
|
|
|
ShortMonthNames[I] := MyMonthNames[I];
|
|
|
|
end;
|
|
|
|
Result := FormatDateTime('ddd, d mmm yyyy hh:mm:ss', t);
|
|
|
|
for I := Low(ShortDayNames) to High(ShortDayNames) do
|
|
|
|
ShortDayNames[I] := SaveDayNames[I];
|
|
|
|
for I := Low(ShortMonthNames) to High(ShortMonthNames) do
|
|
|
|
ShortMonthNames[I] := SaveMonthNames[I];
|
|
|
|
end;
|
|
|
|
Result:=Result+' '+Timezone;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-23 23:26:50 +03:00
|
|
|
{CodeInt}
|
2008-04-23 23:28:55 +03:00
|
|
|
function CodeInt(Value:word):string;
|
|
|
|
begin
|
|
|
|
Result := Chr(Hi(Value))+ Chr(Lo(Value))
|
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
|
|
|
|
{DeCodeInt}
|
|
|
|
function DeCodeInt(Value:string;Index:integer):word;
|
2008-04-23 23:26:50 +03:00
|
|
|
var
|
|
|
|
x,y:Byte;
|
|
|
|
begin
|
2008-04-23 23:32:54 +03:00
|
|
|
if Length(Value)>index then x:=Ord(Value[index])
|
2008-04-23 23:28:55 +03:00
|
|
|
else x:=0;
|
2008-04-23 23:32:54 +03:00
|
|
|
if Length(Value)>(Index+1) then y:=Ord(Value[Index+1])
|
2008-04-23 23:28:55 +03:00
|
|
|
else y:=0;
|
|
|
|
Result:=x*256+y;
|
2008-04-23 23:26:50 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-23 23:26:50 +03:00
|
|
|
{IsIP}
|
|
|
|
function IsIP(Value:string):Boolean;
|
|
|
|
var
|
|
|
|
n,x:integer;
|
|
|
|
begin
|
|
|
|
Result:=true;
|
|
|
|
x:=0;
|
|
|
|
for n:=1 to Length(Value) do
|
2008-04-23 23:32:54 +03:00
|
|
|
if not (Value[n] in ['0'..'9','.'])
|
2008-04-23 23:26:50 +03:00
|
|
|
then begin
|
|
|
|
Result:=False;
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
if Value[n]='.' then Inc(x);
|
|
|
|
end;
|
|
|
|
if x<>3 then Result:=False;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-23 23:26:50 +03:00
|
|
|
{ReverseIP}
|
|
|
|
function ReverseIP(Value:string):string;
|
|
|
|
var
|
|
|
|
x:integer;
|
|
|
|
begin
|
|
|
|
Result:='';
|
|
|
|
repeat
|
|
|
|
x:=LastDelimiter('.',Value);
|
2008-04-23 23:32:54 +03:00
|
|
|
Result:=Result+'.'+Copy(Value,x+1,Length(Value)-x);
|
|
|
|
Delete(Value,x,Length(Value)-x+1);
|
2008-04-23 23:26:50 +03:00
|
|
|
until x<1;
|
|
|
|
if Length(Result)>0 then
|
2008-04-23 23:32:54 +03:00
|
|
|
if Result[1]='.' then
|
|
|
|
Delete(Result, 1, 1);
|
2008-04-23 23:26:50 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
{==============================================================================}
|
|
|
|
|
|
|
|
{dump}
|
2008-04-23 23:26:50 +03:00
|
|
|
procedure dump (Buffer:string;DumpFile:string);
|
|
|
|
var
|
|
|
|
n:integer;
|
|
|
|
s:string;
|
|
|
|
f:Text;
|
|
|
|
begin
|
|
|
|
s:='';
|
|
|
|
for n:=1 to Length(Buffer) do
|
|
|
|
s:=s+' +#$'+IntToHex(Ord(Buffer[n]),2);
|
|
|
|
Assignfile(f,DumpFile);
|
|
|
|
if fileexists(DumpFile) then deletefile(PChar(DumpFile));
|
|
|
|
rewrite(f);
|
|
|
|
try
|
|
|
|
writeln(f,s);
|
|
|
|
finally
|
|
|
|
closefile(f);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:48:39 +03:00
|
|
|
{==============================================================================}
|
|
|
|
{SeparateLeft}
|
|
|
|
function SeparateLeft(value,delimiter:string):string;
|
|
|
|
var
|
|
|
|
x:integer;
|
|
|
|
begin
|
|
|
|
x:=pos(delimiter,value);
|
|
|
|
if x<1
|
|
|
|
then result:=trim(value)
|
|
|
|
else result:=trim(copy(value,1,x-1));
|
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
{SeparateRight}
|
|
|
|
function SeparateRight(value,delimiter:string):string;
|
|
|
|
var
|
|
|
|
x:integer;
|
|
|
|
begin
|
|
|
|
x:=pos(delimiter,value);
|
2008-04-23 23:51:17 +03:00
|
|
|
if x>0
|
|
|
|
then x:=x+length(delimiter)-1;
|
2008-04-23 23:48:39 +03:00
|
|
|
result:=trim(copy(value,x+1,length(value)-x));
|
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
{GetParameter}
|
|
|
|
function getparameter(value,parameter:string):string;
|
|
|
|
var
|
2008-04-23 23:49:43 +03:00
|
|
|
x,x1:integer;
|
2008-04-23 23:48:39 +03:00
|
|
|
s:string;
|
|
|
|
begin
|
|
|
|
x:=pos(uppercase(parameter),uppercase(value));
|
|
|
|
result:='';
|
|
|
|
if x>0 then
|
|
|
|
begin
|
|
|
|
s:=copy(value,x+length(parameter),length(value)-(x+length(parameter))+1);
|
|
|
|
s:=trim(s);
|
|
|
|
x1:=length(s);
|
|
|
|
if length(s)>1 then
|
|
|
|
begin
|
|
|
|
if s[1]='"'
|
|
|
|
then
|
|
|
|
begin
|
|
|
|
s:=copy(s,2,length(s)-1);
|
|
|
|
x:=pos('"',s);
|
|
|
|
if x>0 then x1:=x-1;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
x:=pos(' ',s);
|
|
|
|
if x>0 then x1:=x-1;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
result:=copy(s,1,x1);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
{GetEmailAddr}
|
|
|
|
function GetEmailAddr(value:string):string;
|
|
|
|
var
|
|
|
|
s:string;
|
|
|
|
begin
|
|
|
|
s:=separateright(value,'<');
|
|
|
|
s:=separateleft(s,'>');
|
|
|
|
result:=trim(s);
|
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
{GetEmailDesc}
|
|
|
|
function GetEmailDesc(value:string):string;
|
|
|
|
var
|
|
|
|
s:string;
|
|
|
|
begin
|
|
|
|
value:=trim(value);
|
|
|
|
s:=separateright(value,'"');
|
|
|
|
if s<>value
|
|
|
|
then s:=separateleft(s,'"')
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
s:=separateright(value,'(');
|
|
|
|
if s<>value
|
|
|
|
then s:=separateleft(s,')')
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
s:=separateleft(value,'<');
|
|
|
|
if s=value
|
|
|
|
then s:='';
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
result:=trim(s);
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:49:43 +03:00
|
|
|
{==============================================================================}
|
|
|
|
{StrToHex}
|
|
|
|
function StrToHex(value:string):string;
|
|
|
|
var
|
|
|
|
n:integer;
|
|
|
|
begin
|
|
|
|
result:='';
|
|
|
|
for n:=1 to length(value) do
|
|
|
|
Result:=Result+IntToHex(Byte(value[n]),2);
|
|
|
|
result:=lowercase(result);
|
|
|
|
end;
|
2008-04-24 09:40:58 +03:00
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
{IntToBin}
|
|
|
|
function IntToBin(value:integer;digits:byte):string;
|
|
|
|
var
|
|
|
|
x,y,n:integer;
|
|
|
|
begin
|
|
|
|
result:='';
|
|
|
|
x:=value;
|
|
|
|
repeat
|
|
|
|
y:=x mod 2;
|
|
|
|
x:=x div 2;
|
|
|
|
if y>0
|
|
|
|
then result:='1'+result
|
|
|
|
else result:='0'+result;
|
|
|
|
until x=0;
|
|
|
|
x:=length(result);
|
|
|
|
for n:=x to digits-1 do
|
|
|
|
result:='0'+result;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
{BinToInt}
|
|
|
|
function BinToInt(value:string):integer;
|
|
|
|
var
|
|
|
|
x,n:integer;
|
|
|
|
begin
|
|
|
|
result:=0;
|
|
|
|
for n:=1 to length(value) do
|
|
|
|
begin
|
|
|
|
if value[n]='0'
|
|
|
|
then x:=0
|
|
|
|
else x:=1;
|
|
|
|
result:=result*2+x;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-24 09:42:13 +03:00
|
|
|
{==============================================================================}
|
|
|
|
{ParseURL}
|
|
|
|
procedure ParseURL(URL:string; var Prot,User,Pass,Host,Port,Path,Para:string);
|
|
|
|
var
|
|
|
|
x:integer;
|
|
|
|
sURL:string;
|
|
|
|
s:string;
|
|
|
|
s1,s2:string;
|
|
|
|
begin
|
|
|
|
prot:='http';
|
|
|
|
user:='';
|
|
|
|
pass:='';
|
|
|
|
port:='80';
|
|
|
|
para:='';
|
|
|
|
|
|
|
|
x:=pos('://',URL);
|
|
|
|
if x>0 then
|
|
|
|
begin
|
|
|
|
prot:=separateleft(URL,'://');
|
|
|
|
sURL:=separateright(URL,'://');
|
|
|
|
end
|
|
|
|
else sURL:=URL;
|
|
|
|
x:=pos('@',sURL);
|
|
|
|
if x>0 then
|
|
|
|
begin
|
|
|
|
s:=separateleft(sURL,'@');
|
|
|
|
sURL:=separateright(sURL,'@');
|
|
|
|
x:=pos(':',s);
|
|
|
|
if x>0 then
|
|
|
|
begin
|
|
|
|
user:=separateleft(s,':');
|
|
|
|
pass:=separateright(s,':');
|
|
|
|
end
|
|
|
|
else user:=s;
|
|
|
|
end;
|
|
|
|
x:=pos('/',sURL);
|
|
|
|
if x>0 then
|
|
|
|
begin
|
|
|
|
s1:=separateleft(sURL,'/');
|
|
|
|
s2:=separateright(sURL,'/');
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
s1:=sURL;
|
|
|
|
s2:='';
|
|
|
|
end;
|
|
|
|
x:=pos(':',s1);
|
|
|
|
if x>0 then
|
|
|
|
begin
|
|
|
|
host:=separateleft(s1,':');
|
|
|
|
port:=separateright(s1,':');
|
|
|
|
end
|
|
|
|
else host:=s1;
|
|
|
|
x:=pos('?',s2);
|
|
|
|
if x>0 then
|
|
|
|
begin
|
|
|
|
path:='/'+separateleft(s2,'?');
|
|
|
|
para:=separateright(s2,'?');
|
|
|
|
end
|
|
|
|
else path:='/'+s2;
|
|
|
|
if host=''
|
|
|
|
then host:='localhost';
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:32:54 +03:00
|
|
|
{==============================================================================}
|
2008-04-23 23:26:50 +03:00
|
|
|
|
|
|
|
end.
|