File: userlist.c

package info (click to toggle)
fisg 0.3.8-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 640 kB
  • ctags: 284
  • sloc: ansic: 2,020; makefile: 52; sh: 16
file content (218 lines) | stat: -rw-r--r-- 4,289 bytes parent folder | download
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * FISG - Fast IRC Statistic Generator
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2003 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "fisg.h"
#include "th_util.h"

/*
 * Handling of userlists
 */
t_user_entry *user_new(char *userHandle)
{
 t_user_entry *pResult;

 /* Allocate memory for new node */
 pResult = (t_user_entry *) calloc(1, sizeof(t_user_entry));
 if (pResult == NULL) return NULL;

 /* Set fields */
 th_strcpy(&pResult->userHandle, userHandle);

 return pResult;
}


void user_free(t_user_entry *pUser)
{
 assert(pUser);

 if (pUser->userHandle) free(pUser->userHandle);
 if (pUser->picPath) free(pUser->picPath);
 if (pUser->linkURL) free(pUser->linkURL);
 if (pUser->sComment) free(pUser->sComment);

 free(pUser);
}


t_nick_entry *nick_new(char *newNick)
{
 t_nick_entry *pResult;

 /* Allocate memory for new node */
 pResult = (t_nick_entry *) calloc(1, sizeof(t_nick_entry));
 if (pResult == NULL) return NULL;

 /* Set fields */
 strncpy(pResult->nick, newNick, SET_MAX_NICKLEN);
 pResult->nick[SET_MAX_NICKLEN - 1] = 0;
 
 return pResult;
}


void nick_free(t_nick_entry *pNick)
{
 assert(pNick);

 free(pNick);
}



/*
 * Insert given node to linked list
 */
#define LPREV	(pNode->pPrev)
#define LTHIS	(pNode)
#define LNEXT	(pNode->pNext)

void user_insert(t_user_entry **userList, t_user_entry *pNode)
{
 assert(userList);
 
 if (*userList)
	{
	/* The first node's pPrev points to last node */
	LPREV = (*userList)->pPrev;	/* New node's prev = Previous last node */
	(*userList)->pPrev->pNext = pNode;	/* Previous last node's next = New node */
	(*userList)->pPrev = pNode;		/* New last node = New node */
	LNEXT = NULL;				/* But next is NULL! */
 	} else {
	*userList = pNode;			/* First node ... */
	LPREV = pNode;				/* ... it's also last */
	LNEXT = NULL;				/* But next is NULL! */
 	}
}


/*
 * Delete given node from linked list
 */
void user_delete(t_user_entry **userList, t_user_entry *pNode)
{
 assert(userList);
 assert(*userList);
 assert(pNode);

 if (LPREV) LPREV->pNext = LNEXT;
 if (LNEXT)
	LNEXT->pPrev = LPREV;
	else
	(*userList)->pPrev = LPREV;

 LPREV = LNEXT = NULL;
}


/*
 * Insert given node into the nicklist hashtable
 */
int nick_insert(t_nick_entry *nickList[], t_nick_entry *pNode)
{
 int i;
 char *tmpNick;

 assert(nickList);
 
 /* If the first character is patternmatcher, find the first alphanum */
 tmpNick = pNode->nick;
 while ((*tmpNick == '*') || (*tmpNick == '?')) tmpNick++;
 i = th_tolower(*tmpNick);
 
 /* Check the hashcode */
 if ((i < 0) && (i >= SET_HASH_MAXINDEX))
 	return -1;
 
 if (nickList[i])
	{
	/* The first node's pPrev points to last node */
	pNode->pPrev = nickList[i]->pPrev;	/* New node's prev = Previous last node */
	nickList[i]->pPrev->pNext = pNode;	/* Previous last node's next = New node */
	nickList[i]->pPrev = pNode;		/* New last node = New node */
	pNode->pNext = NULL;			/* But next is NULL! */
 	} else {
	nickList[i] = pNode;			/* First node */
	pNode->pPrev = pNode;			/* But also last */
	pNode->pNext = NULL;			/* But next is NULL! */
 	}
 
 return 0; 
}


/*
 * Check if given nick is in the userlist
 */
t_user_entry *user_search(t_nick_entry *nickList[], char *findNick)
{
 t_nick_entry *pCurr;
 int i, j;
 BOOL isFound;

 assert(nickList);

 isFound = FALSE;
 pCurr = NULL;
 j = 0;
 
 /* Check hash */
 while ((!isFound) && (findNick[j]))
 {
 i = th_tolower(findNick[j]);
 if ((i >= 0) && (i < SET_HASH_MAXINDEX))
 	{
 	/* Find from linked list */
	pCurr = nickList[i];
	while (pCurr && (!isFound))
		{
		if (th_strcasematch(findNick, pCurr->nick))
			isFound = TRUE;
			else
			pCurr = pCurr->pNext;
		}
	}
 j++;
 }
 
 /* Return result */
 if (isFound)
	return pCurr->user;
	else
	return NULL;
}


/*
 * Update nicks for given user to another user
 */
void nick_change(t_nick_entry *nickList[], t_user_entry *pFind, t_user_entry *pNew)
{
 t_nick_entry *pCurr;
 int i;

 assert(nickList);
 
 for (i = 0; i < SET_HASH_MAXINDEX; i++)
	{
 	/* Find from linked list */
	pCurr = nickList[i];
	while (pCurr)
		{
		if (pCurr->user == pFind)
			pCurr->user = pNew;

		pCurr = pCurr->pNext;
		}
	}
}