Cleanup CUtils::GetLongIP()

The new version should be faster (no malloc with pseudo-memset) and it
certainly is a lot more readable.


git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1051 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
psychon
2008-05-12 15:38:12 +00:00
parent bc2af5d49a
commit 4e6c6fea4a
+12 -12
View File
@@ -142,21 +142,21 @@ CString CUtils::GetIP(unsigned long addr) {
}
unsigned long CUtils::GetLongIP(const CString& sIP) {
register int i;
char *addr = (char *) malloc(sIP.length() +1);
char ip[4][4], n;
unsigned long ret;
char ip[4][4];
int i;
strcpy(addr, sIP.c_str());
for (i=0; i<4; ip[0][i]=ip[1][i]=ip[2][i]=ip[3][i]='\0', i++);
if (sscanf(addr, "%3[0-9].%3[0-9].%3[0-9].%3[0-9]%[^\n]", ip[0], ip[1], ip[2], ip[3], &n) != 4) {
free(addr);
i = sscanf(sIP.c_str(), "%3[0-9].%3[0-9].%3[0-9].%3[0-9]",
ip[0], ip[1], ip[2], ip[3]);
if (i != 4)
return 0;
}
free(addr);
return (unsigned long) ((atoi(ip[0]) << 24) + (atoi(ip[1]) << 16) + (atoi(ip[2]) << 8) + atoi(ip[3]));
ret = atoi(ip[0]) << 24;
ret += atoi(ip[1]) << 16;
ret += atoi(ip[2]) << 8;
ret += atoi(ip[3]) << 0;
return ret;
}
CString CUtils::GetHashPass() {