Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.056 Beiträge
 
Delphi 12 Athens
 
#11

AW: IP-Adresse von String nach Integer wandeln

  Alt 13. Sep 2010, 16:52
siehe Class Operator Implicit(Value: String): TIP; läßt sich über PosEx bestimmt noch optimieren

Delphi-Quellcode:
Const WC_IPADDRESS               = 'SysIPAddress32';

  // IP Address Edit Control Messages
  IPM_CLEARADDRESS               = WM_USER + 100;
  IPM_SETADDRESS                 = WM_USER + 101;
  IPM_GETADDRESS                 = WM_USER + 102;
  IPM_SETRANGE                   = WM_USER + 103;
  IPM_SETFOCUS                   = WM_USER + 104;
  IPM_ISBLANK                    = WM_USER + 105;

  // IP Address Edit Control Notification Codes
  IPN_FIELDCHANGED               = IPN_FIRST - 0;

Type NMIPADDRESS = packed Record {IPN_FIELDCHANGED}
    hdr:    NMHDR;
    iField: _INT;
    iValue: _INT;
  End;
  pNMIPADDRESS = ^NMIPADDRESS;

  TIP = packed Record
  Private
    _IP: packed Record  Case Integer of
      0: (B: Array[0..3] of Byte);
      1: (L: LongInt);
    End;
    Procedure _SetIP (IP: LongInt);               Inline;
    Function  _GetIP:     LongInt;                Inline;
    Procedure _SetIPx(Index: Integer; IPx: Byte); Inline;
    Function  _GetIPx(Index: Integer):     Byte;  Inline;
  Public
    Class Operator Implicit          (Value:       LongInt): TIP;     Inline;
    Class Operator Implicit          (Value:       String):  TIP;
    Class Operator Implicit          (Rec:         TIP):     LongInt; Inline;
    Class Operator Implicit          (Rec:         TIP):     String;
    Class Operator Equal             (Left, Right: LongInt): Boolean; Inline;
    Class Operator NotEqual          (Left, Right: LongInt): Boolean; Inline;
    Class Operator LessThanOrEqual   (Left, Right: LongInt): Boolean; Inline;
    Class Operator LessThan          (Left, Right: LongInt): Boolean; Inline;
    Class Operator GreaterThan       (Left, Right: LongInt): Boolean; Inline;
    Class Operator GreaterThanOrEqual(Left, Right: LongInt): Boolean; Inline;

    Property IP: LongInt Read _GetIP Write _SetIP;
    Property IPx[Index: Integer]: Byte Read _GetIPx Write _SetIPx;
  End;

Function MAKEIPRANGE     (Low, High: BYTE): DWORD;      Inline;
Function MAKEIPADDRESS   (B1, B2, B3, B4: BYTE): DWORD; Inline;
Function FIRST_IPADDRESS (X: DWORD): BYTE;              Inline;
Function SECOND_IPADDRESS(X: DWORD): BYTE;              Inline;
Function THIRD_IPADDRESS (X: DWORD): BYTE;              Inline;
Function FOURTH_IPADDRESS(X: DWORD): BYTE;              Inline

Delphi-Quellcode:
Procedure TIP._SetIP(IP: LongInt);
  Begin
    _IP.L := IP;
  End;

Function TIP._GetIP: LongInt;
  Begin
    Result := _IP.L;
  End;

Procedure TIP._SetIPx(Index: Integer; IPx: Byte);
  Begin
    _IP.B[3 - Index] := IPx;
  End;

Function TIP._GetIPx(Index: Integer): Byte;
  Begin
    Result := _IP.B[3 - Index];
  End;

Class Operator TIP.Implicit(Value: LongInt): TIP;
  Begin
    Result._IP.L := Value;
  End;

Class Operator TIP.Implicit(Value: String): TIP;
  Var i, i2, i3, i4: Integer;

  Begin
    i := 3;
    Repeat
      i2 := Pos('.', Value) - 1;
      If i2 < 0 Then i2 := 99;
      Val(Copy(Value, 1, i2), i3, i4);
      If i4 <> 0 Then i3 := 0;
      Result._IP.B[i] := i3;
      Delete(Value, 1, i2 + 1);
      Dec(i);
    Until i < 0;
  End;

Class Operator TIP.Implicit(Rec: TIP): LongInt;
  Begin
    Result := Rec._IP.L;
  End;

Class Operator TIP.Implicit(Rec: TIP): String;
  Var i: Integer;
    S: Array[0..3] of String;

  Begin
    For i := 3 downto 0 do Str(Rec._IP.B[i], S[i]);
    Result := S[3] + '.' + S[2] + '.' + S[1] + '.' + S[0];
  End;

Class Operator TIP.Equal(Left, Right: LongInt): Boolean;
  Begin
    Result := Left = Right;
  End;

Class Operator TIP.NotEqual(Left, Right: LongInt): Boolean;
  Begin
    Result := Left <> Right;
  End;

Class Operator TIP.LessThanOrEqual(Left, Right: LongInt): Boolean;
  Begin
    Result := ByteSwap(Left) <= ByteSwap(Right);
  End;

Class Operator TIP.LessThan(Left, Right: LongInt): Boolean;
  Begin
    Result := ByteSwap(Left) < ByteSwap(Right);
  End;

Class Operator TIP.GreaterThan(Left, Right: LongInt): Boolean;
  Begin
    Result := ByteSwap(Left) > ByteSwap(Right);
  End;

Class Operator TIP.GreaterThanOrEqual(Left, Right: LongInt): Boolean;
  Begin
    Result := ByteSwap(Left) >= ByteSwap(Right);
  End;

Function MAKEIPRANGE(Low, High: BYTE): DWORD;
  Begin
    Result := (LongInt(High) shl 8) or low;
  End;

Function MAKEIPADDRESS(B1, B2, B3, B4: BYTE): DWORD;
  Begin
    Result := ((((((LongInt(B1) shl 8) or B2) shl 8) or B3) shl 8) or B4);
  End;

Function FIRST_IPADDRESS(X: DWORD): BYTE;
  Begin
    Result := x shr 24;
  End;

Function SECOND_IPADDRESS(X: DWORD): BYTE;
  Begin
    Result := x shr 16;
  End;

Function THIRD_IPADDRESS(X: DWORD): BYTE;
  Begin
    Result := x shr 8;
  End;

Function FOURTH_IPADDRESS(X: DWORD): BYTE;
  Begin
    Result := x;
  End;
Neuste Erkenntnis:
Seit Pos einen dritten Parameter hat,
wird PoSex im Delphi viel seltener praktiziert.

Geändert von himitsu (13. Sep 2010 um 16:59 Uhr)
  Mit Zitat antworten Zitat