source: titan/titan/list.h @ 10958

Last change on this file since 10958 was 10500, checked in by nit, 12 years ago

[titan] save list with mutex, add message queue

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