[Geoip-c-discuss] [PATCH] Add array access functions
Brought to you by:
tjmather
From: Ludwig N. <lud...@su...> - 2008-01-23 08:41:15
|
Add array access functions so programs can avoid accessing the arrays directly which whould break binary compatability. Signed-off-by: Ludwig Nussel <lud...@su...> --- libGeoIP/GeoIP.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ libGeoIP/GeoIP.h | 20 ++++++++++++++++++ 2 files changed, 78 insertions(+), 0 deletions(-) diff --git a/libGeoIP/GeoIP.c b/libGeoIP/GeoIP.c index 166cddb..6452d36 100644 --- a/libGeoIP/GeoIP.c +++ b/libGeoIP/GeoIP.c @@ -93,6 +93,8 @@ const char GeoIP_country_code[253][3] = { "--","AP","EU","AD","AE","AF","AG","AI "ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE", "BL","MF"}; +static const unsigned num_GeoIP_countries = (unsigned)(sizeof(GeoIP_country_code)/sizeof(GeoIP_country_code[0])); + const char GeoIP_country_code3[253][4] = { "--","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","ANT", "AGO","AQ","ARG","ASM","AUT","AUS","ABW","AZE","BIH","BRB", "BGD","BEL","BFA","BGR","BHR","BDI","BEN","BMU","BRN","BOL", @@ -1028,3 +1030,59 @@ int GeoIP_last_netmask (GeoIP* gi) { return gi->netmask; } + +/** return two letter country code */ +const char* GeoIP_code_by_id(int id) +{ + if (id < 0 || id >= num_GeoIP_countries) + return NULL; + + return GeoIP_country_code[id]; +} + +/** return three letter country code */ +const char* GeoIP_code3_by_id(int id) +{ + if (id < 0 || id >= num_GeoIP_countries) + return NULL; + + return GeoIP_country_code3[id]; +} + + +/** return full name of country */ +const char* GeoIP_name_by_id(int id) +{ + if (id < 0 || id >= num_GeoIP_countries) + return NULL; + + return GeoIP_country_name[id]; +} + +/** return continent of country */ +const char* GeoIP_continent_by_id(int id) +{ + if (id < 0 || id >= num_GeoIP_countries) + return NULL; + + return GeoIP_country_continent[id]; +} + +/** return id by country code **/ +int GeoIP_id_by_code(const char *country) +{ + unsigned i; + + for ( i = 0; i < num_GeoIP_countries; ++i) + { + if (strcmp(country, GeoIP_country_code[i]) == 0) + return i; + } + + return 0; +} + +unsigned GeoIP_num_countries(void) +{ + return num_GeoIP_countries; +} diff --git a/libGeoIP/GeoIP.h b/libGeoIP/GeoIP.h index 60b2c97..5c46200 100644 --- a/libGeoIP/GeoIP.h +++ b/libGeoIP/GeoIP.h @@ -107,6 +107,8 @@ extern const char *GeoIPCityDBFileName; extern const char *GeoIPOrgDBFileName; extern const char *GeoIPISPDBFileName; +/* Warning: do not use those arrays as doing so may break your + * program with newer GeoIP versions */ extern const char GeoIP_country_code[253][3]; extern const char GeoIP_country_code3[253][4]; extern const char * GeoIP_country_name[253]; @@ -159,6 +161,24 @@ GEOIP_API char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum); GEOIP_API char *GeoIP_name_by_addr (GeoIP* gi, const char *addr); GEOIP_API char *GeoIP_name_by_name (GeoIP* gi, const char *host); +/** return two letter country code */ +GEOIP_API const char* GeoIP_code_by_id(int id); + +/** return three letter country code */ +GEOIP_API const char* GeoIP_code3_by_id(int id); + +/** return full name of country */ +GEOIP_API const char* GeoIP_name_by_id(int id); + +/** return continent of country */ +GEOIP_API const char* GeoIP_continent_by_id(int id); + +/** return id by country code **/ +GEOIP_API int GeoIP_id_by_code(const char *country); + +/** return return number of known countries */ +GEOIP_API unsigned GeoIP_num_countries(void); + GEOIP_API char *GeoIP_database_info (GeoIP* gi); GEOIP_API unsigned char GeoIP_database_edition (GeoIP* gi); -- 1.5.3.4 -- (o_ Ludwig Nussel //\ V_/_ http://www.suse.de/ SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nuernberg) |