]>
Commit | Line | Data |
---|---|---|
2601e83e MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2017 IPFire Development Team <info@ipfire.org> | |
5 | ||
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. | |
10 | ||
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. | |
15 | */ | |
16 | ||
17 | #include <stdio.h> | |
18 | #include <stddef.h> | |
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
21 | #include <fcntl.h> | |
22 | #include <ctype.h> | |
23 | #include <errno.h> | |
24 | #include <unistd.h> | |
25 | ||
26 | #include <loc/libloc.h> | |
9fc7f001 | 27 | #include <loc/database.h> |
c182393f | 28 | #include <loc/writer.h> |
2601e83e | 29 | |
64ce6e2f | 30 | const char* VENDOR = "Test Vendor"; |
2601e83e MT |
31 | const char* DESCRIPTION = |
32 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " | |
33 | "Proin ultrices pulvinar dolor, et sollicitudin eros ultricies " | |
34 | "vitae. Nam in volutpat libero. Nulla facilisi. Pellentesque " | |
35 | "tempor felis enim. Integer congue nisi in maximus pretium. " | |
36 | "Pellentesque et turpis elementum, luctus mi at, interdum erat. " | |
37 | "Maecenas ut venenatis nunc."; | |
38 | ||
39 | int main(int argc, char** argv) { | |
40 | int err; | |
41 | ||
42 | struct loc_ctx* ctx; | |
43 | err = loc_new(&ctx); | |
44 | if (err < 0) | |
45 | exit(EXIT_FAILURE); | |
46 | ||
47 | // Create a database | |
c182393f MT |
48 | struct loc_writer* writer; |
49 | err = loc_writer_new(ctx, &writer); | |
2601e83e MT |
50 | if (err < 0) |
51 | exit(EXIT_FAILURE); | |
52 | ||
53 | // Set the vendor | |
64ce6e2f | 54 | err = loc_writer_set_vendor(writer, VENDOR); |
2601e83e MT |
55 | if (err) { |
56 | fprintf(stderr, "Could not set vendor\n"); | |
57 | exit(EXIT_FAILURE); | |
58 | } | |
59 | ||
60 | // Retrieve vendor | |
64ce6e2f MT |
61 | const char* vendor = loc_writer_get_vendor(writer); |
62 | if (vendor) { | |
63 | printf("Vendor is: %s\n", vendor); | |
2601e83e MT |
64 | } else { |
65 | fprintf(stderr, "Could not retrieve vendor\n"); | |
66 | exit(EXIT_FAILURE); | |
67 | } | |
68 | ||
69 | // Set a description | |
c182393f | 70 | err = loc_writer_set_description(writer, DESCRIPTION); |
2601e83e MT |
71 | if (err) { |
72 | fprintf(stderr, "Could not set description\n"); | |
73 | exit(EXIT_FAILURE); | |
74 | } | |
75 | ||
76 | // Retrieve description | |
c182393f | 77 | const char* description = loc_writer_get_description(writer); |
2601e83e MT |
78 | if (description) { |
79 | printf("Description is: %s\n", description); | |
80 | } else { | |
81 | fprintf(stderr, "Could not retrieve description\n"); | |
82 | exit(EXIT_FAILURE); | |
83 | } | |
84 | ||
85 | FILE* f = fopen("test.db", "w"); | |
86 | if (!f) { | |
87 | fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); | |
88 | exit(EXIT_FAILURE); | |
89 | } | |
90 | ||
c182393f | 91 | err = loc_writer_write(writer, f); |
2601e83e MT |
92 | if (err) { |
93 | fprintf(stderr, "Could not write database: %s\n", strerror(err)); | |
94 | exit(EXIT_FAILURE); | |
95 | } | |
64ce6e2f | 96 | loc_writer_unref(writer); |
2601e83e MT |
97 | |
98 | // Close the file | |
99 | fclose(f); | |
100 | ||
2601e83e MT |
101 | // And open it again from disk |
102 | f = fopen("test.db", "r"); | |
103 | if (!f) { | |
104 | fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno)); | |
105 | exit(EXIT_FAILURE); | |
106 | } | |
107 | ||
c182393f MT |
108 | struct loc_database* db; |
109 | err = loc_database_new(ctx, &db, f); | |
2601e83e MT |
110 | if (err) { |
111 | fprintf(stderr, "Could not open database: %s\n", strerror(-err)); | |
112 | exit(EXIT_FAILURE); | |
113 | } | |
114 | ||
64ce6e2f MT |
115 | vendor = loc_database_get_vendor(db); |
116 | if (!vendor) { | |
2601e83e MT |
117 | fprintf(stderr, "Could not retrieve vendor\n"); |
118 | exit(EXIT_FAILURE); | |
64ce6e2f MT |
119 | } else if (strcmp(vendor, VENDOR) != 0) { |
120 | fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor, VENDOR); | |
2601e83e MT |
121 | exit(EXIT_FAILURE); |
122 | } | |
123 | ||
124 | // Close the database | |
c182393f | 125 | loc_database_unref(db); |
2601e83e MT |
126 | |
127 | loc_unref(ctx); | |
128 | ||
129 | return EXIT_SUCCESS; | |
130 | } |