pascal 一些常用函数

维护的一个iss打包脚本,里面需要挺多校验和一些常规操作的函数,所以很多函数都是自己封装再调用,这样以后维护起来比较方便,改动也比较小,罗列下总结的一些比较通用的函数:

//判断是否需要安装某软件
function NeedInstallsoft(): Boolean;
var
 version: Cardinal;
begin
 result := false;
 //32位或64位都没有,才安装
 if (RegQueryDWordValue(HKLM, 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{appid}', 'Version', version) = false) and (RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{appid}', 'Version', version) = false) then begin
  result := true;//要安装
   end;
end;

//将java路径加到系统环境变量中
function JavaEnvirUpdate() : Boolean;
var
    javaPath,EnvirPath:String;
begin
    javaPath:= ExpandConstant('c:\java\bin;');
    RegQueryStringValue(HKLM, 'System\CurrentControlSet\Control\Session Manager\Environment', 'Path', EnvirPath);
    if pos(javaPath,EnvirPath) = 0 then begin
      RegWriteStringValue(HKLM,'System\CurrentControlSet\Control\Session Manager\Environment','Path', javaPath+EnvirPath);
    end;
end;

//把TArrayOfString转成string的函数
function TArrayOfStringToString(svArray:TArrayOfString): String;
var
    nLines,i:Integer;
begin
 Result := '';
 nLines := GetArrayLength(svArray);
    for i := 0 to nLines - 1 do
    begin
        Result := Result+svArray[i];
    end;
end;

//修改 TArrayOfString 中某一个字符串的函数
function TArrayOfStringChanges(svArray:TArrayOfString; FromStr, ToStr: String): TArrayOfString;
var
    nLines,i:Integer;
    tempStr:String;
begin
    nLines := GetArrayLength(svArray);
    for i := 0 to nLines - 1 do
    begin
       tempStr := svArray[i];
       if ( Pos(FromStr, tempStr)>0 ) then
       begin
         StringChangeEx(svArray[i], FromStr, ToStr, True);
       end;     
    end;
    Result := svArray;
end;

//获取文件中指定标识符之间的内容
function GetFileSectionContent(fileBuffer:TArrayOfString;startSection,endSection:String): String;
var
   RetValue,startpos,copycount : Integer;
   filestring:string;
begin
   filestring := TArrayOfStringToString(fileBuffer);
   RetValue :=  Pos(startSection,filestring);
   if RetValue = 0 then begin
       exit;
   end;
   startpos := RetValue + length(startSection);

   RetValue :=  Pos(endSection,filestring);
   if RetValue = 0 then begin
       exit;
   end;
   copycount := RetValue - startpos;
   Result := Copy(filestring,startpos,copycount);
end;

//刪除字符串中空格的函數
function DelStringSpace(starstring:String): String;
begin
 while pos(' ',starstring)<>0 do begin
  delete(starstring,pos(' ',starstring),1);
 end;
 Result := starstring;
end;

//搜索一个字符出现的次数
function calStringNumber(calstring,stringfile: String): Integer;
var
 RetValue:Integer;
begin
 Result := 0;
 RetValue :=  Pos(calstring,stringfile);
  while (RetValue > 0) do begin
  Result := Result+1;
  Delete(stringfile,1,RetValue+length(calstring)-1);
        RetValue :=  Pos(calstring,stringfile);
  end
end;

//查打指定端口是否被占用
function CheckTCPPortAvaliable(Port: String): Boolean;
var 
 RunStateContent,IpString,IpStringTotal: String;
 RetValue,PortSign,i,IpRetValue: Integer;
    RunStateContentarr:TArrayOfString;
begin
    Result := true;
    Exec('cmd',ExpandConstant('/C netstat -na | findstr  "'+Port+'"|findstr /r "LISTENING CLOSE_WAIT ESTABLISHED TIME_WAIT" >c:\VMtcp.txt'), '', SW_HIDE,ewWaitUntilTerminated, RetValue);
    LoadStringsFromFile('c:\VMtcp.txt',RunStateContentarr);
    RunStateContent := TArrayOfStringToString(RunStateContentarr);
 deleteFile('c:\VMtcp.txt');   
    IpStringTotal := strArr;
    IpRetValue :=  Pos('|',IpStringTotal);
    while (IpRetValue > 0) do begin
        IpString := Copy(IpStringTotal,1,IpRetValue-1);
        Delete(IpStringTotal,1,IpRetValue);
        PortSign := pos(IpString+':'+Port,RunStateContent);
        if PortSign> 0 then begin
          Result := false;
          exit;
        end;
        IpRetValue :=  Pos('|',IpStringTotal);
    end;
end;

//增加IP地址校验函数
function IsValidIP(strIP: string): Boolean;
var
  i,j,PartCount: Integer;
  Part: string;
begin
  PartCount := 0;
  while strIP <> '' do
  begin
    i := Pos('.', strIP);
    if i = 0 then
    begin
      Part := strIP;
      strIP := '';
    end
    //如果取第四位数值了还有点,说明输入有问题
    //bug id:15828  2017.7.10 by yelu ces3.11.1.0
    else if (PartCount = 3) and (i <> 0) then
    begin
      Result := False;
      Exit;
    end
    else
    begin
      Part := Copy(strIP, 1, i - 1);
      Delete(strIP, 1, i);
    end;
    //判断字符中是否有空格
 //StrToIntDef返回-1说明part这个字符串不能正常转换成整数
 if (Pos(' ', Part) > 0) or (StrToIntDef(Part,-1) = -1)then
 begin
  Result := False;
  Exit;
 end
    else
    begin
      j := StrToIntDef(Part,-1);
    end;
    if (j < 0) or (j > 255) then
    begin
      Result := False;
      Exit;
    end;
    PartCount := PartCount+1;
  end;
  Result := PartCount = 4; //必须要有4段
end;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值