#2, InvalidProperty - Решил старым, дедовским способом, однако хотелось бы услышать комментарии по этому коду.
| Код | #include <Windows.h> #include <stdio.h> //--------------------------------------------------------------------------- class TCheckIPv4 { private: bool IsCorrectIPv4Part(char *cip, int len); bool IsCorrectIPv4PartCompare(char *ip1, int len1, char *ip2, int len2); bool GetIPParts(char *cIP, int len, char *ip1, char *ip2, char *ip3, char *ip4); public: TCheckIPv4(); ~TCheckIPv4(); // ---- bool IsCorrectIPv4(char *cIP, int len); bool IsIPsMatch(char *IP1, int len1, char *IP2, int len2); }; //---------------------------------------------------------------------------
TCheckIPv4::TCheckIPv4() { // ---- } //---------------------------------------------------------------------------
TCheckIPv4::~TCheckIPv4() { // ---- } //---------------------------------------------------------------------------
bool TCheckIPv4::IsCorrectIPv4Part(char *cip, int len) { bool Result = false; // ---- if(len > 0 && len < 4) { if(stricmp(cip, "*") == 0) { Result = true; } else { int iip = 0; if(sscanf(cip, "%d", &iip) == 1) Result = (iip >= 0 && iip < 256); } } // ---- return Result; } //---------------------------------------------------------------------------
bool TCheckIPv4::IsCorrectIPv4PartCompare(char *ipsrc, int len1, char *ipdes, int len2) { bool Result = false; // ---- if(len1 > 0 && len1 < 4 && len2 > 0 && len2 < 4) { if(stricmp(ipsrc, "*") == 0 || stricmp(ipdes, "*") == 0) { Result = true; } else { int iip1, iip2 = 0; if(sscanf(ipsrc, "%d", &iip1) == 1 && sscanf(ipdes, "%d", &iip2) == 1) { Result = (iip1 >= 0 && iip1 < 256 && iip2 >= 0 && iip2 < 256 && iip1 == iip2); } } } // ---- return Result; } //---------------------------------------------------------------------------
bool TCheckIPv4::GetIPParts(char *cIP, int len, char *ip1, char *ip2, char *ip3, char *ip4) { bool Result = false; // ---- char sTemp[16]; int i = 0, k = 0, n = 0; bool bip1, bip2, bip3, bip4; // ---- if(cIP != NULL && len > 6 && len < 17) { for(i = 0; i < len; i++) if(cIP[i] == '.') k++; // ---- if(k == 3) { for(i = 0, k = 0; i < len; i++) { if(cIP[i] == '.') { k++; n = 0; } else { switch(k) { case 0: ip1[n] = cIP[i]; break; case 1: ip2[n] = cIP[i]; break; case 2: ip3[n] = cIP[i]; break; case 3: ip4[n] = cIP[i]; break; } // ---- n++; } } // ---- memset(&sTemp, 0, sizeof(sTemp)); strcpy(sTemp, ip1); bip1 = IsCorrectIPv4Part(sTemp, strlen(sTemp)); memset(&sTemp, 0, sizeof(sTemp)); strcpy(sTemp, ip2); bip2 = IsCorrectIPv4Part(sTemp, strlen(sTemp)); memset(&sTemp, 0, sizeof(sTemp)); strcpy(sTemp, ip3); bip3 = IsCorrectIPv4Part(sTemp, strlen(sTemp)); memset(&sTemp, 0, sizeof(sTemp)); strcpy(sTemp, ip4); bip4 = IsCorrectIPv4Part(sTemp, strlen(sTemp)); // ----- Result = (bip1 && bip2 && bip3 && bip4); } } // ---- return Result; } //---------------------------------------------------------------------------
bool TCheckIPv4::IsCorrectIPv4(char *cIP, int len) { bool Result = false; // ---- char cip[4][16]; // ---- memset(&cip, 0, sizeof(cip)); // ---- Result = GetIPParts(cIP, len, cip[0], cip[1], cip[2], cip[3]); // ---- return Result; } //---------------------------------------------------------------------------
bool TCheckIPv4::IsIPsMatch(char *IPsrc, int len1, char *IPdes, int len2) { bool Result = false; // ---- char sTemp1[16], sTemp2[16]; char cip1[4][16], cip2[4][16]; bool bip1, bip2, bip3, bip4; // ---- memset(&cip1, 0, sizeof(cip1)); memset(&cip2, 0, sizeof(cip2)); // ---- if(GetIPParts(IPsrc, len1, cip1[0], cip1[1], cip1[2], cip1[3]) && GetIPParts(IPdes, len2, cip2[0], cip2[1], cip2[2], cip2[3])) { memset(&sTemp1, 0, sizeof(sTemp1)); memset(&sTemp2, 0, sizeof(sTemp2)); strcpy(sTemp1, cip1[0]); strcpy(sTemp2, cip2[0]); bip1 = IsCorrectIPv4PartCompare(sTemp1, strlen(sTemp1), sTemp2, strlen(sTemp2)); memset(&sTemp1, 0, sizeof(sTemp1)); memset(&sTemp2, 0, sizeof(sTemp2)); strcpy(sTemp1, cip1[1]); strcpy(sTemp2, cip2[1]); bip2 = IsCorrectIPv4PartCompare(sTemp1, strlen(sTemp1), sTemp2, strlen(sTemp2)); memset(&sTemp1, 0, sizeof(sTemp1)); memset(&sTemp2, 0, sizeof(sTemp2)); strcpy(sTemp1, cip1[2]); strcpy(sTemp2, cip2[2]); bip3 = IsCorrectIPv4PartCompare(sTemp1, strlen(sTemp1), sTemp2, strlen(sTemp2)); memset(&sTemp1, 0, sizeof(sTemp1)); memset(&sTemp2, 0, sizeof(sTemp2)); strcpy(sTemp1, cip1[3]); strcpy(sTemp2, cip2[3]); bip4 = IsCorrectIPv4PartCompare(sTemp1, strlen(sTemp1), sTemp2, strlen(sTemp2)); // ----- Result = (bip1 && bip2 && bip3 && bip4); } // ---- return Result; } //---------------------------------------------------------------------------
|
P.S. -> Просьба прокомментировать данный код. |