initial commit
[voxelands-alt.git] / src / nvp.c
blob5b431cc7b838e2597cc5ae7c143dca1760e448b9
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 "list.h"
21 #include "nvp.h"
22 #include "crypto.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
28 /* free memory of an nvp list, if data is non-zero, data is also freed */
29 void nvp_free(nvp_t **list, int data)
31 nvp_t *n;
32 while ((n = list_pop(list))) {
33 if (n->name)
34 free(n->name);
35 if (n->value)
36 free(n->value);
37 if (data && n->data)
38 free(n->data);
42 /* get a name/value pair */
43 nvp_t *nvp_get(nvp_t **list, char* name)
45 nvp_t *c = *list;
46 unsigned int h = hash(name);
47 while (c) {
48 if (c->h == h && !strcmp(c->name,name))
49 return c;
50 c = c->next;
52 return NULL;
55 /* get the value of a name/value pair */
56 char* nvp_get_str(nvp_t **list, char* name)
58 nvp_t *c = nvp_get(list,name);
59 if (c)
60 return c->value;
61 return NULL;
64 /* get the value of a name/value pair as an int value */
65 int nvp_get_int(nvp_t **list, char* name)
67 nvp_t *c = nvp_get(list,name);
68 if (c)
69 return strtol(c->value,NULL,10);
70 return 0;
73 /* get the value of a name/value pair as a float value */
74 float nvp_get_float(nvp_t **list, char* name)
76 nvp_t *c = nvp_get(list,name);
77 if (c)
78 return strtof(c->value,NULL);
79 return 0.0;
82 /* get the value of a name/value pair as a boolean value */
83 int nvp_get_bool(nvp_t **list, char* name)
85 nvp_t *c = nvp_get(list,name);
86 if (c && (!strcmp(c->value,"1") || !strcmp(c->value,"true")))
87 return 1;
88 return 0;
91 /* get a name/value pair's data value */
92 void *nvp_get_data(nvp_t **list, char* name)
94 nvp_t *c = nvp_get(list,name);
95 if (c)
96 return c->data;
97 return NULL;
100 /* set the value of a name/value pair */
101 void nvp_set(nvp_t **list, char* name, char* value, void *data)
103 nvp_t *c = *list;
104 unsigned int h = hash(name);
105 while (c) {
106 if (c->h == h && !strcmp(c->name,name)) {
107 if (c->value) {
108 free(c->value);
109 c->value = NULL;
111 if (value)
112 c->value = strdup(value);
113 if (data)
114 c->data = data;
115 return;
117 c = c->next;
120 if (!value && !data)
121 return;
123 c = malloc(sizeof(nvp_t));
124 c->name = strdup(name);
125 c->h = h;
126 if (value) {
127 c->value = strdup(value);
128 }else{
129 c->value = NULL;
131 c->data = data;
133 *list = list_push(list,c);
136 /* set a name/value pair to an int value */
137 void nvp_set_int(nvp_t **list, char* name, int value)
139 char str[20];
140 sprintf(str,"%d",value);
141 nvp_set(list,name,str,NULL);
144 /* set a name/value pair to a float value */
145 void nvp_set_float(nvp_t **list, char* name, float value)
147 char str[20];
148 sprintf(str,"%f",value);
149 nvp_set(list,name,str,NULL);