]> jfr.im git - irc/rqf/shadowircd.git/blame - libratbox/include/rb_tools.h
Copied libratbox and related stuff from shadowircd upstream.
[irc/rqf/shadowircd.git] / libratbox / include / rb_tools.h
CommitLineData
b57f37fb
WP
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * tools.h: Header for the various tool functions.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
22 * USA
23 *
94b4fbf9 24 * $Id: rb_tools.h 26170 2008-10-26 20:59:07Z androsyn $
b57f37fb
WP
25 */
26
27#ifndef RB_LIB_H
94b4fbf9 28# error "Do not use tools.h directly"
b57f37fb
WP
29#endif
30
31#ifndef __TOOLS_H__
32#define __TOOLS_H__
33
34size_t rb_strlcpy(char *dst, const char *src, size_t siz);
35size_t rb_strlcat(char *dst, const char *src, size_t siz);
36size_t rb_strnlen(const char *s, size_t count);
94b4fbf9
VY
37char *rb_basename(const char *);
38char *rb_dirname(const char *);
b57f37fb
WP
39
40int rb_string_to_array(char *string, char **parv, int maxpara);
41
42/*
43 * double-linked-list stuff
44 */
45typedef struct _rb_dlink_node rb_dlink_node;
46typedef struct _rb_dlink_list rb_dlink_list;
47
48struct _rb_dlink_node
49{
50 void *data;
51 rb_dlink_node *prev;
52 rb_dlink_node *next;
53
54};
55
56struct _rb_dlink_list
57{
58 rb_dlink_node *head;
59 rb_dlink_node *tail;
60 unsigned long length;
61};
62
63rb_dlink_node *rb_make_rb_dlink_node(void);
94b4fbf9 64void rb_free_rb_dlink_node(rb_dlink_node *lp);
b57f37fb
WP
65void rb_init_rb_dlink_nodes(size_t dh_size);
66
67/* This macros are basically swiped from the linux kernel
68 * they are simple yet effective
69 */
70
71/*
72 * Walks forward of a list.
73 * pos is your node
74 * head is your list head
75 */
76#define RB_DLINK_FOREACH(pos, head) for (pos = (head); pos != NULL; pos = pos->next)
77
78/*
79 * Walks forward of a list safely while removing nodes
80 * pos is your node
81 * n is another list head for temporary storage
82 * head is your list head
83 */
84#define RB_DLINK_FOREACH_SAFE(pos, n, head) for (pos = (head), n = pos ? pos->next : NULL; pos != NULL; pos = n, n = pos ? pos->next : NULL)
85
86#define RB_DLINK_FOREACH_PREV(pos, head) for (pos = (head); pos != NULL; pos = pos->prev)
87
88
89/* Returns the list length */
90#define rb_dlink_list_length(list) (list)->length
91
92#define rb_dlinkAddAlloc(data, list) rb_dlinkAdd(data, rb_make_rb_dlink_node(), list)
93#define rb_dlinkAddTailAlloc(data, list) rb_dlinkAddTail(data, rb_make_rb_dlink_node(), list)
94#define rb_dlinkDestroy(node, list) do { rb_dlinkDelete(node, list); rb_free_rb_dlink_node(node); } while(0)
95
96
97/*
98 * dlink_ routines are stolen from squid, except for rb_dlinkAddBefore,
99 * which is mine.
100 * -- adrian
101 */
102
103static inline void
94b4fbf9 104rb_dlinkMoveNode(rb_dlink_node *m, rb_dlink_list *oldlist, rb_dlink_list *newlist)
b57f37fb
WP
105{
106 /* Assumption: If m->next == NULL, then list->tail == m
107 * and: If m->prev == NULL, then list->head == m
108 */
109 assert(m != NULL);
110 assert(oldlist != NULL);
111 assert(newlist != NULL);
112
113 if(m->next)
114 m->next->prev = m->prev;
115 else
116 oldlist->tail = m->prev;
117
118 if(m->prev)
119 m->prev->next = m->next;
120 else
121 oldlist->head = m->next;
122
123 m->prev = NULL;
124 m->next = newlist->head;
125 if(newlist->head != NULL)
126 newlist->head->prev = m;
127 else if(newlist->tail == NULL)
128 newlist->tail = m;
129 newlist->head = m;
130
131 oldlist->length--;
132 newlist->length++;
133}
134
135static inline void
94b4fbf9 136rb_dlinkAdd(void *data, rb_dlink_node *m, rb_dlink_list *list)
b57f37fb
WP
137{
138 assert(data != NULL);
139 assert(m != NULL);
140 assert(list != NULL);
141
142 m->data = data;
143 m->prev = NULL;
144 m->next = list->head;
145
146 /* Assumption: If list->tail != NULL, list->head != NULL */
147 if(list->head != NULL)
148 list->head->prev = m;
149 else if(list->tail == NULL)
150 list->tail = m;
151
152 list->head = m;
153 list->length++;
154}
155
156static inline void
94b4fbf9 157rb_dlinkAddBefore(rb_dlink_node *b, void *data, rb_dlink_node *m, rb_dlink_list *list)
b57f37fb
WP
158{
159 assert(b != NULL);
160 assert(data != NULL);
161 assert(m != NULL);
162 assert(list != NULL);
163
164 /* Shortcut - if its the first one, call rb_dlinkAdd only */
165 if(b == list->head)
166 {
167 rb_dlinkAdd(data, m, list);
168 }
169 else
170 {
171 m->data = data;
172 b->prev->next = m;
173 m->prev = b->prev;
174 b->prev = m;
175 m->next = b;
176 list->length++;
177 }
178}
179
180static inline void
181rb_dlinkMoveTail(rb_dlink_node *m, rb_dlink_list *list)
182{
183 if(list->tail == m)
184 return;
94b4fbf9 185
b57f37fb
WP
186 /* From here assume that m->next != NULL as that can only
187 * be at the tail and assume that the node is on the list
188 */
94b4fbf9 189
b57f37fb
WP
190 m->next->prev = m->prev;
191
192 if(m->prev != NULL)
193 m->prev->next = m->next;
194 else
195 list->head = m->next;
196
197 list->tail->next = m;
198 m->prev = list->tail;
199 m->next = NULL;
200 list->tail = m;
201}
202
203static inline void
94b4fbf9 204rb_dlinkAddTail(void *data, rb_dlink_node *m, rb_dlink_list *list)
b57f37fb
WP
205{
206 assert(m != NULL);
207 assert(list != NULL);
208 assert(data != NULL);
209
210 m->data = data;
211 m->next = NULL;
212 m->prev = list->tail;
213
214 /* Assumption: If list->tail != NULL, list->head != NULL */
215 if(list->tail != NULL)
216 list->tail->next = m;
217 else if(list->head == NULL)
218 list->head = m;
219
220 list->tail = m;
221 list->length++;
222}
223
224/* Execution profiles show that this function is called the most
225 * often of all non-spontaneous functions. So it had better be
226 * efficient. */
227static inline void
94b4fbf9 228rb_dlinkDelete(rb_dlink_node *m, rb_dlink_list *list)
b57f37fb
WP
229{
230 assert(m != NULL);
231 assert(list != NULL);
232 /* Assumption: If m->next == NULL, then list->tail == m
233 * and: If m->prev == NULL, then list->head == m
234 */
235 if(m->next)
236 m->next->prev = m->prev;
237 else
238 list->tail = m->prev;
239
240 if(m->prev)
241 m->prev->next = m->next;
242 else
243 list->head = m->next;
244
245 m->next = m->prev = NULL;
246 list->length--;
247}
248
249static inline rb_dlink_node *
250rb_dlinkFindDelete(void *data, rb_dlink_list *list)
251{
252 rb_dlink_node *m;
253 assert(list != NULL);
254 assert(data != NULL);
255 RB_DLINK_FOREACH(m, list->head)
256 {
257 if(m->data != data)
258 continue;
259
260 if(m->next)
261 m->next->prev = m->prev;
262 else
263 list->tail = m->prev;
264
265 if(m->prev)
266 m->prev->next = m->next;
267 else
268 list->head = m->next;
269
270 m->next = m->prev = NULL;
271 list->length--;
272 return m;
273 }
274 return NULL;
275}
276
277static inline int
278rb_dlinkFindDestroy(void *data, rb_dlink_list *list)
279{
280 void *ptr;
281
282 assert(list != NULL);
283 assert(data != NULL);
284 ptr = rb_dlinkFindDelete(data, list);
285
286 if(ptr != NULL)
287 {
288 rb_free_rb_dlink_node(ptr);
289 return 1;
290 }
291 return 0;
292}
293
294/*
295 * rb_dlinkFind
296 * inputs - list to search
297 * - data
298 * output - pointer to link or NULL if not found
299 * side effects - Look for ptr in the linked listed pointed to by link.
300 */
301static inline rb_dlink_node *
302rb_dlinkFind(void *data, rb_dlink_list *list)
303{
304 rb_dlink_node *ptr;
305 assert(list != NULL);
306 assert(data != NULL);
94b4fbf9 307
b57f37fb
WP
308 RB_DLINK_FOREACH(ptr, list->head)
309 {
310 if(ptr->data == data)
311 return (ptr);
312 }
313 return (NULL);
314}
315
316static inline void
94b4fbf9 317rb_dlinkMoveList(rb_dlink_list *from, rb_dlink_list *to)
b57f37fb
WP
318{
319 assert(from != NULL);
320 assert(to != NULL);
321
322 /* There are three cases */
323 /* case one, nothing in from list */
324 if(from->head == NULL)
325 return;
326
327 /* case two, nothing in to list */
328 if(to->head == NULL)
329 {
330 to->head = from->head;
331 to->tail = from->tail;
332 from->head = from->tail = NULL;
333 to->length = from->length;
334 from->length = 0;
335 return;
336 }
337
338 /* third case play with the links */
339 from->tail->next = to->head;
340 to->head->prev = from->tail;
341 to->head = from->head;
342 from->head = from->tail = NULL;
343 to->length += from->length;
344 from->length = 0;
345}
346
347#endif /* __TOOLS_H__ */