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;

你可能感兴趣的:(oracle,c,String,function,服务器,Delphi)