DELPHI直接分析Oracle tnsnames.ora提取服务器列表

 

function TForm1.GetTNS:TStrings;
var
  ARegistry:TRegistry;
  froot:string;
  Mylist:TStringList;
  FileStr,dbname,c:String;
  i,Stack,Comment:Integer;
begin
  ARegistry:=TRegistry.Create;
  ARegistry.RootKey:=hkey_local_machine;
  if ARegistry.OpenKey('Software',false) then
  begin
    ARegistry.OpenKey('oracle',false);
    froot:=ARegistry.ReadString('VOBHOME2.0')+'/NETWORK/ADMIN/tnsnames.ora';
  end;
  Mylist:=TstringList.Create;
  Mylist.LoadFromFile(froot);
  FileStr:=MyList.Text;
  MyList.Clear;
  FileStr:=StringReplace(FileStr,' ','',[rfReplaceAll]);
  FileStr:=StringReplace(FileStr,#10,'',[rfReplaceAll]);

  dbname:='';
  Stack:=0;             //括号是成对出现的,所以利用栈的思想来处理  '(' stack+1    ')' stack-1
  Comment:=0;           //comment来处理是否是注释行
  for i:=1 to Length(FileStr) do
  begin
    c:=FileStr[i];
    if c='#' then
     Comment:=1
    else if c=#$D then
      Comment:=0
    else if (comment=0) and (c='(') then
      Stack:=Stack+1
    else if (comment=0) and (c=')') then
      Stack:=Stack-1
    else if (Comment=0) and (Stack=0) and (c='=') then
    begin
      MyList.Add(dbname);
      dbname:='';
    end
    else if (Comment=0) and (Stack=0) then
      dbname:=dbname+c;
  end;
  Result:=MyList;
end;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值