1 /*
2 * Copyright (c) 2014 Jerry Lundström <lundstrom.jerry@gmail.com>
3 * Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
4 * Copyright (c) 2014 OpenDNSSEC AB (svb)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include "db_configuration.h"
31 #include "db_error.h"
32
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 /* DB CONFIGURATION */
38
39
40
db_configuration_new(void)41 db_configuration_t* db_configuration_new(void) {
42 db_configuration_t* configuration =
43 (db_configuration_t*)calloc(1, sizeof(db_configuration_t));
44
45 return configuration;
46 }
47
db_configuration_free(db_configuration_t * configuration)48 void db_configuration_free(db_configuration_t* configuration) {
49 if (configuration) {
50 if (configuration->name) {
51 free(configuration->name);
52 }
53 if (configuration->value) {
54 free(configuration->value);
55 }
56 free(configuration);
57 }
58 }
59
db_configuration_value(const db_configuration_t * configuration)60 const char* db_configuration_value(const db_configuration_t* configuration) {
61 if (!configuration) {
62 return NULL;
63 }
64
65 return configuration->value;
66 }
67
db_configuration_set_name(db_configuration_t * configuration,const char * name)68 int db_configuration_set_name(db_configuration_t* configuration, const char* name) {
69 char* new_name;
70
71 if (!configuration) {
72 return DB_ERROR_UNKNOWN;
73 }
74 if (!name) {
75 return DB_ERROR_UNKNOWN;
76 }
77
78 if (!(new_name = strdup(name))) {
79 return DB_ERROR_UNKNOWN;
80 }
81
82 if (configuration->name) {
83 free(configuration->name);
84 }
85 configuration->name = new_name;
86 return DB_OK;
87 }
88
db_configuration_set_value(db_configuration_t * configuration,const char * value)89 int db_configuration_set_value(db_configuration_t* configuration, const char* value) {
90 char* new_value;
91
92 if (!configuration) {
93 return DB_ERROR_UNKNOWN;
94 }
95 if (!value) {
96 return DB_ERROR_UNKNOWN;
97 }
98
99 if (!(new_value = strdup(value))) {
100 return DB_ERROR_UNKNOWN;
101 }
102
103 if (configuration->value) {
104 free(configuration->value);
105 }
106 configuration->value = new_value;
107 return DB_OK;
108 }
109
db_configuration_not_empty(const db_configuration_t * configuration)110 int db_configuration_not_empty(const db_configuration_t* configuration) {
111 if (!configuration) {
112 return DB_ERROR_UNKNOWN;
113 }
114 if (!configuration->name) {
115 return DB_ERROR_UNKNOWN;
116 }
117 if (!configuration->value) {
118 return DB_ERROR_UNKNOWN;
119 }
120 return DB_OK;
121 }
122
123 /* DB CONFIGURATION LIST */
124
125
126
db_configuration_list_new(void)127 db_configuration_list_t* db_configuration_list_new(void) {
128 db_configuration_list_t* configuration_list =
129 (db_configuration_list_t*)calloc(1, sizeof(db_configuration_list_t));
130
131 return configuration_list;
132 }
133
db_configuration_list_free(db_configuration_list_t * configuration_list)134 void db_configuration_list_free(db_configuration_list_t* configuration_list) {
135 if (configuration_list) {
136 if (configuration_list->begin) {
137 db_configuration_t* this = configuration_list->begin;
138 db_configuration_t* next = NULL;
139
140 while (this) {
141 next = this->next;
142 db_configuration_free(this);
143 this = next;
144 }
145 }
146 free(configuration_list);
147 }
148 }
149
db_configuration_list_add(db_configuration_list_t * configuration_list,db_configuration_t * configuration)150 int db_configuration_list_add(db_configuration_list_t* configuration_list, db_configuration_t* configuration) {
151 if (!configuration_list) {
152 return DB_ERROR_UNKNOWN;
153 }
154 if (!configuration) {
155 return DB_ERROR_UNKNOWN;
156 }
157 if (db_configuration_not_empty(configuration)) {
158 return DB_ERROR_UNKNOWN;
159 }
160 if (configuration->next) {
161 return DB_ERROR_UNKNOWN;
162 }
163
164 if (configuration_list->begin) {
165 if (!configuration_list->end) {
166 return DB_ERROR_UNKNOWN;
167 }
168 configuration_list->end->next = configuration;
169 configuration_list->end = configuration;
170 }
171 else {
172 configuration_list->begin = configuration;
173 configuration_list->end = configuration;
174 }
175
176 return DB_OK;
177 }
178
db_configuration_list_find(const db_configuration_list_t * configuration_list,const char * name)179 const db_configuration_t* db_configuration_list_find(const db_configuration_list_t* configuration_list, const char* name) {
180 db_configuration_t* configuration;
181
182 if (!configuration_list) {
183 return NULL;
184 }
185 if (!name) {
186 return NULL;
187 }
188
189 configuration = configuration_list->begin;
190 while (configuration) {
191 if (db_configuration_not_empty(configuration)) {
192 return NULL;
193 }
194 if (!strcmp(configuration->name, name)) {
195 break;
196 }
197 configuration = configuration->next;
198 }
199
200 return configuration;
201 }
202