]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/test-database.c
2dd052851c59da040a29d8c17b370c010fe69d45
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.
26 #include <loc/libloc.h>
27 #include <loc/writer.h>
30 const char* DESCRIPTION
=
31 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
32 "Proin ultrices pulvinar dolor, et sollicitudin eros ultricies "
33 "vitae. Nam in volutpat libero. Nulla facilisi. Pellentesque "
34 "tempor felis enim. Integer congue nisi in maximus pretium. "
35 "Pellentesque et turpis elementum, luctus mi at, interdum erat. "
36 "Maecenas ut venenatis nunc.";
38 int main(int argc
, char** argv
) {
47 struct loc_writer
* writer
;
48 err
= loc_writer_new(ctx
, &writer
);
53 err
= loc_writer_set_vendor(writer
, "Test Vendor");
55 fprintf(stderr
, "Could not set vendor\n");
60 const char* vendor1
= loc_writer_get_vendor(writer
);
62 printf("Vendor is: %s\n", vendor1
);
64 fprintf(stderr
, "Could not retrieve vendor\n");
69 err
= loc_writer_set_description(writer
, DESCRIPTION
);
71 fprintf(stderr
, "Could not set description\n");
75 // Retrieve description
76 const char* description
= loc_writer_get_description(writer
);
78 printf("Description is: %s\n", description
);
80 fprintf(stderr
, "Could not retrieve description\n");
84 FILE* f
= fopen("test.db", "w");
86 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
90 err
= loc_writer_write(writer
, f
);
92 fprintf(stderr
, "Could not write database: %s\n", strerror(err
));
99 // And open it again from disk
100 f
= fopen("test.db", "r");
102 fprintf(stderr
, "Could not open file for reading: %s\n", strerror(errno
));
106 struct loc_database
* db
;
107 err
= loc_database_new(ctx
, &db
, f
);
109 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
113 const char* vendor2
= loc_database_get_vendor(db
);
115 fprintf(stderr
, "Could not retrieve vendor\n");
117 } else if (strcmp(vendor1
, vendor2
) != 0) {
118 fprintf(stderr
, "Vendor doesn't match: %s != %s\n", vendor1
, vendor2
);
122 // Close the database
123 loc_database_unref(db
);
125 loc_writer_unref(writer
);