www.pudn.com > MailServer.rar > Functions.pas


unit Functions; 
 
interface 
 
uses 
  Windows, SysUtils, DateUtils, ShlObj, ActiveX, ComObj, Registry; 
 
const 
  C1: Integer = 38784345; //初始化加解密钥的两个因子 
  C2: Integer = 7342294; 
  BaseTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; //这是Base64转换用的常量字符 
 
function getuserid: string; //得到一个用户的在线20位ID识别号(string) 
function Replacing(S, source, target: string; times: Integer): string; //替换指定的字符串(源串,被换的串,换成为,多少次[-1--所有]) 
function DeleteSubString(S, substring: string; Times: Integer; CaseSensivte: Boolean): string; //删除指定字串,Times:指定次数[-1--所有],CaseSensivte:大小写是否敏感 
function EncodeBase64(Source:string):string; 
function DecodeBase64(Source:string):string; 
function GetFileSize(filename:string):integer; //用findfrist方法得到一个文件的大小 
 
implementation 
 
 
function getuserid: string; 
begin 
  Randomize; 
  result := formatdatetime('mmddhhnnsszzz', now) + FormatFloat('0000000', Random(9999999)); 
end; 
 
function Replacing(S, Source, Target: string; Times: Integer): string; 
var 
  Site, StrLen, i: integer; 
begin 
  if Source <> Target then 
  begin 
    {source在S中出现的位置} 
    StrLen := Length(Source); 
    {source的长度} 
    Site := Pos(Source, S); 
    i := Times; 
    while Site >= 1 do 
    begin 
      {删除source字符串} 
      Delete(S, Site, StrLen); 
      {插入target字符串到S中} 
      Insert(Target, S, Site); 
      {返回新串} 
      Site := Pos(Source, s); 
      dec(i); 
      if i = 0 then 
        break; 
    end; 
  end; 
  Result := S; 
end; 
 
function DeleteSubString(S, substring: string; Times: Integer; CaseSensivte: Boolean): string; 
var 
  CharPos, l, i: Integer; 
begin 
  l := Length(SubString); 
  if CaseSensivte then 
    CharPos := Pos(SubString, S) 
  else 
    CharPos := Pos(UpperCase(SubString), UpperCase(S)); 
  i := Times; 
  while CharPos > 0 do 
  begin 
    Delete(S, CharPos, l); 
    dec(i); 
    if i = 0 then 
      break; 
    if CaseSensivte then 
      CharPos := Pos(SubString, S) 
    else 
      CharPos := Pos(UpperCase(SubString), UpperCase(S)); 
  end; 
  Result := S; 
end; 
 
function EncodeBase64(Source:string):string; 
var 
  Times, LenSrc, i: Integer; 
  x1, x2, x3, x4: Char; 
  xt: Byte; 
begin 
  Result := ''; 
  LenSrc := Length(Source); 
  if LenSrc mod 3 = 0 then 
    Times := LenSrc div 3 
  else 
    Times := LenSrc div 3 + 1; 
  for i := 0 to Times - 1 do 
  begin 
    if LenSrc >= (3 + i * 3) then 
    begin 
      x1 := BaseTable[(ord(Source[1 + i * 3]) shr 2)+1]; 
      xt := (ord(Source[1 + i * 3]) shl 4) and 48; 
      xt := xt or (ord(Source[2 + i * 3]) shr 4); 
      x2 := BaseTable[xt + 1]; 
      xt := (Ord(Source[2 + i * 3]) shl 2) and 60; 
      xt := xt or (Ord(Source[3 + i * 3]) shr 6); 
      x3 := BaseTable[xt + 1]; 
      xt := (ord(Source[3 + i * 3]) and 63); 
      x4 := BaseTable[xt + 1]; 
    end 
    else if LenSrc >= (2 + i * 3) then 
    begin 
      x1 := BaseTable[(Ord(Source[1 + i * 3]) shr 2) + 1]; 
      xt := (Ord(Source[1 + i * 3]) shl 4) and 48; 
      xt := xt or (Ord(Source[2 + i * 3]) shr 4); 
      x2 := BaseTable[xt + 1]; 
      xt := (Ord(Source[2 + i * 3]) shl 2) and 60; 
      x3 := BaseTable[xt + 1]; 
      x4 := '='; 
    end else 
    begin 
      x1 := BaseTable[(Ord(Source[1 + i * 3]) shr 2)+1]; 
      xt := (Ord(Source[1 + i * 3]) shl 4) and 48; 
      x2 := BaseTable[xt + 1]; 
      x3 := '='; 
      x4 := '='; 
    end; 
    Result := Result + x1 + x2 + x3 + x4; 
  end; 
end; 
 
function DecodeBase64(Source:string):string; 
var 
  SrcLen,Times,i:integer; 
  x1,x2,x3,x4,xt:byte; 
begin 
  result:=''; 
  SrcLen:=Length(Source); 
  Times:=SrcLen div 4; 
  for i:=0 to Times-1 do 
  begin 
    x1:=Pos(Source[1+i*4],BaseTable)-1; 
    x2:=Pos(Source[2+i*4],BaseTable)-1; 
    x3:=Pos(Source[3+i*4],BaseTable)-1; 
    x4:=Pos(Source[4+i*4],BaseTable)-1; 
    x1:=x1 shl 2; 
    xt:=x2 shr 4; 
    x1:=x1 or xt; 
    x2:=x2 shl 4; 
    result:=result+chr(x1); 
    if x3= 64 then break; 
    xt:=x3 shr 2; 
    x2:=x2 or xt; 
    x3:=x3 shl 6; 
    result:=result+chr(x2); 
    if x4=64 then break; 
    x3:=x3 or x4; 
    result:=result+chr(x3); 
  end; 
end; 
 
function GetFileSize(filename:string):integer; //用findfrist方法得到一个文件的大小 
var 
  sr: TSearchRec; 
begin 
result:=0; 
if FindFirst(filename, faAnyFile-faDirectory, sr) = 0 then 
result:=sr.Size; 
end; 
end.