Debian Codegraph @

src/d2dbs/charlock.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2001 faster ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"
19 #include "setup.h"
20 #include "charlock.h"
21
22 #include <limits>
23 #include <cstring>
24 #include <cctype>
25
26 #include "compat/strcasecmp.h"
27 #include "common/xalloc.h"
28 #include "common/introtate.h"
29 #include "common/xstring.h"
30 #include "common/setup_after.h"
31
32 namespace pvpgn
33 {
34
35 namespace d2dbs
36 {
37
38 /* FIXME: for simplification, no multiple realm support now */
39
40 /* local functions */
41 static int cl_insert_to_gsq_list(unsigned int gsid, t_charlockinfo *pcl);
42 static int cl_delete_from_gsq_list(t_charlockinfo *pcl);
43 static unsigned int string_hash(char const *string);
44
45
46 /* variables */
47 static unsigned int clitbl_len = 0;
48 static unsigned int gsqtbl_len = 0;
49 static t_charlockinfo * * clitbl = NULL;
50 static t_charlockinfo * * gsqtbl = NULL;
51
52
53 int cl_init(unsigned int tbllen, unsigned int maxgs)
54 {
55 if (!tbllen || !maxgs) return -1;
56 cl_destroy();
57
58 clitbl = (t_charlockinfo**)xmalloc(tbllen*sizeof(t_charlockinfo**));
59 gsqtbl = (t_charlockinfo**)xmalloc(maxgs*sizeof(t_charlockinfo**));
60 std::memset(clitbl, 0, tbllen*sizeof(t_charlockinfo**));
61 std::memset(gsqtbl, 0, maxgs*sizeof(t_charlockinfo**));
62 clitbl_len = tbllen;
63 gsqtbl_len = maxgs;
64 return 0;
65 }
66
67
68 int cl_destroy(void)
69 {
70 unsigned int i;
71 t_charlockinfo * ptl, *ptmp;
72
73 if (clitbl) {
74 for (i = 0; i < clitbl_len; i++) {
75 ptl = clitbl[i];
76 while (ptl) {
77 ptmp = ptl;
78 ptl = ptl->next;
79 xfree(ptmp);
80 }
81 }
82 xfree(clitbl);
83 }
84 if (gsqtbl) xfree(gsqtbl);
85 clitbl = gsqtbl = NULL;
86 clitbl_len = gsqtbl_len = 0;
87 return 0;
88 }
89
90
91 int cl_query_charlock_status(unsigned char *charname,
92 unsigned char *realmname, unsigned int *gsid)
93 {
94 t_charlockinfo *pcl;
95 unsigned int hashval;
96
97 if (!charname || !realmname) return -1;
98 if (!clitbl_len || !gsqtbl) return -1;
99 if (std::strlen((char*)charname) >= MAX_CHARNAME_LEN) return -1;
100
101 hashval = string_hash((char*)charname) % clitbl_len;
102 pcl = clitbl[hashval];
103 while (pcl)
104 {
105 if (strcasecmp((char*)pcl->charname, (char*)charname) == 0) {
106 *gsid = pcl->gsid;
107 return 1; /* found the char, it is locked */
108 }
109 pcl = pcl->next;
110 }
111 return 0; /* not found, it is unlocked */
112 }
113
114
115 int cl_lock_char(unsigned char *charname,
116 unsigned char *realmname, unsigned int gsid)
117 {
118 t_charlockinfo *pcl, *ptmp;
119 unsigned int hashval;
120
121 if (!charname || !realmname) return -1;
122 if (!clitbl_len || !gsqtbl) return -1;
123 if (std::strlen((char*)charname) >= MAX_CHARNAME_LEN) return -1;
124
125 hashval = string_hash((char*)charname) % clitbl_len;
126 pcl = clitbl[hashval];
127 ptmp = NULL;
128 while (pcl)
129 {
130 if (strcasecmp((char*)pcl->charname, (char*)charname) == 0)
131 return 0; /* found the char is already locked */
132 ptmp = pcl;
133 pcl = pcl->next;
134 }
135
136 /* not found, locked it */
137 pcl = (t_charlockinfo*)xmalloc(sizeof(t_charlockinfo));
138 std::memset(pcl, 0, sizeof(t_charlockinfo));
139 std::strncpy((char*)pcl->charname, (char*)charname, MAX_CHARNAME_LEN - 1);
140 std::strncpy((char*)pcl->realmname, (char*)realmname, MAX_REALMNAME_LEN - 1);
141 pcl->gsid = gsid;
142
143 /* add to hash table link list */
144 if (ptmp) ptmp->next = pcl;
145 else clitbl[hashval] = pcl;
146 /* add to gs specified link list */
147 cl_insert_to_gsq_list(gsid, pcl);
148
149 return 0;
150 }
151
152
153 int cl_unlock_char(unsigned char *charname, unsigned char *realmname, unsigned int gsid)
154 {
155 t_charlockinfo *pcl, *ptmp;
156 unsigned int hashval;
157
158 if (!charname || !realmname) return -1;
159 if (!clitbl_len || !gsqtbl) return 0;
160 if (std::strlen((char*)charname) >= MAX_CHARNAME_LEN) return -1;
161
162 hashval = string_hash((char*)charname) % clitbl_len;
163 pcl = clitbl[hashval];
164 ptmp = NULL;
165 while (pcl)
166 {
167 if ((strcasecmp((char*)pcl->charname, (char*)charname) == 0) && (pcl->gsid == gsid)) {
168 cl_delete_from_gsq_list(pcl);
169 if (ptmp) ptmp->next = pcl->next;
170 else clitbl[hashval] = pcl->next;
171 xfree(pcl);
172 return 0;
173 }
174 ptmp = pcl;
175 pcl = pcl->next;
176 }
177 return 0;
178 }
179
180
181 int cl_unlock_all_char_by_gsid(unsigned int gsid)
182 {
183 unsigned int index_pos;
184 t_charlockinfo *pcl, *pnext;
185
186 index_pos = gsid % gsqtbl_len;
187 pcl = gsqtbl[index_pos];
188 while (pcl)
189 {
190 pnext = pcl->gsqnext;
191 cl_unlock_char(pcl->charname, pcl->realmname, gsid);
192 pcl = pnext;
193 }
194 return 0;
195 }
196
197
198 static int cl_insert_to_gsq_list(unsigned int gsid, t_charlockinfo *pcl)
199 {
200 unsigned int index_pos;
201 t_charlockinfo *ptmp;
202
203 if (!pcl) return -1;
204
205 index_pos = gsid % gsqtbl_len;
206 ptmp = gsqtbl[index_pos];
207 gsqtbl[index_pos] = pcl;
208 if (ptmp) {
209 pcl->gsqnext = ptmp;
210 ptmp->gsqprev = pcl;
211 }
212 return 0;
213 }
214
215
216 static int cl_delete_from_gsq_list(t_charlockinfo *pcl)
217 {
218 unsigned int index_pos;
219 t_charlockinfo *pprev, *pnext;
220
221 if (!pcl) return -1;
222
223 index_pos = (pcl->gsid) % gsqtbl_len;
224 pprev = pcl->gsqprev;
225 pnext = pcl->gsqnext;
226 if (pprev) pprev->gsqnext = pnext;
227 else gsqtbl[index_pos] = pnext;
228 if (pnext) pnext->gsqprev = pprev;
229
230 return 0;
231 }
232
233
234 static unsigned int string_hash(char const *string)
235 {
236 unsigned int i;
237 unsigned int pos;
238 unsigned int hash;
239 unsigned int ch;
240
241 if (!string) return 0;
242
243 for (hash = 0, pos = 0, i = 0; i < std::strlen(string); i++)
244 {
245 ch = safe_tolower(string[i]);
246 hash ^= ROTL(ch, pos, (std::numeric_limits<unsigned int>::digits));
247 pos += (std::numeric_limits<unsigned char>::digits) - 1;
248 }
249
250 return hash;
251 }
252
253 }
254
255 }