1 /************************************************************************
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 ************************************************************************/
29 /* comparison functions:
31 * return 1 to insert later
32 * return -1 to not insert at all
34 static int nvp_insert_cmp(void *e1
, void *e2
)
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
)
48 while ((n
= list_pop(list
))) {
58 /* get a name/value pair */
59 nvp_t
*nvp_get(nvp_t
**list
, char* name
)
62 unsigned int h
= hash(name
);
66 if (c
->h
== h
&& !strcmp(c
->name
,name
))
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
);
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
);
87 return strtol(c
->value
,NULL
,10);
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
);
96 return strtof(c
->value
,NULL
);
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")))
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
);
118 /* set the value of a name/value pair */
119 void nvp_set(nvp_t
**list
, char* name
, char* value
, void *data
)
122 unsigned int h
= hash(name
);
127 if (c
->h
== h
&& !strcmp(c
->name
,name
)) {
133 c
->value
= strdup(value
);
144 c
= malloc(sizeof(nvp_t
));
145 c
->name
= strdup(name
);
148 c
->value
= strdup(value
);
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
)
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
)
169 sprintf(str
,"%f",value
);
170 nvp_set(list
,name
,str
,NULL
);