source: titan/titan/list.h @ 39052

Last change on this file since 39052 was 23286, checked in by nit, 11 years ago

[titan] cleanup

File size: 11.3 KB
Line 
1#ifndef LIST_H
2#define LIST_H
3
4void debuglist(struct clist* node, char* search)
5{
6        while(node != NULL)
7        {
8                if(search != NULL)
9                {       
10                        if(ostrncmp(node->key, search, strlen(search)) == 0)
11                                printf("key=%s, tmp=%s, val=%s, def=%s\n", node->key, node->tmp, node->value, node->def);
12                       
13                }
14                else
15                        printf("key=%s, tmp=%s, val=%s, def=%s\n", node->key, node->tmp, node->value, node->def);
16                node = node->next;
17        }
18}
19
20struct clist* addlisttmp(struct clist **clist, char *key1, char *value1)
21{
22        m_lock(&status.clistmutex, 12);
23        struct clist *newnode = NULL, *node = NULL, *prev = NULL;
24        char *key = NULL, *value = NULL;
25        int hash = 0;
26
27        if(key1 == NULL || value1 == NULL)
28        {
29                m_unlock(&status.clistmutex, 12);
30                return NULL;
31        }
32
33        key = ostrcat(key1, NULL, 0, 0);
34        if(key == NULL)
35        {
36                err("no memory");
37                m_unlock(&status.clistmutex, 12);
38                return NULL;
39        }
40
41        value = ostrcat(value1, NULL, 0, 0);
42        if(value == NULL)
43        {
44                err("no memory");
45                free(key);
46                m_unlock(&status.clistmutex, 12);
47                return NULL;
48        }
49
50        hash = key1[0] - 96;
51        if(hash < 0 || hash >= LISTHASHSIZE) hash = 0;
52        node = clist[hash];
53        prev = clist[hash];
54
55        newnode = (struct clist*)malloc(sizeof(struct clist)); 
56        if(newnode == NULL)
57        {
58                err("no memory");
59                free(key);
60                free(value);
61                m_unlock(&status.clistmutex, 12);
62                return NULL;
63        }
64
65        memset(newnode, 0, sizeof(struct clist));
66        newnode->key = key;
67        newnode->tmp = value;
68
69        if(node != NULL)
70        {
71                while(node != NULL)
72                {
73                        if(ostrcmp(node->key, key) == 0)
74                        {
75                                free(key); key = NULL;
76                                free(newnode); newnode = NULL;
77                                if(ostrcmp(node->tmp, value) == 0)
78                                {
79                                        free(value);
80                                        m_unlock(&status.clistmutex, 12);
81                                        return NULL;
82                                }
83                                free(node->tmp);
84                                node->tmp = value;
85                                m_unlock(&status.clistmutex, 12);
86                                return newnode;
87                        }
88                        prev = node;
89                        node = node->next;
90                }
91                prev->next = newnode;
92        }
93        else
94                clist[hash] = newnode;
95
96        m_unlock(&status.clistmutex, 12);
97        return newnode;
98}
99
100struct clist* addlistdef(struct clist **clist, char *key1, char *value1)
101{
102        m_lock(&status.clistmutex, 12);
103        struct clist *newnode = NULL, *node = NULL, *prev = NULL;
104        char *key = NULL, *value = NULL;
105        int hash = 0;
106
107        if(key1 == NULL || value1 == NULL)
108        {
109                m_unlock(&status.clistmutex, 12);
110                return NULL;
111        }
112
113        key = ostrcat(key1, NULL, 0, 0);
114        if(key == NULL)
115        {
116                err("no memory");
117                m_unlock(&status.clistmutex, 12);
118                return NULL;
119        }
120
121        value = ostrcat(value1, NULL, 0, 0);
122        if(value == NULL)
123        {
124                err("no memory");
125                free(key);
126                m_unlock(&status.clistmutex, 12);
127                return NULL;
128        }
129
130        hash = key1[0] - 96;
131        if(hash < 0 || hash >= LISTHASHSIZE) hash = 0;
132        node = clist[hash];
133        prev = clist[hash];
134
135        newnode = (struct clist*)malloc(sizeof(struct clist)); 
136        if(newnode == NULL)
137        {
138                err("no memory");
139                free(key);
140                free(value);
141                m_unlock(&status.clistmutex, 12);
142                return NULL;
143        }
144
145        memset(newnode, 0, sizeof(struct clist));
146        newnode->key = key;
147        newnode->def = value;
148
149        if(node != NULL)
150        {
151                while(node != NULL)
152                {
153                        if(ostrcmp(node->key, key) == 0)
154                        {
155                                free(key); key = NULL;
156                                free(newnode); newnode = NULL;
157                                if(ostrcmp(node->def, value) == 0)
158                                {
159                                        free(value);
160                                        m_unlock(&status.clistmutex, 12);
161                                        return NULL;
162                                }
163                                free(node->def);
164                                node->def = value;
165                                m_unlock(&status.clistmutex, 12);
166                                return newnode;
167                        }
168                        prev = node;
169                        node = node->next;
170                }
171                prev->next = newnode;
172        }
173        else
174                clist[hash] = newnode;
175
176        m_unlock(&status.clistmutex, 12);
177        return newnode;
178}
179
180struct clist* addlist(struct clist **clist, char *key1, char *value1)
181{
182        m_lock(&status.clistmutex, 12);
183        struct clist *newnode = NULL, *node = NULL, *prev = NULL;
184        char *key = NULL, *value = NULL;
185        int hash = 0;
186
187        if(key1 == NULL || value1 == NULL)
188        {
189                m_unlock(&status.clistmutex, 12);
190                return NULL;
191        }
192
193        key = ostrcat(key1, NULL, 0, 0);
194        if(key == NULL)
195        {
196                err("no memory");
197                m_unlock(&status.clistmutex, 12);
198                return NULL;
199        }
200
201        value = ostrcat(value1, NULL, 0, 0);
202        if(value == NULL)
203        {
204                err("no memory");
205                free(key);
206                m_unlock(&status.clistmutex, 12);
207                return NULL;
208        }
209
210        hash = key1[0] - 96;
211        if(hash < 0 || hash >= LISTHASHSIZE) hash = 0;
212        node = clist[hash];
213        prev = clist[hash];
214
215        newnode = (struct clist*)malloc(sizeof(struct clist)); 
216        if(newnode == NULL)
217        {
218                err("no memory");
219                free(key);
220                free(value);
221                m_unlock(&status.clistmutex, 12);
222                return NULL;
223        }
224
225        memset(newnode, 0, sizeof(struct clist));
226        newnode->key = key;
227        newnode->value = value;
228
229        if(node != NULL)
230        {
231                while(node != NULL)
232                {
233                        if(ostrcmp(node->key, key) == 0)
234                        {
235                                free(key); key = NULL;
236                                free(newnode); newnode = NULL;
237                                if(ostrcmp(node->value, value) == 0)
238                                {
239                                        free(value);
240                                        m_unlock(&status.clistmutex, 12);
241                                        return NULL;
242                                }
243                                if(node->value == NULL && ostrcmp(node->def, value) == 0)
244                                {
245                                        free(value);
246                                        m_unlock(&status.clistmutex, 12);
247                                        return NULL;
248                                }
249                                free(node->value);
250                                node->value = value;
251                                m_unlock(&status.clistmutex, 12);
252                                return node;
253                        }
254                        prev = node;
255                        node = node->next;
256                }
257                prev->next = newnode;
258        }
259        else
260                clist[hash] = newnode;
261
262        m_unlock(&status.clistmutex, 12);
263        return newnode;
264}
265
266int writelist(struct clist **clist, const char *filename)
267{
268        m_lock(&status.clistmutex, 12);
269        FILE *fd = NULL;
270        struct clist *node = NULL;
271        int ret = 0, i;
272
273        fd = fopen(filename, "w+");
274        if(fd == NULL)
275        {
276                perr("can't open %s", filename);
277                m_unlock(&status.clistmutex, 12);
278                return 1;
279        }
280
281        for(i = 0; i < LISTHASHSIZE; i++)
282        {
283                node = clist[i];
284
285                while(node != NULL)
286                {
287                        if(node->value == NULL)
288                        {
289                                node = node->next;
290                                continue;
291                        }
292                        if(ostrcmp(node->value, node->def) == 0 || strlen(node->value) == 0)
293                        {
294                                node = node->next;
295                                continue;
296                        }
297                        ret = fprintf(fd, "%s=%s\n", node->key, node->value);
298                        if(ret < 0)
299                        {
300                                perr("writting file %s (ret = %d)", filename, ret);
301                        }
302                        node = node->next;
303                }
304        }
305       
306        fclose(fd);
307        m_unlock(&status.clistmutex, 12);
308        return 0;
309}
310
311char* getlistbyval(struct clist **clist, char *value, char *ext)
312{
313        m_lock(&status.clistmutex, 12);
314        struct clist *node = NULL;
315        char* tmpstr = NULL, *tmpval = NULL;
316        int i;
317
318        if(ext != NULL)
319                tmpstr = ostrcat(value, ext, 0, 0);
320        else
321                tmpstr = ostrcat(NULL, value, 0, 0);
322
323        for(i = 0; i < LISTHASHSIZE; i++)
324        {
325                node = clist[i];
326                while(node != NULL)
327                {
328                        if(node->tmp != NULL)
329                                tmpval = node->tmp;
330                        else if(node->value != NULL)
331                                tmpval = node->value;
332                        else if(node->def != NULL)
333                                tmpval = node->def;
334
335                        if(ostrcmp(tmpval, tmpstr) == 0)
336                        {
337                                free(tmpstr);
338                                m_unlock(&status.clistmutex, 12);
339                                return node->key;
340                        }
341
342                        node = node->next;
343                }
344        }
345        free(tmpstr);
346        debug(100, "value not found (%s)", value);
347        m_unlock(&status.clistmutex, 12);
348        return NULL;
349}
350
351char* getlistnotmp(struct clist **clist, char *key, char *ext)
352{
353        m_lock(&status.clistmutex, 12);
354        struct clist *node = NULL;
355        char* tmpstr = NULL;
356        int hash = 0;
357
358        if(key == NULL)
359        {
360                m_unlock(&status.clistmutex, 12);
361                return NULL;
362        }
363        if(ext != NULL)
364                tmpstr = ostrcat(key, ext, 0, 0);
365        else
366                tmpstr = ostrcat(NULL, key, 0, 0);
367
368        hash = key[0] - 96;
369        if(hash < 0 || hash >= LISTHASHSIZE) hash = 0;
370        node = clist[hash];
371
372        while(node != NULL)
373        {
374                if(ostrcmp(node->key, tmpstr) == 0)
375                {
376                        free(tmpstr);
377                        if(node->value != NULL)
378                        {
379                                m_unlock(&status.clistmutex, 12);
380                                return node->value;
381                        }
382                        else
383                        {
384                                m_unlock(&status.clistmutex, 12);
385                                return node->def;
386                        }
387                }
388
389                node = node->next;
390        }
391        free(tmpstr);
392        debug(100, "key not found (%s)", key);
393        m_unlock(&status.clistmutex, 12);
394        return NULL;
395}
396
397char* getlist(struct clist **clist, char *key, char *ext)
398{
399        m_lock(&status.clistmutex, 12);
400        struct clist *node = NULL;
401        char* tmpstr = NULL;
402        int hash = 0;
403       
404        if(key == NULL)
405        {
406                m_unlock(&status.clistmutex, 12);
407                return NULL;
408        }
409        if(ext != NULL)
410                tmpstr = ostrcat(key, ext, 0, 0);
411        else
412                tmpstr = ostrcat(NULL, key, 0, 0);
413
414        hash = key[0] - 96;
415        if(hash < 0 || hash >= LISTHASHSIZE) hash = 0;
416        node = clist[hash];
417
418        while(node != NULL)
419        {
420                if(ostrcmp(node->key, tmpstr) == 0)
421                {
422                        free(tmpstr);
423                        if(node->tmp != NULL && strlen(node->tmp) > 0)
424                        {
425                                m_unlock(&status.clistmutex, 12);
426                                return node->tmp;
427                        }
428                        else if(node->value != NULL)
429                        {
430                                m_unlock(&status.clistmutex, 12);
431                                return node->value;
432                        }
433                        else
434                        {
435                                m_unlock(&status.clistmutex, 12);
436                                return node->def;
437                        }
438                }
439
440                node = node->next;
441        }
442        free(tmpstr);
443        debug(100, "key not found (%s)", key);
444        m_unlock(&status.clistmutex, 12);
445        return NULL;
446}
447
448//flag 0: set lock
449//flag 1: no lock
450void dellist(struct clist **clist, char *key, int flag)
451{
452        if(flag == 0) m_lock(&status.clistmutex, 12);
453        struct clist *node = NULL, *prev = NULL;
454        int hash = 0;
455
456        if(key == NULL)
457        {
458                if(flag == 0) m_unlock(&status.clistmutex, 12);
459                return;
460        }
461        hash = key[0] - 96;
462        if(hash < 0 || hash >= LISTHASHSIZE) hash = 0;
463        node = clist[hash];
464        prev = clist[hash];
465
466        while(node != NULL)
467        {
468                if(ostrcmp(node->key, key) == 0)
469                {
470                        if(node == clist[hash])
471                                clist[hash] = node->next;
472                        else
473                                prev->next = node->next;
474
475                        free(node->key);
476                        node->key = NULL;
477
478                        free(node->value);
479                        node->value = NULL;
480
481                        free(node->def);
482                        node->def = NULL;
483
484                        free(node->tmp);
485                        node->tmp = NULL;
486
487                        free(node);
488                        node = NULL;
489                        break;
490                }
491
492                prev = node;
493                node = node->next;
494        }
495        if(flag == 0) m_unlock(&status.clistmutex, 12);
496}
497
498void dellisttmpall(struct clist **clist)
499{
500        m_lock(&status.clistmutex, 12);
501        struct clist *node = NULL, *prev = NULL;
502        int i;
503
504        for(i = 0; i < LISTHASHSIZE; i++)
505        {
506                node = clist[i];
507                while(node != NULL)
508                {
509                        free(node->tmp);
510                        node->tmp = NULL;
511
512                        if(node->tmp != NULL && strlen(node->tmp) == 0)
513                        {
514                                free(node->tmp);
515                                node->tmp = NULL;
516                        }
517
518                        if(node->value == NULL && node->tmp == NULL && node->def == NULL)
519                                dellist(clist, node->key, 1);
520
521                        prev = node;
522                        node = node->next;
523                }
524        }
525        m_unlock(&status.clistmutex, 12);
526}
527
528void dellisttmp(struct clist **clist, char *key)
529{
530        m_lock(&status.clistmutex, 12);
531        struct clist *node = NULL, *prev = NULL;
532        int hash = 0;
533
534        if(key == NULL)
535        {
536                m_unlock(&status.clistmutex, 12);
537                return;
538        }
539        hash = key[0] - 96;
540        if(hash < 0 || hash >= LISTHASHSIZE) hash = 0;
541        node = clist[hash];
542        prev = clist[hash];
543
544        while(node != NULL)
545        {
546                if(ostrcmp(node->key, key) == 0)
547                {
548                        free(node->tmp);
549                        node->tmp = NULL;
550                        break;
551                }
552
553                prev = node;
554                node = node->next;
555        }
556        m_unlock(&status.clistmutex, 12);
557}
558
559void freelist(struct clist** clist)
560{
561        m_lock(&status.clistmutex, 12);
562        struct clist *node = NULL, *prev = NULL;
563        int i;
564
565        for(i = 0; i < LISTHASHSIZE; i++)
566        {
567                node = clist[i];
568                while(node != NULL)
569                {
570                        prev = node;
571                        node = node->next;
572                        if(prev != NULL)
573                                dellist(clist, prev->key, 1);
574                }
575        }
576        m_unlock(&status.clistmutex, 12);
577}
578
579int writelisttmp(struct clist **clist)
580{
581        m_lock(&status.clistmutex, 12);
582        struct clist *node = NULL;
583        int ret = 1, i;
584
585        for(i = 0; i < LISTHASHSIZE; i++)
586        {
587                node = clist[i];
588                while(node != NULL)
589                {
590                        if(node->tmp != NULL && strlen(node->tmp) == 0)
591                        {
592                                free(node->tmp); node->tmp = NULL;
593                                free(node->value); node->value = NULL;
594                                ret = 0;
595                        }
596                        if(node->value == NULL && node->tmp == NULL && node->def == NULL)
597                        {
598                                dellist(clist, node->key, 1);
599                                node = node->next;
600                                ret = 0;
601                                continue;
602                        }
603                        if(ostrcmp(node->tmp, node->def) == 0)
604                        {
605                                node = node->next;
606                                continue;
607                        }
608                        if(node->tmp != NULL)
609                        {
610                                free(node->value);
611                                node->value = node->tmp;
612                                node->tmp = NULL;
613                                ret = 0;
614                        }
615                        node = node->next;
616                }
617        }
618
619        m_unlock(&status.clistmutex, 12);
620        return ret;
621}
622
623#endif
Note: See TracBrowser for help on using the repository browser.