config.c
[voxelands-alt.git] / src / lib / nvp.c
blob66c06a518855c43ab61bee62ded19a410acc2f70
1 /************************************************************************
2 * nvp.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
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 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #include "list.h"
22 #include "nvp.h"
23 #include "crypto.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 /* comparison functions:
30 * return 0 to insert
31 * return 1 to insert later
32 * return -1 to not insert at all
34 static int nvp_insert_cmp(void *e1, void *e2)
36 nvp_t *n1 = e1;
37 nvp_t *n2 = e2;
39 if (n2->h > n1->h)
40 return 0;
41 return 1;
44 /* free memory of an nvp list, if data is non-zero, data is also freed */
45 void nvp_free(nvp_t **list, int data)
47 nvp_t *n;
48 while ((n = list_pop(list))) {
49 if (n->name)
50 free(n->name);
51 if (n->value)
52 free(n->value);
53 if (data && n->data)
54 free(n->data);
58 /* get a name/value pair */
59 nvp_t *nvp_get(nvp_t **list, char* name)
61 nvp_t *c = *list;
62 unsigned int h = hash(name);
63 while (c) {
64 if (c->h > h)
65 return NULL;
66 if (c->h == h && !strcmp(c->name,name))
67 return c;
68 c = c->next;
70 return NULL;
73 /* get the value of a name/value pair */
74 char* nvp_get_str(nvp_t **list, char* name)
76 nvp_t *c = nvp_get(list,name);
77 if (c)
78 return c->value;
79 return NULL;
82 /* get the value of a name/value pair as an int value */
83 int nvp_get_int(nvp_t **list, char* name)
85 nvp_t *c = nvp_get(list,name);
86 if (c)
87 return strtol(c->value,NULL,10);
88 return 0;
91 /* get the value of a name/value pair as a float value */
92 float nvp_get_float(nvp_t **list, char* name)
94 nvp_t *c = nvp_get(list,name);
95 if (c)
96 return strtof(c->value,NULL);
97 return 0.0;
100 /* get the value of a name/value pair as a boolean value */
101 int nvp_get_bool(nvp_t **list, char* name)
103 nvp_t *c = nvp_get(list,name);
104 if (c && (!strcmp(c->value,"1") || !strcmp(c->value,"true")))
105 return 1;
106 return 0;
109 /* get a name/value pair's data value */
110 void *nvp_get_data(nvp_t **list, char* name)
112 nvp_t *c = nvp_get(list,name);
113 if (c)
114 return c->data;
115 return NULL;
118 /* set the value of a name/value pair */
119 void nvp_set(nvp_t **list, char* name, char* value, void *data)
121 nvp_t *c = *list;
122 unsigned int h = hash(name);
124 while (c) {
125 if (c->h > h)
126 break;
127 if (c->h == h && !strcmp(c->name,name)) {
128 if (c->value) {
129 free(c->value);
130 c->value = NULL;
132 if (value)
133 c->value = strdup(value);
134 if (data)
135 c->data = data;
136 return;
138 c = c->next;
141 if (!value && !data)
142 return;
144 c = malloc(sizeof(nvp_t));
145 c->name = strdup(name);
146 c->h = h;
147 if (value) {
148 c->value = strdup(value);
149 }else{
150 c->value = NULL;
152 c->data = data;
154 *list = list_insert_cmp(list,c,nvp_insert_cmp);
157 /* set a name/value pair to an int value */
158 void nvp_set_int(nvp_t **list, char* name, int value)
160 char str[20];
161 sprintf(str,"%d",value);
162 nvp_set(list,name,str,NULL);
165 /* set a name/value pair to a float value */
166 void nvp_set_float(nvp_t **list, char* name, float value)
168 char str[20];
169 sprintf(str,"%f",value);
170 nvp_set(list,name,str,NULL);