[fpc-pascal] Return value of StrToHostAddr and StrToHostAddr6 in sockets unit

Noel Duffy noelduffy at xtra.co.nz
Tue May 12 23:05:23 CEST 2020


On 13/05/20 12:56 am, Marco van de Voort wrote:
> 
> Op 2020-05-12 om 12:32 schreef Michael Van Canneyt:
>>
>>> The names I use here are the libc names, which many other languages 
>>> also use.
>>>
>>> Now, StrToHostAddr and StrToHostAddr6 can call the appropriate 
>>> function, and programs that use those functions won't notice any 
>>> change. But new code will be able to use the new functions and have 
>>> an unambiguous answer as to whether the address is valid or not.
>>>
>>> The change is quite straightforward to make, and it won't break 
>>> existing code, so I think it's worthwhile to do.
>>>
>>> Any thoughts or opinions on this?
>>
>> I'm all for it, but please use the following names:
>>
>> function TryStrToHostAddr(IP: String; var ip4: in_addr): Boolean;
>> function TryStrToHostAddr6(IP: String; var ip6: in6_addr): Boolean;
>>
>> Rationale for this request: All conversion calls in sysutils and 
>> dateutils use this naming scheme, it's good for consistency.
>>
>> (and I hate underscores, but that's beside the point ;))
>>
> Yes to the principle and yes to the TryStrTo naming. But I would make 
> the IP param CONST ;-)

Good idea, and normally I would, but the current implementation of 
StrToHostAddr (see below) does Delete on IP as it processes it. As it 
currently stands this function must accept a non-const.

function StrToHostAddr(IP : AnsiString) : in_addr ;

Var
     Dummy : AnsiString;
     I,j,k     : Longint;
     Temp : in_addr;

begin
   strtohostaddr.s_addr:=0;              //:=NoAddress;
   For I:=1 to 4 do
     begin
       If I<4 Then
         begin
           J:=Pos('.',IP);
           If J=0 then
             exit;
           Dummy:=Copy(IP,1,J-1);
           Delete (IP,1,J);
         end
        else
          Dummy:=IP;
       Val (Dummy,k,J);
       array4int(temp.s_addr)[i]:=k;
       If J<>0 then Exit;
    end;
    strtohostaddr.s_addr:=ntohl(Temp.s_addr);
end;





More information about the fpc-pascal mailing list