Re: IsValidIP ?

Giganews Newsgroups
Subject: Re: IsValidIP ?
Posted by:  Remy Lebeau \(Indy Team\) (no.spam@no.spam.com)
Date: Mon, 19 Oct 2009

<us…@domain.invalid> wrote in message
news:9668B640B094E340us…@domain.invalid...

> IsValidIP('1.2.2.3.localhost')

IsValidIP() is a legacy function that only supports IPv4 addresses, which
your example is not.  Nothing in Indy actually uses that function anymore.
If you are using Indy 10 (Indy 9 does not support IPv6), you can use the
TIdIPAddress class instead to support both IPv4 and IPv6 addresses, ie:

IP := TIdIPAddress.MakeAddressObject('1.2.2.3.localhost');
if IP <> nil then
begin
    ...
end else
begin
  ...
end;

I have marked IsValidIP() as deprecated now.

> +    if ((i = 4) and (LTmp <> '')) then Exit;

It makes more sense to perform that check outside the loop instead, ie:

function IsValidIP(const S: String): Boolean;
var
  j, i: Integer;
  LTmp: String;
begin
  Result := False;
  LTmp := Trim(S);
  for i := 1 to 4 do begin
    j := IndyStrToInt(Fetch(LTmp, '.'), -1);    {Do not Localize}
    if (j < 0) or (j >= 256) then begin
      Exit;
    end;
  end;
  if LTmp <> '' then begin
    Exit;
  end;
  Result := True;
end;

--
Remy Lebeau (Indy Team)

Replies

None

In response to

IsValidIP ? posted by us…@domain.invalid on Fri, 16 Oct 2009