1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2/* test-geoip-city.c
3 *
4 * Copyright (C) 2006 MaxMind LLC
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 */
20
21#include "GeoIP.h"
22#include "GeoIPCity.h"
23
24static const char *_mk_NA(const char *p)
25{
26    return p ? p : "N/A";
27}
28
29int main(int argc, char *argv[])
30{
31    FILE *f;
32    GeoIP *gi;
33    GeoIPRecord *gir;
34    int generate = 0;
35    char host[50];
36    const char *time_zone = NULL;
37    char **ret;
38    if (argc == 2)
39        if (!strcmp(argv[1], "gen"))
40            generate = 1;
41
42    gi = GeoIP_open("../data/GeoIPCity.dat", GEOIP_INDEX_CACHE);
43
44    if (gi == NULL) {
45        fprintf(stderr, "Error opening database\n");
46        exit(1);
47    }
48
49    f = fopen("city_test.txt", "r");
50
51    if (f == NULL) {
52        fprintf(stderr, "Error opening city_test.txt\n");
53        exit(1);
54    }
55
56    while (fscanf(f, "%s", host) != EOF) {
57        gir = GeoIP_record_by_name(gi, (const char *)host);
58
59        if (gir != NULL) {
60            ret = GeoIP_range_by_ip(gi, (const char *)host);
61            time_zone =
62                GeoIP_time_zone_by_country_and_region(gir->country_code,
63                                                      gir->region);
64            printf("%s\t%s\t%s\t%s\t%s\t%s\t%f\t%f\t%d\t%d\t%s\t%s\t%s\n", host,
65                   _mk_NA(gir->country_code), _mk_NA(gir->region),
66                   _mk_NA(GeoIP_region_name_by_code
67                          (gir->country_code, gir->region)), _mk_NA(gir->city),
68                   _mk_NA(gir->postal_code), gir->latitude, gir->longitude,
69                   gir->metro_code, gir->area_code, _mk_NA(time_zone), ret[0],
70                   ret[1]);
71            GeoIP_range_by_ip_delete(ret);
72            GeoIPRecord_delete(gir);
73        }
74    }
75    GeoIP_delete(gi);
76    fclose(f);
77    return 0;
78}
79