From 4e6c6fea4a4acf53e3d09797e5addb3e3478c55a Mon Sep 17 00:00:00 2001 From: psychon Date: Mon, 12 May 2008 15:38:12 +0000 Subject: [PATCH] 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 --- Utils.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Utils.cpp b/Utils.cpp index 451d7547..a0a0e66c 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -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() {