File: list.c

package info (click to toggle)
teknap 1.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,428 kB
  • ctags: 6,297
  • sloc: ansic: 55,591; makefile: 545; sh: 112
file content (264 lines) | stat: -rw-r--r-- 5,779 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * list.c: some generic linked list managing stuff 
 *
 * Written By Michael Sandrof
 *
 * Copyright(c) 1990 
 *
 * See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT 
 * $Id: list.c,v 1.1.1.1 2000/06/23 05:55:53 edwards Exp $ 
 */


#include "teknap.h"
#include "struct.h"

#include "list.h"

#include "ircaux.h"

	int	add_list_stricmp (List *, List *);
static	int	list_stricmp (List *, char *);
static	int	list_match (List *, char *);

/*
 * These have now been made more general. You used to only be able to
 * order these lists by alphabetical order. You can now order them
 * arbitrarily. The functions are still called the same way if you
 * wish to use alphabetical order on the key string, and the old
 * function name now represents a stub function which calls the
 * new with the appropriate parameters.
 *
 * The new function name is the same in each case as the old function
 * name, with the addition of a new parameter, cmp_func, which is
 * used to perform comparisons.
 *
 */

int add_list_strcmp(List *item1, List *item2)
{
	return strcmp(item1->name, item2->name);
}

int add_list_stricmp(List *item1, List *item2)
{
	return my_stricmp(item1->name, item2->name);
}

/*
static	int list_strcmp(List *item1, char *str)
{
	return strcmp(item1->name, str);
}
*/

static	int list_stricmp(List *item1, char *str)
{
	return my_stricmp(item1->name, str);
}

int list_strnicmp(List *item1, char *str)
{
	return my_strnicmp(item1->name, str, strlen(str));
}

static int  list_wildstrcmp(List *item1, char *str)
{
	if (wild_match(item1->name, str) || wild_match(str, item1->name))
		return 0;
	else
		return 1;
}

static	int list_match(List *item1, char *str)
{
	return wild_match(item1->name, str);
}

/*
 * add_to_list: This will add an element to a list.  The requirements for the
 * list are that the first element in each list structure be a pointer to the
 * next element in the list, and the second element in the list structure be
 * a pointer to a character (char *) which represents the sort key.  For
 * example 
 *
 * struct my_list{ struct my_list *next; char *name; <whatever else you want>}; 
 *
 * The parameters are:  "list" which is a pointer to the head of the list. "add"
 * which is a pre-allocated element to be added to the list.  
 */
void add_to_list_ext(List **list, List *add, int dble, int (*cmp_func)(List *, List *))
{
register List	*tmp;
	 List	*last;

	if (!cmp_func)
		cmp_func = add_list_stricmp;
	last = NULL;
	for (tmp = *list; tmp; tmp = tmp->next)
	{
		if (cmp_func(tmp, add) > 0)
			break;
		last = tmp;
	}
	if (last)
	{
		last->next = add;
		if (dble)
		{
			add->prev = last;
			if (tmp)
				tmp->prev = add;
		}
	}
	else
	{
		if (dble && *list)
			(*list)->prev = add;
		*list = add;
	}
	add->next = tmp;
}

void add_to_list(List **list, List *add)
{
	add_to_list_ext(list, add, 0, NULL);
}

void add_to_list_double(List **list, List *add)
{
	add_to_list_ext(list, add, 1, NULL);
}


/*
 * find_in_list: This looks up the given name in the given list.  List and
 * name are as described above.  If wild is true, each name in the list is
 * used as a wild card expression to match name... otherwise, normal matching
 * is done 
 */
List	* find_in_list_ext(register List **list, char *name, int wild, int (*cmp_func)(List *, char *))
{
register List	*tmp;
	int	best_match,
		current_match;

	if (!cmp_func)
		cmp_func = wild ? list_match : list_stricmp;
	best_match = 0;

	if (wild)
	{
		register List	*match = NULL;

		for (tmp = *list; tmp; tmp = tmp->next)
		{
			if ((current_match = cmp_func(tmp, name)) > best_match)
			{
				match = tmp;
				best_match = current_match;
			}
		}
		return (match);
	}
	else
	{
		for (tmp = *list; tmp; tmp = tmp->next)
			if (cmp_func(tmp, name) == 0)
				return (tmp);
	}
	return NULL;
}

List	* find_in_list(List **list, char *name, int wild)
{
	return find_in_list_ext(list, name, wild, NULL);
}

/*
 * remove_from_list: this remove the given name from the given list (again as
 * described above).  If found, it is removed from the list and returned
 * (memory is not deallocated).  If not found, null is returned. 
 */
List	*remove_from_list_ext(List **list, char *name, int (*cmp_func)(List *, char *))
{
register	List	*tmp;
		List 	*last;

	if (!cmp_func)
		cmp_func = list_stricmp;
	last = NULL;
	for (tmp = *list; tmp; tmp = tmp->next)
	{
		if (!cmp_func(tmp, name))
		{
			if (last)
				last->next = tmp->next;
			else
				*list = tmp->next;
			return (tmp);
		}
		last = tmp;
	}
	return NULL;
}

List	*remove_from_list(List **list, char *name)
{
	return remove_from_list_ext(list, name, NULL);
}

List	*remove_from_list_double(List **list, char *name)
{
List *tmp = NULL;
	if ((tmp = remove_from_list_ext(list, name, NULL)))
	{
		if (tmp->prev)
			tmp->prev->next = tmp->next;
		if (tmp->next)
			tmp->next->prev = tmp->prev;
	}
	return tmp;
}

List	*removewild_from_list(List **list, char *name)
{
	return remove_from_list_ext(list, name, list_wildstrcmp);
}


/*
 * list_lookup: this routine just consolidates remove_from_list and
 * find_in_list.  I did this cause it fit better with some already existing
 * code 
 */
List	*list_lookup_ext(register List **list, char *name, int wild, int delete, int (*cmp_func)(List *, char *))
{
register List	*tmp;

	if (delete)
		tmp = remove_from_list_ext(list, name, cmp_func);
	else
		tmp = find_in_list_ext(list, name, wild, cmp_func);
	return (tmp);
}

List	*list_lookup(List **list, char *name, int wild, int delete)
{
	return list_lookup_ext(list, name, wild, delete, NULL);
}

List *break_from_list(List **list, List *tmp)
{
	if (tmp->prev)
		tmp->prev->next = tmp->next;
	else
		*list = tmp->next;
	if (tmp->next)
		tmp->next->prev = tmp->prev;
	if (*list)
		(*list)->prev = NULL;
	tmp->prev = tmp->next = NULL;
	return tmp;
}