Changeset 19


Ignore:
Timestamp:
May 14, 2010, 6:59:30 AM (15 years ago)
Author:
george
Message:
  • Upraveno: Robustnější dekódování z řetězce.
  • Přidáno: Metoda Assign pro přiřazení hodnoty do jiné proměnné.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • IPAddress/UIPAddress.pas

    r17 r19  
    11unit UIPAddress;
     2
     3// Date: 2010-05-14
    24
    35{$mode objfpc}{$H+}
     
    1921    procedure SetString(Value: string);
    2022    constructor Create;
     23    procedure Assign(Source: TObject);
    2124    property AsString: string read GetString write SetString;
    2225  end;
     
    4144var
    4245  Part: string;
     46  Octet: Integer;
     47  TempValue: Integer;
    4348begin
    4449  if Pos('/', Value) > 0 then begin
    45     Prefix := StrToInt(Copy(Value, Pos('/', Value) + 1, Length(Value)));
     50    if TryStrToInt(Copy(Value, Pos('/', Value) + 1, Length(Value)), TempValue) then
     51      Prefix := TempValue else Prefix := 32;
    4652    Delete(Value, Pos('/', Value), Length(Value));
    4753  end else Prefix := 32;
    48   Part := Copy(Value, 1, Pos('.', Value) - 1);
    49   Delete(Value, 1, Pos('.', Value));
    50   Address := Cardinal(StrToInt(Part) and $ff) shl 24;
    51   Part := Copy(Value, 1, Pos('.', Value) - 1);
    52   Delete(Value, 1, Pos('.', Value));
    53   Address := Address or Cardinal((StrToInt(Part) and $ff) shl 16);
    54   Part := Copy(Value, 1, Pos('.', Value) - 1);
    55   Delete(Value, 1, Pos('.', Value));
    56   Address := Address or Cardinal((StrToInt(Part) and $ff) shl 8);
    57   Address := Address or Cardinal((StrToInt(Value) and $ff) shl 0);
     54  if Pos('.', Value) > 0 then begin
     55    Part := Copy(Value, 1, Pos('.', Value) - 1);
     56    Delete(Value, 1, Pos('.', Value));
     57    if TryStrToInt(Part, TempValue) then Octet := TempValue
     58      else Octet := 0;
     59    Address := Address or Cardinal((Octet and $ff) shl 24);
     60  end;
     61  if Pos('.', Value) > 0 then begin
     62    Part := Copy(Value, 1, Pos('.', Value) - 1);
     63    Delete(Value, 1, Pos('.', Value));
     64    if TryStrToInt(Part, TempValue) then Octet := TempValue
     65      else Octet := 0;
     66    Address := Address or Cardinal((Octet and $ff) shl 16);
     67  end;
     68  if Pos('.', Value) > 0 then begin
     69    Part := Copy(Value, 1, Pos('.', Value) - 1);
     70    Delete(Value, 1, Pos('.', Value));
     71    if TryStrToInt(Part, TempValue) then Octet := TempValue
     72      else Octet := 0;
     73    Address := Address or Cardinal((Octet and $ff) shl 8);
     74  end;
     75  if TryStrToInt(Value, TempValue) then Octet := TempValue
     76    else Octet := 0;
     77  Address := Address or Cardinal((Octet and $ff) shl 0);
    5878end;
    5979
     
    6484end;
    6585
     86procedure TIPAddress.Assign(Source: TObject);
     87begin
     88  if Source is TIPAddress then begin
     89    Address := TIPAddress(Source).Address;
     90    Prefix := TIPAddress(Source).Prefix;
     91  end;
     92end;
     93
    6694end.
    6795
Note: See TracChangeset for help on using the changeset viewer.