source: titan/titan/config.h @ 43326

Last change on this file since 43326 was 23206, checked in by nit, 11 years ago

[titan] revert

File size: 4.9 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        char* fileline = NULL;
76        struct clist* ret = NULL;
77
78        fileline = oitoa(value);
79        ret = addconfigtmp(key, fileline);
80
81        free(fileline);
82        return ret;
83}
84
85struct clist* addconfigllutmp(char *key, uint64_t value)
86{
87        char* fileline = NULL;
88        struct clist* ret = NULL;
89
90        fileline = ollutoa(value);
91        ret = addconfigtmp(key, fileline);
92
93        free(fileline);
94        return ret;
95}
96
97struct clist* addconfiglutmp(char *key, unsigned long value)
98{
99        char* fileline = NULL;
100        struct clist* ret = NULL;
101
102        fileline = olutoa(value);
103        ret = addconfigtmp(key, fileline);
104
105        free(fileline);
106        return ret;
107}
108
109struct clist* addconfigint(char *key, int value)
110{
111        char* fileline = NULL;
112        struct clist* ret = NULL;
113
114        fileline = oitoa(value);
115        ret = addconfig(key, fileline);
116
117        free(fileline);
118        return ret;
119}
120
121struct clist* addconfigllu(char *key, uint64_t value)
122{
123        char* fileline = NULL;
124        struct clist* ret = NULL;
125
126        fileline = ollutoa(value);
127        ret = addconfig(key, fileline);
128
129        free(fileline);
130        return ret;
131}
132
133struct clist* addconfiglu(char *key, unsigned long value)
134{
135        char* fileline = NULL;
136        struct clist* ret = NULL;
137
138        fileline = olutoa(value);
139        ret = addconfig(key, fileline);
140
141        free(fileline);
142        return ret;
143}
144
145struct clist* addconfigintcheck(char *key, int value, int check)
146{
147        if(value == check)
148                delconfig(key);
149        else
150                return addconfigint(key, value);
151
152        return NULL;
153}
154
155int readconfig(const char *filename, struct clist** tmpconfig)
156{
157        FILE *fd = NULL;
158        char *fileline = NULL, *pos;
159        int len = 0;
160
161        fileline = malloc(MINMALLOC);
162        if(fileline == NULL)
163        {
164                err("no memory");       
165                return 1;
166        }
167
168        fd = fopen(filename, "r");
169        if(fd == NULL)
170        {
171                perr("can't open %s", filename);
172                free(fileline);
173                return 1;
174        }
175
176        while(fgets(fileline, MINMALLOC, fd) != NULL)
177        {
178                if(fileline[0] == '#' || fileline[0] == '\n')
179                        continue;
180                len = strlen(fileline) - 1;
181                if(len >= 0 && fileline[len] == '\n')
182                        fileline[len] = '\0';
183                len--;
184                if(len >= 0 && fileline[len] == '\r')
185                        fileline[len] = '\0';
186
187                pos = strchr(fileline, '=');
188                if(pos != NULL)
189                {
190                        pos[0] = '\0';
191                        addlist(tmpconfig, fileline, pos + 1);
192                }
193        }
194
195        fclose(fd);
196        free(fileline);
197        return 0;
198}
199
200int writeconfigtmp()
201{
202        int ret = 0;
203
204        if(writelisttmp(config) == 0)
205                status.writeconfig = 1;
206
207        return ret;
208}
209
210int writeconfig(const char *filename)
211{
212        return writelist(config, filename);
213}
214
215char* getconfigbyval(char *value, char *ext)
216{
217        return getlistbyval(config, value, ext);
218}
219
220char* getconfignotmp(char *key, char *ext)
221{
222        return getlistnotmp(config, key, ext);
223}
224
225char* getconfig(char *key, char *ext)
226{
227        return getlist(config, key, ext);
228}
229
230int getconfigint(char *key, char *ext)
231{
232        char *ret = NULL;
233
234        ret = getlist(config, key, ext);
235        if(ret != NULL)
236                return atoi(ret);
237        else
238                return 0;
239}
240
241float getconfigfloat(char *key, char *ext)
242{
243        char *ret = NULL;
244
245        ret = getlist(config, key, ext);
246        if(ret != NULL)
247                return atof(ret);
248        else
249                return 0;
250}
251
252uint64_t getconfigllu(char *key, char *ext)
253{
254        char *ret = NULL;
255
256        ret = getlist(config, key, ext);
257        if(ret != NULL)
258                return strtoull(ret, NULL, 10);
259        else
260                return 0;
261}
262
263unsigned long getconfiglu(char *key, char *ext)
264{
265        char *ret = NULL;
266
267        ret = getlist(config, key, ext);
268        if(ret != NULL)
269                return strtoul(ret, NULL, 10);
270        else
271                return 0;
272}
273
274void delconfigtmpall()
275{
276        dellisttmpall(config);
277}
278
279void delconfigtmp(char *key)
280{
281        dellisttmp(config, key);
282}
283
284void delconfig(char *key)
285{
286        status.writeconfig = 1;
287        dellist(config, key, 0);
288}
289
290void freeconfig()
291{
292        freelist(config);
293}
294
295int reloadconfig(char *filename)
296{
297        int ret = 0;
298
299        freeconfig();
300        ret = readconfig(filename, config);
301        if(ret != 0)
302        {
303                err("readconfig fail");
304                return 1;
305        }
306
307        return 0;
308}
309
310#endif
Note: See TracBrowser for help on using the repository browser.