Debian Codegraph @

src/core/xep/tool_datalist.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 Colin DIDIER
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <string.h>
19
20 #include "module.h"
21
22 #include "tool_datalist.h"
23
24 DATALIST_REC *
25 datalist_find(DATALIST *dl, XMPP_SERVER_REC *server, const char *jid)
26 {
27 GSList *tmp;
28 DATALIST_REC *rec;
29
30 for (tmp = dl->list; tmp != NULL; tmp = tmp->next) {
31 rec = tmp->data;
32 if (rec->server == server && strcmp(rec->jid, jid) == 0)
33 return rec;
34 }
35 return NULL;
36 }
37
38 DATALIST_REC *
39 datalist_add(DATALIST *dl, XMPP_SERVER_REC *server, const char *jid,
40 void *data)
41 {
42 DATALIST_REC *rec;
43
44 if ((rec = datalist_find(dl, server, jid)) != NULL) {
45 dl->freedata_func(rec);
46 rec->data = data;
47 } else {
48 rec = g_new0(DATALIST_REC, 1);
49 rec->server = server;
50 rec->jid = g_strdup(jid);
51 rec->data = data;
52 dl->list = g_slist_prepend(dl->list, rec);
53 }
54 return rec;
55 }
56
57 void
58 datalist_free(DATALIST *dl, DATALIST_REC *rec)
59 {
60 dl->list = g_slist_remove(dl->list, rec);
61 g_free(rec->jid);
62 dl->freedata_func(rec);
63 g_free(rec);
64 }
65
66 void
67 datalist_remove(DATALIST *dl, XMPP_SERVER_REC *server, const char *jid)
68 {
69 DATALIST_REC *rec;
70
71 if ((rec = datalist_find(dl, server, jid)) != NULL)
72 datalist_free(dl, rec);
73 }
74
75 void
76 datalist_cleanup(DATALIST *dl, XMPP_SERVER_REC *server)
77 {
78 GSList *tmp, *next;
79 DATALIST_REC *rec;
80
81 for (tmp = dl->list; tmp != NULL; tmp = next) {
82 next = tmp->next;
83 rec = tmp->data;
84 if (server == NULL || rec->server == server)
85 datalist_free(dl, rec);
86 }
87 }
88
89 static void
90 dummy_freedata_func(DATALIST_REC *rec)
91 {
92 }
93
94 DATALIST *
95 datalist_new(void (*freedata_func)(DATALIST_REC *))
96 {
97 DATALIST *dl;
98
99 dl = g_new0(DATALIST, 1);
100 dl->list = NULL;
101 dl->freedata_func = freedata_func == NULL ?
102 dummy_freedata_func : freedata_func;
103 return dl;
104 }
105
106 void
107 datalist_destroy(DATALIST *dl)
108 {
109 datalist_cleanup(dl, NULL);
110 g_free(dl);
111 }