forked from pvpgn/d2gs109
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharlist.c
175 lines (147 loc) · 3.23 KB
/
charlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include "charlist.h"
/* variables */
static unsigned int clitbl_len = 0;
static D2CHARLIST **clitbl = NULL;
unsigned int string_hash(char const *string)
{
unsigned int i;
unsigned int pos;
unsigned int hash;
unsigned int ch;
if (!string) return 0;
for (hash=0,pos=0,i=0; i<strlen(string); i++)
{
if (isascii((int)string[i]))
ch = (unsigned int)(unsigned char)tolower((int)string[i]);
else
ch = (unsigned int)(unsigned char)string[i];
hash ^= ROTL(ch,pos,sizeof(unsigned int)*CHAR_BIT);
pos += CHAR_BIT-1;
}
return hash;
}
int charlist_init(unsigned int tbllen)
{
if (!tbllen) return -1;
charlist_destroy();
clitbl = (D2CHARLIST**)malloc(tbllen*sizeof(D2CHARLIST**));
if (!clitbl) return -1;
memset(clitbl, 0, tbllen*sizeof(D2CHARLIST**));
clitbl_len = tbllen;
return 0;
}
int charlist_destroy(void)
{
charlist_flush();
if (clitbl) free(clitbl);
clitbl = NULL;
clitbl_len = 0;
return 0;
}
void charlist_flush(void)
{
unsigned int i;
D2CHARLIST *pcl, *ptmp;
if (clitbl) {
for(i=0; i<clitbl_len; i++)
{
pcl = clitbl[i];
while(pcl)
{
ptmp = pcl;
pcl = pcl->next;
free(ptmp);
}
}
}
return;
}
void *charlist_getdata(unsigned char const *charname, int type)
{
D2CHARLIST *pcl;
unsigned int hashval;
void *ret;
if (!charname) return NULL;
if (!clitbl_len) return NULL;
if (strlen(charname)>=(MAX_CHARNAME_LEN)) return NULL;
ret = NULL;
hashval = string_hash(charname) % clitbl_len;
pcl = clitbl[hashval];
while(pcl)
{
if (strcmpi(pcl->charname, charname) == 0) {
switch(type)
{
case CHARLIST_GET_CHARINFO:
ret = pcl->pCharInfo;
break;
case CHARLIST_GET_GAMEINFO:
ret = pcl->pGameInfo;
break;
default:
ret = NULL;
}
break;
}
pcl = pcl->next;
}
return ret;
}
int charlist_insert(unsigned char *charname, void *pCharInfo, void *pGameInfo)
{
D2CHARLIST *pcl, *ptmp;
unsigned int hashval;
if (!charname) return -1;
if (!clitbl_len) return -1;
if (strlen(charname)>=(MAX_CHARNAME_LEN)) return -2;
hashval = string_hash(charname) % clitbl_len;
pcl = clitbl[hashval];
ptmp = NULL;
while(pcl)
{
if (strcmpi(pcl->charname, charname) == 0)
return -3; /* found the char, error */
ptmp = pcl;
pcl = pcl->next;
}
/* not found, insert one */
pcl = (D2CHARLIST*)malloc(sizeof(D2CHARLIST));
if (!pcl) return -4; /* no free memory available :( */
memset(pcl, 0, sizeof(D2CHARLIST));
strncpy(pcl->charname, charname, MAX_CHARNAME_LEN-1);
pcl->pCharInfo = pCharInfo;
pcl->pGameInfo = pGameInfo;
/* add to hash table link list */
if (ptmp) ptmp->next = pcl;
else clitbl[hashval] = pcl;
return 0;
}
int charlist_delete(unsigned char *charname)
{
D2CHARLIST *pcl, *ptmp;
unsigned int hashval;
if (!charname) return -1;
if (!clitbl_len) return -1;
if (strlen(charname)>=(MAX_CHARNAME_LEN)) return -1;
hashval = string_hash(charname) % clitbl_len;
pcl = clitbl[hashval];
ptmp = NULL;
while(pcl)
{
if (strcmpi(pcl->charname, charname) == 0) {
if (ptmp) ptmp->next = pcl->next;
else clitbl[hashval] = pcl->next;
free(pcl);
return 0;
}
ptmp = pcl;
pcl = pcl->next;
}
return 0;
}