]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/test-country.c
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2019 IPFire Development Team <info@ipfire.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
23 #include <loc/libloc.h>
24 #include <loc/country.h>
25 #include <loc/database.h>
26 #include <loc/network.h>
27 #include <loc/writer.h>
29 int main(int argc
, char** argv
) {
30 struct loc_country
* country
;
33 // Check some valid country codes
34 if (!loc_country_code_is_valid("XX")) {
35 fprintf(stderr
, "Valid country code detected as invalid: %s\n", "XX");
39 // Check some invalid country codes
40 if (loc_country_code_is_valid("X1")) {
41 fprintf(stderr
, "Invalid country code detected as valid: %s\n", "X1");
51 struct loc_writer
* writer
;
52 err
= loc_writer_new(ctx
, &writer
, NULL
);
57 err
= loc_writer_add_country(writer
, &country
, "XX");
59 fprintf(stderr
, "Could not create country\n");
63 // Set name & continent
64 loc_country_set_name(country
, "Testistan");
65 loc_country_set_continent_code(country
, "YY");
68 loc_country_unref(country
);
70 // Add another country
71 err
= loc_writer_add_country(writer
, &country
, "YY");
73 fprintf(stderr
, "Could not create country: YY\n");
76 loc_country_unref(country
);
78 FILE* f
= fopen("test.db", "w+");
80 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
84 err
= loc_writer_write(writer
, f
);
86 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
91 loc_writer_unref(writer
);
93 // And open it again from disk
94 f
= fopen("test.db", "r");
96 fprintf(stderr
, "Could not open file for reading: %s\n", strerror(errno
));
100 struct loc_database
* db
;
101 err
= loc_database_new(ctx
, &db
, f
);
103 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
107 // Lookup an address in the subnet
108 err
= loc_database_get_country(db
, &country
, "YY");
110 fprintf(stderr
, "Could not find country: YY\n");
113 loc_country_unref(country
);
115 loc_database_unref(db
);