]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/test-country.c
96e1c3bce066a70f7be6869259dd8ede1ba3db69
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/database.h>
25 #include <loc/network.h>
26 #include <loc/writer.h>
28 int main(int argc
, char** argv
) {
29 struct loc_country
* country
;
38 struct loc_writer
* writer
;
39 err
= loc_writer_new(ctx
, &writer
);
44 err
= loc_writer_add_country(writer
, &country
, "T1");
46 fprintf(stderr
, "Could not create country\n");
50 // Set name & continent
51 loc_country_set_name(country
, "Testistan");
52 loc_country_set_continent_code(country
, "XX");
55 loc_country_unref(country
);
57 FILE* f
= fopen("test.db", "w");
59 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
63 err
= loc_writer_write(writer
, f
);
65 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
70 loc_writer_unref(writer
);
72 // And open it again from disk
73 f
= fopen("test.db", "r");
75 fprintf(stderr
, "Could not open file for reading: %s\n", strerror(errno
));
79 struct loc_database
* db
;
80 err
= loc_database_new(ctx
, &db
, f
);
82 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
86 // Lookup an address in the subnet
87 err
= loc_database_get_country(db
, &country
, "T1");
89 fprintf(stderr
, "Could not find country T1\n");
92 loc_country_unref(country
);
94 loc_database_unref(db
);