source: titan/titan/config.h @ 15280

Last change on this file since 15280 was 13482, checked in by nit, 12 years ago

[titan] add getconfigfloat (needed for usals)

File size: 4.6 KB
Line 
1#ifndef CONFIG_H
2#define CONFIG_H
3
4struct clist* addconfigtmp(char *key, char *value)
5{
6        return addlisttmp(config, key, value);
7}
8
9struct clist* addconfigdef(char *key, char *value)
10{
11        return addlistdef(config, key, value);
12}
13
14struct clist* addconfig(char *key, char *value)
15{
16        struct clist* node = NULL;
17
18        node = addlist(config, key, value);
19        if(node != NULL)
20                status.writeconfig = 1;
21
22        return node;
23}
24
25struct clist* addconfigscreentmp(char *key, struct skin *node)
26{
27        struct clist* ret = NULL;
28
29        if(key != NULL && node != NULL && node->ret != NULL)
30        {
31                if(node->hidden == NO)
32                        ret = addconfigtmp(key, node->ret);
33                else
34                        ret = addconfigtmp(key, "");
35        }
36       
37        return ret;
38}
39
40struct clist* addconfigscreentmpcheck(char *key, struct skin *node, char* check)
41{
42        if(ostrcmp(node->ret, check) == 0)
43                return addconfigtmp(key, "");
44        else
45                return addconfigscreentmp(key, node);
46}
47
48struct clist* addconfigscreen(char *key, struct skin *node)
49{
50        struct clist* ret = NULL;
51
52        if(key != NULL && node != NULL && node->ret != NULL)
53        {
54                if(node->hidden == NO)
55                        ret = addconfig(key, node->ret);
56                else
57                        delconfig(key);
58        }
59
60        return ret;
61}
62
63struct clist* addconfigscreencheck(char *key, struct skin *node, char* check)
64{
65        if(ostrcmp(node->ret, check) == 0)
66                delconfig(key);
67        else
68                return addconfigscreen(key, node);
69
70        return NULL;
71}
72
73struct clist* addconfiginttmp(char *key, int value)
74{
75        debug(1000, "in");
76        char* fileline = NULL;
77        struct clist* ret = NULL;
78
79        fileline = oitoa(value);
80        ret = addconfigtmp(key, fileline);
81
82        free(fileline);
83        debug(1000, "out");
84        return ret;
85}
86
87struct clist* addconfiglutmp(char *key, int value)
88{
89        debug(1000, "in");
90        char* fileline = NULL;
91        struct clist* ret = NULL;
92
93        fileline = olutoa(value);
94        ret = addconfigtmp(key, fileline);
95
96        free(fileline);
97        debug(1000, "out");
98        return ret;
99}
100
101struct clist* addconfigint(char *key, int value)
102{
103        debug(1000, "in");
104        char* fileline = NULL;
105        struct clist* ret = NULL;
106
107        fileline = oitoa(value);
108        ret = addconfig(key, fileline);
109
110        free(fileline);
111        debug(1000, "out");
112        return ret;
113}
114
115struct clist* addconfiglu(char *key, int value)
116{
117        debug(1000, "in");
118        char* fileline = NULL;
119        struct clist* ret = NULL;
120
121        fileline = olutoa(value);
122        ret = addconfig(key, fileline);
123
124        free(fileline);
125        debug(1000, "out");
126        return ret;
127}
128
129struct clist* addconfigintcheck(char *key, int value, int check)
130{
131        if(value == check)
132                delconfig(key);
133        else
134                return addconfigint(key, value);
135
136        return NULL;
137}
138
139int readconfig(const char *filename, struct clist** tmpconfig)
140{
141        debug(1000, "in");
142        FILE *fd = NULL;
143        char *fileline = NULL, *pos;
144
145        fileline = malloc(MINMALLOC);
146        if(fileline == NULL)
147        {
148                err("no memory");       
149                return 1;
150        }
151
152        fd = fopen(filename, "r");
153        if(fd == NULL)
154        {
155                perr("can't open %s", filename);
156                free(fileline);
157                return 1;
158        }
159
160        while(fgets(fileline, MINMALLOC, fd) != NULL)
161        {
162                if(fileline[0] == '#' || fileline[0] == '\n')
163                        continue;
164                if(fileline[strlen(fileline) - 1] == '\n')
165                        fileline[strlen(fileline) - 1] = '\0';
166                if(fileline[strlen(fileline) - 1] == '\r')
167                        fileline[strlen(fileline) - 1] = '\0';
168
169                pos = strchr(fileline, '=');
170                if(pos != NULL)
171                {
172                        pos[0] = '\0';
173                        addlist(tmpconfig, fileline, pos + 1);
174                }
175        }
176
177        fclose(fd);
178        free(fileline);
179        debug(1000, "out");
180        return 0;
181}
182
183int writeconfigtmp()
184{
185        int ret = 0;
186
187        if(writelisttmp(config) == 0)
188                status.writeconfig = 1;
189
190        return ret;
191}
192
193int writeconfig(const char *filename)
194{
195        return writelist(config, filename);
196}
197
198char* getconfigbyval(char *value, char *ext)
199{
200        return getlistbyval(config, value, ext);
201}
202
203char* getconfignotmp(char *key, char *ext)
204{
205        return getlistnotmp(config, key, ext);
206}
207
208char* getconfig(char *key, char *ext)
209{
210        return getlist(config, key, ext);
211}
212
213int getconfigint(char *key, char *ext)
214{
215        char *ret = NULL;
216
217        ret = getlist(config, key, ext);
218        if(ret != NULL)
219                return atoi(ret);
220        else
221                return 0;
222}
223
224float getconfigfloat(char *key, char *ext)
225{
226        char *ret = NULL;
227
228        ret = getlist(config, key, ext);
229        if(ret != NULL)
230                return atof(ret);
231        else
232                return 0;
233}
234
235unsigned long getconfiglu(char *key, char *ext)
236{
237        char *ret = NULL;
238
239        ret = getlist(config, key, ext);
240        if(ret != NULL)
241                return strtoul(ret, NULL, 10);
242        else
243                return 0;
244}
245
246void delconfigtmpall()
247{
248        dellisttmpall(config);
249}
250
251void delconfigtmp(char *key)
252{
253        dellisttmp(config, key);
254}
255
256void delconfig(char *key)
257{
258        status.writeconfig = 1;
259        dellist(config, key, 0);
260}
261
262void freeconfig()
263{
264        freelist(config);
265}
266
267int reloadconfig(char *filename)
268{
269        debug(1000, "in");
270        int ret = 0;
271
272        freeconfig();
273        ret = readconfig(filename, config);
274        if(ret != 0)
275        {
276                debug(1000, "out -> readconfig fail");
277                return 1;
278        }
279
280        debug(1000, "out");
281        return 0;
282}
283
284#endif
Note: See TracBrowser for help on using the repository browser.