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 ************************************************************************/
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
)
32 while ((n
= list_pop(list
))) {
42 /* get a name/value pair */
43 nvp_t
*nvp_get(nvp_t
**list
, char* name
)
46 unsigned int h
= hash(name
);
48 if (c
->h
== h
&& !strcmp(c
->name
,name
))
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
);
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
);
69 return strtol(c
->value
,NULL
,10);
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
);
78 return strtof(c
->value
,NULL
);
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")))
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
);
100 /* set the value of a name/value pair */
101 void nvp_set(nvp_t
**list
, char* name
, char* value
, void *data
)
104 unsigned int h
= hash(name
);
106 if (c
->h
== h
&& !strcmp(c
->name
,name
)) {
112 c
->value
= strdup(value
);
123 c
= malloc(sizeof(nvp_t
));
124 c
->name
= strdup(name
);
127 c
->value
= strdup(value
);
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
)
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
)
148 sprintf(str
,"%f",value
);
149 nvp_set(list
,name
,str
,NULL
);