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 static int attempt_to_open(struct loc_ctx
* ctx
, char* path
) {
42 FILE* f
= fopen(path
, "r");
46 struct loc_database
* db
;
47 int r
= loc_database_new(ctx
, &db
, f
);
50 fprintf(stderr
, "Opening %s was unexpectedly successful\n", path
);
51 loc_database_unref(db
);
56 // Close the file again
62 int main(int argc
, char** argv
) {
70 // Enable debug logging
71 loc_set_log_priority(ctx
, LOG_DEBUG
);
73 // Try opening an empty file
74 err
= attempt_to_open(ctx
, "/dev/null");
78 // Try opening a file with all zeroes
79 err
= attempt_to_open(ctx
, "/dev/zero");
83 // Try opening a file with random data
84 err
= attempt_to_open(ctx
, "/dev/urandom");
89 struct loc_writer
* writer
;
90 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
95 err
= loc_writer_set_vendor(writer
, VENDOR
);
97 fprintf(stderr
, "Could not set vendor\n");
102 const char* vendor
= loc_writer_get_vendor(writer
);
104 printf("Vendor is: %s\n", vendor
);
106 fprintf(stderr
, "Could not retrieve vendor\n");
111 err
= loc_writer_set_description(writer
, DESCRIPTION
);
113 fprintf(stderr
, "Could not set description\n");
117 // Retrieve description
118 const char* description
= loc_writer_get_description(writer
);
120 printf("Description is: %s\n", description
);
122 fprintf(stderr
, "Could not retrieve description\n");
127 err
= loc_writer_set_license(writer
, LICENSE
);
129 fprintf(stderr
, "Could not set license\n");
134 const char* license
= loc_writer_get_license(writer
);
136 printf("License is: %s\n", license
);
138 fprintf(stderr
, "Could not retrieve license\n");
144 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
148 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
150 fprintf(stderr
, "Could not write database: %s\n", strerror(err
));
153 loc_writer_unref(writer
);
155 // And open it again from disk
156 struct loc_database
* db
;
157 err
= loc_database_new(ctx
, &db
, f
);
159 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
163 // Try reading something from the database
164 vendor
= loc_database_get_vendor(db
);
166 fprintf(stderr
, "Could not retrieve vendor\n");
168 } else if (strcmp(vendor
, VENDOR
) != 0) {
169 fprintf(stderr
, "Vendor doesn't match: %s != %s\n", vendor
, VENDOR
);
173 // Close the database
174 loc_database_unref(db
);