Skip to content

Instantly share code, notes, and snippets.

@ottomata
Created July 11, 2012 19:09
Show Gist options
  • Save ottomata/3092456 to your computer and use it in GitHub Desktop.
Save ottomata/3092456 to your computer and use it in GitHub Desktop.
geoip code
//...
typedef enum {
GEOIP_CHARSET_ISO_8859_1 = 0,
GEOIP_CHARSET_UTF8 = 1
} GeoIPCharset;
//...
// ...
/* get city */
while (record_buf[str_length] != '\0')
str_length++;
if (str_length > 0) {
if ( gi->charset == GEOIP_CHARSET_UTF8 ) {
record->city = _iso_8859_1__utf8( (const char * ) record_buf );
} else {
record->city = malloc(str_length+1);
strncpy(record->city, ( const char * ) record_buf, str_length+1);
}
}
//...
case CITY:{
GeoIPRecord *grecord;
char *city;
int mustFreeCity = 0;
grecord = GeoIP_record_by_addr(gi, ipaddr);
if (grecord !=NULL){
if (grecord->city == NULL){
strncpy(area, unknown_geography, MAX_BUF_LENGTH);
} else {
int len = strlen(grecord->city);
if (grecord->charset == 0) {
city = GeoIP_iso_8859_1__utf8(grecord->city);
mustFreeCity = 1;
} else {
city = grecord->city;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment