← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2364: cache country names

 

------------------------------------------------------------
revno: 2364
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Sun 2010-12-26 18:45:12 +0100
message:
  cache country names
modified:
  dcpp/Util.cpp
  dcpp/Util.h
  win32/main.cpp


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/Util.cpp'
--- dcpp/Util.cpp	2010-12-10 22:16:24 +0000
+++ dcpp/Util.cpp	2010-12-26 17:45:12 +0000
@@ -60,6 +60,7 @@
 time_t Util::awayTime;
 
 Util::CountryList Util::countries;
+StringList Util::countryNames;
 
 string Util::paths[Util::PATH_LAST];
 
@@ -200,36 +201,43 @@
 
 		const char* start = data.c_str();
 		string::size_type linestart = 0;
-		string::size_type comma1 = 0;
-		string::size_type comma2 = 0;
-		string::size_type comma3 = 0;
-		string::size_type comma4 = 0;
-		string::size_type comma5 = 0;
 		string::size_type lineend = 0;
 		auto last = countries.end();
 		uint32_t startIP = 0;
 		uint32_t endIP = 0, endIPprev = 0;
 
+		countryNames.push_back(_("Unknown"));
+		auto addCountry = [](const string& countryName) -> size_t {
+			auto begin = countryNames.cbegin(), end = countryNames.cend();
+			auto pos = std::find(begin, end, countryName);
+			if(pos != end)
+				return pos - begin;
+			countryNames.push_back(countryName);
+			return countryNames.size() - 1;
+		};
+
 		while(true) {
-			comma1 = data.find(',', linestart);
-			if(comma1 == string::npos) break;
-			comma2 = data.find(',', comma1 + 1);
-			if(comma2 == string::npos) break;
-			comma3 = data.find(',', comma2 + 1);
-			if(comma3 == string::npos) break;
-			comma4 = data.find(',', comma3 + 1);
-			if(comma4 == string::npos) break;
-			comma5 = data.find(',', comma4 + 1);
-			if(comma5 == string::npos) break;
-			lineend = data.find('\n', comma5);
+			auto pos = data.find(',', linestart);
+			if(pos == string::npos) break;
+			pos = data.find(',', pos + 1);
+			if(pos == string::npos) break;
+			startIP = toUInt32(start + pos + 2) - 1;
+
+			pos = data.find(',', pos + 1);
+			if(pos == string::npos) break;
+			endIP = toUInt32(start + pos + 2);
+
+			pos = data.find(',', pos + 1);
+			if(pos == string::npos) break;
+			pos = data.find(',', pos + 1);
+			if(pos == string::npos) break;
+			lineend = data.find('\n', pos);
 			if(lineend == string::npos) break;
 
-			startIP = Util::toUInt32(start + comma2 + 2);
-			endIP = Util::toUInt32(start + comma3 + 2);
-			if((startIP-1) != endIPprev)
-				last = countries.insert(last, make_pair((startIP-1), _("Unknown")));
-			auto nameStart = comma5 + 2;
-			last = countries.insert(last, make_pair(endIP, string(start + nameStart, lineend - 1 - nameStart)));
+			if(startIP != endIPprev)
+				last = countries.insert(last, make_pair(startIP, 0));
+			pos += 2;
+			last = countries.insert(last, make_pair(endIP, addCountry(data.substr(pos, lineend - 1 - pos))));
 
 			endIPprev = endIP;
 			linestart = lineend + 1;
@@ -904,25 +912,26 @@
 const string& Util::getIpCountry(const string& IP) {
 	if(BOOLSETTING(GET_USER_COUNTRY)) {
 		if(count(IP.begin(), IP.end(), '.') != 3)
-			return Util::emptyString;
+			return emptyString;
 
 		//e.g IP 23.24.25.26 : w=23, x=24, y=25, z=26
 		string::size_type a = IP.find('.');
 		string::size_type b = IP.find('.', a+1);
 		string::size_type c = IP.find('.', b+2);
 
-		uint32_t ipnum = (Util::toUInt32(IP.c_str()) << 24) |
-			(Util::toUInt32(IP.c_str() + a + 1) << 16) |
-			(Util::toUInt32(IP.c_str() + b + 1) << 8) |
-			(Util::toUInt32(IP.c_str() + c + 1) );
+		/// @todo this is impl dependant and is working by chance because we are currently using atoi!
+		uint32_t ipnum = (toUInt32(IP.c_str()) << 24) |
+			(toUInt32(IP.c_str() + a + 1) << 16) |
+			(toUInt32(IP.c_str() + b + 1) << 8) |
+			(toUInt32(IP.c_str() + c + 1) );
 
 		auto i = countries.lower_bound(ipnum);
 		if(i != countries.end()) {
-			return i->second;
+			return countryNames[i->second];
 		}
 	}
 
-	return Util::emptyString;
+	return emptyString;
 }
 
 string Util::getTimeString() {

=== modified file 'dcpp/Util.h'
--- dcpp/Util.h	2010-12-10 22:16:24 +0000
+++ dcpp/Util.h	2010-12-26 17:45:12 +0000
@@ -446,8 +446,9 @@
 	static string awayMsg;
 	static time_t awayTime;
 
-	typedef map<uint32_t, string> CountryList;
+	typedef map<uint32_t, size_t> CountryList;
 	static CountryList countries;
+	static StringList countryNames;
 
 	static void loadBootConfig();
 };

=== modified file 'win32/main.cpp'
--- win32/main.cpp	2010-12-18 16:42:12 +0000
+++ win32/main.cpp	2010-12-26 17:45:12 +0000
@@ -70,6 +70,16 @@
 #endif
 
 int SmartWinMain(dwt::Application& app) {
+#ifdef _DEBUG
+	old_handler = set_terminate(&term_handler);
+
+#ifndef CONSOLE
+	// attach to the parent console, or create one
+	if(AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
+		freopen("conout$", "w", stdout);
+#endif
+#endif
+
 	// http://www.kb.cert.org/vuls/id/707943 part III, "For Developers".
 	::SetDllDirectory(_T(""));
 
@@ -109,16 +119,6 @@
 		return 1;
 	}
 
-#ifdef _DEBUG
-	old_handler = set_terminate(&term_handler);
-
-#ifndef CONSOLE
-	// attach to the parent console, or create one
-	if(AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
-		freopen("conout$", "w", stdout);
-#endif
-#endif
-
 	// For SHBrowseForFolder, UPnP_COM
 	HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
 	if(FAILED(hr))