]> git.ipfire.org Git - people/ms/libloc.git/blame - src/test-database.c
Draft initial database format
[people/ms/libloc.git] / src / test-database.c
CommitLineData
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>
27#include "database.h"
28
29const char* DESCRIPTION =
30 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
31 "Proin ultrices pulvinar dolor, et sollicitudin eros ultricies "
32 "vitae. Nam in volutpat libero. Nulla facilisi. Pellentesque "
33 "tempor felis enim. Integer congue nisi in maximus pretium. "
34 "Pellentesque et turpis elementum, luctus mi at, interdum erat. "
35 "Maecenas ut venenatis nunc.";
36
37int main(int argc, char** argv) {
38 int err;
39
40 struct loc_ctx* ctx;
41 err = loc_new(&ctx);
42 if (err < 0)
43 exit(EXIT_FAILURE);
44
45 // Create a database
46 struct loc_database* db;
47 err = loc_database_new(ctx, &db, 1024);
48 if (err < 0)
49 exit(EXIT_FAILURE);
50
51 // Set the vendor
52 err = loc_database_set_vendor(db, "Test Vendor");
53 if (err) {
54 fprintf(stderr, "Could not set vendor\n");
55 exit(EXIT_FAILURE);
56 }
57
58 // Retrieve vendor
59 const char* vendor1 = loc_database_get_vendor(db);
60 if (vendor1) {
61 printf("Vendor is: %s\n", vendor1);
62 } else {
63 fprintf(stderr, "Could not retrieve vendor\n");
64 exit(EXIT_FAILURE);
65 }
66
67 // Set a description
68 err = loc_database_set_description(db, DESCRIPTION);
69 if (err) {
70 fprintf(stderr, "Could not set description\n");
71 exit(EXIT_FAILURE);
72 }
73
74 // Retrieve description
75 const char* description = loc_database_get_description(db);
76 if (description) {
77 printf("Description is: %s\n", description);
78 } else {
79 fprintf(stderr, "Could not retrieve description\n");
80 exit(EXIT_FAILURE);
81 }
82
83 FILE* f = fopen("test.db", "w");
84 if (!f) {
85 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
86 exit(EXIT_FAILURE);
87 }
88
89 err = loc_database_write(db, f);
90 if (err) {
91 fprintf(stderr, "Could not write database: %s\n", strerror(err));
92 exit(EXIT_FAILURE);
93 }
94
95 // Close the file
96 fclose(f);
97
98 // Close the database
99 //loc_database_unref(db);
100
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
108 struct loc_database* db2;
109 err = loc_database_open(ctx, &db2, f);
110 if (err) {
111 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
112 exit(EXIT_FAILURE);
113 }
114
115 const char* vendor2 = loc_database_get_vendor(db2);
116 if (!vendor2) {
117 fprintf(stderr, "Could not retrieve vendor\n");
118 exit(EXIT_FAILURE);
119 } else if (strcmp(vendor1, vendor2) != 0) {
120 fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor1, vendor2);
121 exit(EXIT_FAILURE);
122 }
123
124 // Close the database
125 loc_database_unref(db2);
126 fclose(f);
127
128 loc_unref(ctx);
129
130 return EXIT_SUCCESS;
131}