2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 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.
27 #include <loc/libloc.h>
28 #include <loc/database.h>
29 #include <loc/writer.h>
31 const char* VENDOR
= "Test Vendor";
32 const char* DESCRIPTION
=
33 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
34 "Proin ultrices pulvinar dolor, et sollicitudin eros ultricies "
35 "vitae. Nam in volutpat libero. Nulla facilisi. Pellentesque "
36 "tempor felis enim. Integer congue nisi in maximus pretium. "
37 "Pellentesque et turpis elementum, luctus mi at, interdum erat. "
38 "Maecenas ut venenatis nunc.";
39 const char* LICENSE
= "CC";
41 const char* networks
[] = {
49 static int attempt_to_open(struct loc_ctx
* ctx
, char* path
) {
50 FILE* f
= fopen(path
, "r");
54 struct loc_database
* db
;
55 int r
= loc_database_new(ctx
, &db
, f
);
58 fprintf(stderr
, "Opening %s was unexpectedly successful\n", path
);
59 loc_database_unref(db
);
64 // Close the file again
70 int main(int argc
, char** argv
) {
78 // Enable debug logging
79 loc_set_log_priority(ctx
, LOG_DEBUG
);
81 // Try opening an empty file
82 err
= attempt_to_open(ctx
, "/dev/null");
86 // Try opening a file with all zeroes
87 err
= attempt_to_open(ctx
, "/dev/zero");
91 // Try opening a file with random data
92 err
= attempt_to_open(ctx
, "/dev/urandom");
97 struct loc_writer
* writer
;
98 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
103 err
= loc_writer_set_vendor(writer
, VENDOR
);
105 fprintf(stderr
, "Could not set vendor\n");
110 const char* vendor
= loc_writer_get_vendor(writer
);
112 printf("Vendor is: %s\n", vendor
);
114 fprintf(stderr
, "Could not retrieve vendor\n");
119 err
= loc_writer_set_description(writer
, DESCRIPTION
);
121 fprintf(stderr
, "Could not set description\n");
125 // Retrieve description
126 const char* description
= loc_writer_get_description(writer
);
128 printf("Description is: %s\n", description
);
130 fprintf(stderr
, "Could not retrieve description\n");
135 err
= loc_writer_set_license(writer
, LICENSE
);
137 fprintf(stderr
, "Could not set license\n");
142 const char* license
= loc_writer_get_license(writer
);
144 printf("License is: %s\n", license
);
146 fprintf(stderr
, "Could not retrieve license\n");
150 struct loc_network
* network
= NULL
;
153 const char** n
= networks
;
155 err
= loc_writer_add_network(writer
, &network
, *n
);
157 fprintf(stderr
, "Could not add network %s\n", *n
);
162 loc_network_set_country_code(network
, "XX");
170 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
174 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
176 fprintf(stderr
, "Could not write database: %s\n", strerror(err
));
179 loc_writer_unref(writer
);
181 // And open it again from disk
182 struct loc_database
* db
;
183 err
= loc_database_new(ctx
, &db
, f
);
185 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
189 // Try reading something from the database
190 vendor
= loc_database_get_vendor(db
);
192 fprintf(stderr
, "Could not retrieve vendor\n");
194 } else if (strcmp(vendor
, VENDOR
) != 0) {
195 fprintf(stderr
, "Vendor doesn't match: %s != %s\n", vendor
, VENDOR
);
200 struct loc_database_enumerator
* enumerator
;
201 err
= loc_database_enumerator_new(&enumerator
, db
, LOC_DB_ENUMERATE_NETWORKS
, 0);
203 fprintf(stderr
, "Could not initialise the enumerator: %d\n", err
);
207 // Walk through all networks
209 err
= loc_database_enumerator_next_network(enumerator
, &network
);
211 fprintf(stderr
, "Error fetching the next network: %d\n", err
);
218 char* s
= loc_network_str(network
);
219 printf("Got network: %s\n", s
);
223 // Free the enumerator
224 loc_database_enumerator_unref(enumerator
);
226 // Close the database
227 loc_database_unref(db
);