source: Network/IPAddress/UIPAddress.pas

Last change on this file was 33, checked in by george, 14 years ago
  • Přidáno: Zásobník vláken a resetovatelné vlákno.
  • Přidáno: Podsložka Network pro síťově orientované knihovny. Knihovny TCPServer a NetworkAddress.
File size: 2.5 KB
Line 
1unit UIPAddress;
2
3// Date: 2010-05-14
4
5{$mode objfpc}{$H+}
6
7interface
8
9uses
10 Classes, SysUtils;
11
12type
13
14 { TIPAddress }
15
16 TIPAddress = class
17 Address: Cardinal;
18 Prefix: Byte;
19 function GetNetMask: Cardinal;
20 function GetString: string;
21 procedure SetString(Value: string);
22 constructor Create;
23 procedure Assign(Source: TObject);
24 property AsString: string read GetString write SetString;
25 end;
26
27implementation
28
29{ TIPAddress }
30
31function TIPAddress.GetNetMask: Cardinal;
32begin
33 Result := Cardinal((1 shl (32 - Prefix)) - 1) xor $ffffffff;
34end;
35
36function TIPAddress.GetString: string;
37begin
38 Result := IntToStr((Address shr 24) and $ff) + '.' + IntToStr((Address shr 16) and $ff) + '.' +
39 IntToStr((Address shr 8) and $ff) + '.' + IntToStr((Address shr 0) and $ff);
40 if(Prefix <> 32) then Result := Result + '/' + IntToStr(Prefix);
41end;
42
43procedure TIPAddress.SetString(Value: string);
44var
45 Part: string;
46 Octet: Integer;
47 TempValue: Integer;
48begin
49 if Pos('/', Value) > 0 then begin
50 if TryStrToInt(Copy(Value, Pos('/', Value) + 1, Length(Value)), TempValue) then
51 Prefix := TempValue else Prefix := 32;
52 Delete(Value, Pos('/', Value), Length(Value));
53 end else Prefix := 32;
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);
78end;
79
80constructor TIPAddress.Create;
81begin
82 Address := 0;
83 Prefix := 32;
84end;
85
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
94end.
95
Note: See TracBrowser for help on using the repository browser.