]> git.ipfire.org Git - people/ms/libloc.git/blame - src/test-database.c
Split database into a writer and reader
[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>
c182393f 27#include <loc/writer.h>
2601e83e
MT
28#include "database.h"
29
30const 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.";
37
38int main(int argc, char** argv) {
39 int err;
40
41 struct loc_ctx* ctx;
42 err = loc_new(&ctx);
43 if (err < 0)
44 exit(EXIT_FAILURE);
45
46 // Create a database
c182393f
MT
47 struct loc_writer* writer;
48 err = loc_writer_new(ctx, &writer);
2601e83e
MT
49 if (err < 0)
50 exit(EXIT_FAILURE);
51
52 // Set the vendor
c182393f 53 err = loc_writer_set_vendor(writer, "Test Vendor");
2601e83e
MT
54 if (err) {
55 fprintf(stderr, "Could not set vendor\n");
56 exit(EXIT_FAILURE);
57 }
58
59 // Retrieve vendor
c182393f 60 const char* vendor1 = loc_writer_get_vendor(writer);
2601e83e
MT
61 if (vendor1) {
62 printf("Vendor is: %s\n", vendor1);
63 } else {
64 fprintf(stderr, "Could not retrieve vendor\n");
65 exit(EXIT_FAILURE);
66 }
67
68 // Set a description
c182393f 69 err = loc_writer_set_description(writer, DESCRIPTION);
2601e83e
MT
70 if (err) {
71 fprintf(stderr, "Could not set description\n");
72 exit(EXIT_FAILURE);
73 }
74
75 // Retrieve description
c182393f 76 const char* description = loc_writer_get_description(writer);
2601e83e
MT
77 if (description) {
78 printf("Description is: %s\n", description);
79 } else {
80 fprintf(stderr, "Could not retrieve description\n");
81 exit(EXIT_FAILURE);
82 }
83
84 FILE* f = fopen("test.db", "w");
85 if (!f) {
86 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
87 exit(EXIT_FAILURE);
88 }
89
c182393f 90 err = loc_writer_write(writer, f);
2601e83e
MT
91 if (err) {
92 fprintf(stderr, "Could not write database: %s\n", strerror(err));
93 exit(EXIT_FAILURE);
94 }
95
96 // Close the file
97 fclose(f);
98
2601e83e
MT
99 // And open it again from disk
100 f = fopen("test.db", "r");
101 if (!f) {
102 fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
103 exit(EXIT_FAILURE);
104 }
105
c182393f
MT
106 struct loc_database* db;
107 err = loc_database_new(ctx, &db, f);
2601e83e
MT
108 if (err) {
109 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
110 exit(EXIT_FAILURE);
111 }
112
c182393f 113 const char* vendor2 = loc_database_get_vendor(db);
2601e83e
MT
114 if (!vendor2) {
115 fprintf(stderr, "Could not retrieve vendor\n");
116 exit(EXIT_FAILURE);
117 } else if (strcmp(vendor1, vendor2) != 0) {
118 fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor1, vendor2);
119 exit(EXIT_FAILURE);
120 }
121
122 // Close the database
c182393f 123 loc_database_unref(db);
2601e83e 124
c182393f 125 loc_writer_unref(writer);
2601e83e
MT
126 loc_unref(ctx);
127
128 return EXIT_SUCCESS;
129}