MaskEdit에서 ip 입력

2013. 2. 19. 10:11언어/C++ Builder

보기만 하고 테스트 해보지 않음.. 


__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  // Initialize the mask edit boxes to contain the empty string.
  MaskEdit1->Text = "";
  MaskEdit2->Text = "";
  MaskEdit3->Text = "";
 
  // Use the Courier font in the mask edit boxes.
  MaskEdit1->Font->Name = "Courier";
  MaskEdit2->Font->Name = "Courier";
  MaskEdit3->Font->Name = "Courier";
 
  // Initialize the mask of each mask edit box
  // according to IP address coding conventions.
  MaskEdit1->EditMask = "!099.099.099.099;1; ";
  MaskEdit2->EditMask = "!099.099.099.099;1; ";
  MaskEdit3->EditMask = "!099.099.099.099;1; ";
 
  MaskEdit2->OnExit = MaskEdit2Exit;
}
 
void __fastcall TForm1::MaskEdit2Exit(TObject *Sender)
{
  // Extract the net and host address from the IP.
  String IP = MaskEdit1->Text;
  int net1 = IP.SubString(0, 3).TrimRight().ToInt();
  int net2 = IP.SubString(5, 3).TrimRight().ToInt();
  int host1 = IP.SubString(9, 3).TrimRight().ToInt();
  int host2 = IP.SubString(13, 3).TrimRight().ToInt();
 
  // A range test that you cannot validate through edit masks
  if (net1 < 0 || net1 > 255 ||
      net2 < 0 || net2 > 255 ||
      host1 < 0 || host1 > 255 ||
      host2 < 0 || host2 > 255)
    throw(Exception("Not a valid IP address."));
 
  // Extract the net and host mask from the subnet mask.
  String mask = MaskEdit2->Text;
  int netmask1 = mask.SubString(0, 3).TrimRight().ToInt();
  int netmask2 = mask.SubString(5, 3).TrimRight().ToInt();
  int hostmask1 = mask.SubString(9, 3).TrimRight().ToInt();
  int hostmask2 = mask.SubString(13, 3).TrimRight().ToInt();
 
  // A range test that you cannot validate through edit masks
  if (netmask1 < 0 || netmask1 > 255 ||
      netmask2 < 0 || netmask2 > 255 ||
      hostmask1 < 0 || hostmask1 > 255 ||
      hostmask2 < 0 || hostmask2 > 255)
    throw(Exception("Not a valid subnet mask."));
 
  // Compute the subnet address using bit-wise AND.
  int sub_net1 = net1 & netmask1;
  int sub_net2 = net2 & netmask2;
  int sub_host1 = host1 & hostmask1;
  int sub_host2 = host2 & hostmask2;
 
  // Display the subnet address.
  MaskEdit3->Text =
    String(sub_net1) + "." + String(sub_net2) + "." +
    String(sub_host1) + "." + String(sub_host2);
}