2 * ircd-ratbox: A slightly useful ircd.
3 * tools.h: Header for the various tool functions.
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
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.
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.
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
24 * $Id: rb_tools.h 26170 2008-10-26 20:59:07Z androsyn $
28 # error "Do not use tools.h directly"
34 size_t rb_strlcpy(char *dst
, const char *src
, size_t siz
);
35 size_t rb_strlcat(char *dst
, const char *src
, size_t siz
);
36 size_t rb_strnlen(const char *s
, size_t count
);
37 char *rb_basename(const char *);
38 char *rb_dirname(const char *);
40 int rb_string_to_array(char *string
, char **parv
, int maxpara
);
43 * double-linked-list stuff
45 typedef struct _rb_dlink_node rb_dlink_node
;
46 typedef struct _rb_dlink_list rb_dlink_list
;
63 rb_dlink_node
*rb_make_rb_dlink_node(void);
64 void rb_free_rb_dlink_node(rb_dlink_node
*lp
);
65 void rb_init_rb_dlink_nodes(size_t dh_size
);
67 /* This macros are basically swiped from the linux kernel
68 * they are simple yet effective
72 * Walks forward of a list.
74 * head is your list head
76 #define RB_DLINK_FOREACH(pos, head) for (pos = (head); pos != NULL; pos = pos->next)
79 * Walks forward of a list safely while removing nodes
81 * n is another list head for temporary storage
82 * head is your list head
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)
86 #define RB_DLINK_FOREACH_PREV(pos, head) for (pos = (head); pos != NULL; pos = pos->prev)
89 /* Returns the list length */
90 #define rb_dlink_list_length(list) (list)->length
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)
98 * dlink_ routines are stolen from squid, except for rb_dlinkAddBefore,
104 rb_dlinkMoveNode(rb_dlink_node
*m
, rb_dlink_list
*oldlist
, rb_dlink_list
*newlist
)
106 /* Assumption: If m->next == NULL, then list->tail == m
107 * and: If m->prev == NULL, then list->head == m
110 assert(oldlist
!= NULL
);
111 assert(newlist
!= NULL
);
114 m
->next
->prev
= m
->prev
;
116 oldlist
->tail
= m
->prev
;
119 m
->prev
->next
= m
->next
;
121 oldlist
->head
= m
->next
;
124 m
->next
= newlist
->head
;
125 if(newlist
->head
!= NULL
)
126 newlist
->head
->prev
= m
;
127 else if(newlist
->tail
== NULL
)
136 rb_dlinkAdd(void *data
, rb_dlink_node
*m
, rb_dlink_list
*list
)
138 assert(data
!= NULL
);
140 assert(list
!= NULL
);
144 m
->next
= list
->head
;
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
)
157 rb_dlinkAddBefore(rb_dlink_node
*b
, void *data
, rb_dlink_node
*m
, rb_dlink_list
*list
)
160 assert(data
!= NULL
);
162 assert(list
!= NULL
);
164 /* Shortcut - if its the first one, call rb_dlinkAdd only */
167 rb_dlinkAdd(data
, m
, list
);
181 rb_dlinkMoveTail(rb_dlink_node
*m
, rb_dlink_list
*list
)
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
190 m
->next
->prev
= m
->prev
;
193 m
->prev
->next
= m
->next
;
195 list
->head
= m
->next
;
197 list
->tail
->next
= m
;
198 m
->prev
= list
->tail
;
204 rb_dlinkAddTail(void *data
, rb_dlink_node
*m
, rb_dlink_list
*list
)
207 assert(list
!= NULL
);
208 assert(data
!= NULL
);
212 m
->prev
= list
->tail
;
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
)
224 /* Execution profiles show that this function is called the most
225 * often of all non-spontaneous functions. So it had better be
228 rb_dlinkDelete(rb_dlink_node
*m
, rb_dlink_list
*list
)
231 assert(list
!= NULL
);
232 /* Assumption: If m->next == NULL, then list->tail == m
233 * and: If m->prev == NULL, then list->head == m
236 m
->next
->prev
= m
->prev
;
238 list
->tail
= m
->prev
;
241 m
->prev
->next
= m
->next
;
243 list
->head
= m
->next
;
245 m
->next
= m
->prev
= NULL
;
249 static inline rb_dlink_node
*
250 rb_dlinkFindDelete(void *data
, rb_dlink_list
*list
)
253 assert(list
!= NULL
);
254 assert(data
!= NULL
);
255 RB_DLINK_FOREACH(m
, list
->head
)
261 m
->next
->prev
= m
->prev
;
263 list
->tail
= m
->prev
;
266 m
->prev
->next
= m
->next
;
268 list
->head
= m
->next
;
270 m
->next
= m
->prev
= NULL
;
278 rb_dlinkFindDestroy(void *data
, rb_dlink_list
*list
)
282 assert(list
!= NULL
);
283 assert(data
!= NULL
);
284 ptr
= rb_dlinkFindDelete(data
, list
);
288 rb_free_rb_dlink_node(ptr
);
296 * inputs - list to search
298 * output - pointer to link or NULL if not found
299 * side effects - Look for ptr in the linked listed pointed to by link.
301 static inline rb_dlink_node
*
302 rb_dlinkFind(void *data
, rb_dlink_list
*list
)
305 assert(list
!= NULL
);
306 assert(data
!= NULL
);
308 RB_DLINK_FOREACH(ptr
, list
->head
)
310 if(ptr
->data
== data
)
317 rb_dlinkMoveList(rb_dlink_list
*from
, rb_dlink_list
*to
)
319 assert(from
!= NULL
);
322 /* There are three cases */
323 /* case one, nothing in from list */
324 if(from
->head
== NULL
)
327 /* case two, nothing in to list */
330 to
->head
= from
->head
;
331 to
->tail
= from
->tail
;
332 from
->head
= from
->tail
= NULL
;
333 to
->length
= from
->length
;
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
;
347 #endif /* __TOOLS_H__ */