2011-06-22 12 views

答えて

5

IPアドレス(実際にはIPv4を意味します)は実際には整数ですが、普通は.で区切られた4つの数字として書かれています。これらの数字のそれぞれは、整数のバイト値を表します。したがって、それぞれの数字は0〜255(incl)の数字でなければなりません。

function CheckIP(Input: String): Cardinal; 
var 
    IP: Cardinal; 
    i : Integer; 
    Part: Integer; 
    PartValue: Cardinal; 
    PartValid: Boolean; 
begin 
    Part := 3; 
    PartValue := 0; 
    PartValid := False; 
    IP := 0; 
    { When a '.' is encountered, the previous part is processed. Force processing } 
    { the last part by adding a '.' to the input. } 
    Input := Input + '.'; 

    for i := 1 to Length(Input) do 
    begin 

    { Check next character } 
    if Input[i] = '.' then 
    begin 

     if PartValue <= 255 then 
     begin 
     if PartValid then 
     begin 
      { A valid part is encountered. Put it in the result. } 
      IP := IP or (PartValue shl (Part * 8)); 
      { Stop processing if this is the last '.' we put in ourselves. } 
      if i = Length(Input) then 
      Break; 
      { Else reset the temporary values. } 
      PartValid := False; 
      PartValue := 0; 
      Dec(Part); 
     end 
     else 
      RaiseException('Empty part'); 
     end 
     else 
     RaiseException('Part not within 0..255'); 

    end 
    else if ((Input[i] >= '0') and (Input[i] <= '9')) then 
    begin 

     { A digit is found. Add it to the current part. } 
     PartValue := PartValue * 10 + Cardinal((Ord(Input[i]) - Ord('0'))); 
     PartValid := True; 

    end 
    else 
    begin 

     { Any other character raises an exception } 
     RaiseException('Invalid character'); 

    end; 

    { If part < 0, we processed too many dots. } 
    if Part < 0 then 
     RaiseException('Too many dots'); 

    end; 

    { Check if we found enough parts. } 
    if Part > 0 then 
    RaiseException('Address most consist of 4 numbers'); 

    { If Part is not valid after the loop, the input ended in a dot. } 
    if not PartValid then 
    RaiseException('IP cannot end in a dot'); 

    { Return the calculated IP address as a cardinal. } 
    Result := IP; 
end; 
+0

私はそれを承知しています。私はそれをどのように行うことができますか、パスカルで質問しています。 –

+0

私はこのInnoセットアップスクリプトを知らないので、Delphiで関数を書いて、Delphi固有の関数を残そうとしました。関数は入力文字列をチェックし、IPアドレスを基数として返します。エラーが見つかった場合、この関数は例外をスローします。この結果を使用して、IPが特定の範囲内にあるかどうかをさらに確認できます。このスクリプトが例外をサポートしていない場合は、関数をTrueまたはFalseに戻し、* out *パラメータでIPを返すような別の解決策を見つける必要があります。 – GolezTrol

+0

Delphiの場合、このコードは非常に複雑になり、非効率的になります(例えば、charでcharをループする必要はありません)。 – TLama

2

私は、コードを修正し、今ではあなたはInno Setupのでそれを使用することができます。

//Validate an IPv4 address 
function ValidateIP(
    Input: String 
): Boolean; 
var 
    InputTemp : String; 
    IP: Cardinal; 
    i : Integer; 
    Part: Integer; 
    PartValue: Cardinal; 
    PartValid: Boolean; 
begin 
    InputTemp := Input; 
    Result := True; 

    Part := 3; 
    PartValue := 0; 
    PartValid := False; 
    IP := 0; 
    // When a '.' is encountered, the previous part is processed. Force processing 
    // the last part by adding a '.' to the input. 
    Input := Input + '.'; 

    for i := 1 to Length(Input) do 
    begin 
    // Check next character 
    if Input[i] = '.' then 
    begin 

     if PartValue <= 255 then 
     begin 
     if PartValid then 
     begin 
      // A valid part is encountered. Put it in the result. 
      IP := IP or (PartValue shl (Part * 8)); 
      // Stop processing if this is the last '.' we put in ourselves. 
      if i = Length(Input) then 
      Break; 
      // Else reset the temporary values. 
      PartValid := False; 
      PartValue := 0; 
      Dec(Part); 
     end 
     else 
      Result := False; 
     end 
     else 
     Result := False; 

    end 
    else if ((((Ord(Input[i]) - Ord('0'))) >= 0) and ((Ord(Input[i]) - Ord('0')) <= 9)) then 
    begin 
     // A digit is found. Add it to the current part. 
     PartValue := PartValue * 10 + Cardinal((Ord(Input[i]) - Ord('0'))); 
     PartValid := True; 
    end 
    else 
    begin 
     // Any other character 
    Result := False; 
    end; 
    // If part < 0, we processed too many dots. 
    if Part < 0 then 
     Result := False; 
    end; 
    // Check if we found enough parts. 
    if Part > 0 then 
    Result := False; 
    // If Part is not valid after the loop, the input ended in a dot. 
    if not PartValid then 
    Result := False; 
end; 
+0

偉大な、ありがとう! – GolezTrol

+0

Windowsで文字列をIPアドレスに変換する方法を知っている人は誰ですか?それはWinsockです。それらの狂った点線または点線のない8進数、16進数およびそれらの混合表記を変換することができます。 'RtlIpv4StringToAddress'機能は改善されましたが、Vistaで追加されたため、私の[リンクコード](http://pastebin.com/wyPr923P)で' inet_addr'を使用しました。 – TLama