Code: Select all
try {
// This product includes GeoIP data created by MaxMind, available from http://maxmind.com/
// Updates at http://www.maxmind.com/app/geoip_country
string file = Util::getDataPath() + "GeoIpCountryWhois.csv";
string data = File(file, File::READ, File::OPEN).read();
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 lineend = 0;
CountryIter last = countries.end();
u_int32_t startIP = 0;
u_int32_t endIP = 0, endIPprev = 0;
for(;;) {
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;
lineend = data.find('\n', comma4);
if(lineend == string::npos) break;
startIP = Util::toUInt32(start + comma2 + 2);
endIP = Util::toUInt32(start + comma3 + 2);
u_int16_t* country = (u_int16_t*)(start + comma4 + 2);
if((startIP-1) != endIPprev)
last = countries.insert(last, make_pair((startIP-1), (u_int16_t)16191));
last = countries.insert(last, make_pair(endIP, *country));
endIPprev = endIP;
linestart = lineend + 1;
}
} catch(const FileException&) {
}
}
This is an sample of how the CSV file is structured:
"begin_ip","end_ip","begin_num","end_num","country","name"
"61.88.0.0","61.91.255.255","1029177344","1029439487","AU","Australia"
"61.92.0.0","61.93.255.255","1029439488","1029570559","HK","Hong Kong"
"61.94.0.0","61.94.7.255","1029570560","1029572607","ID","Indonesia"
Code: Select all
/* getIpCountry
This function returns the country(Abbreviation) of an ip
for exemple: it returns "PT", whitch standards for "Portugal"
more info: http://www.maxmind.com/app/csv
*/
string Util::getIpCountry (string IP) {
if (BOOLSETTING(GET_USER_COUNTRY)) {
dcassert(count(IP.begin(), IP.end(), '.') == 3);
//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);
u_int32_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) );
CountryIter i = countries.lower_bound(ipnum);
if(i != countries.end()) {
return string((char*)&(i->second), 2);
}
}
return Util::emptyString; //if doesn't returned anything already, something is wrong...
}
What im wondering about is someone can remake the code so it doesnt return country code, but returns instead the full name of the country.