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
27 # error "Do not use tools.h directly"
33 size_t rb_strlcpy(char *dst
, const char *src
, size_t siz
);
34 size_t rb_strlcat(char *dst
, const char *src
, size_t siz
);
35 size_t rb_strnlen(const char *s
, size_t count
);
36 char *rb_basename(const char *);
37 char *rb_dirname(const char *);
39 int rb_string_to_array(char *string
, char **parv
, int maxpara
);
42 * double-linked-list stuff
44 typedef struct _rb_dlink_node rb_dlink_node
;
45 typedef struct _rb_dlink_list rb_dlink_list
;
62 rb_dlink_node
*rb_make_rb_dlink_node(void);
63 void rb_free_rb_dlink_node(rb_dlink_node
*lp
);
64 void rb_init_rb_dlink_nodes(size_t dh_size
);
66 /* This macros are basically swiped from the linux kernel
67 * they are simple yet effective
71 * Walks forward of a list.
73 * head is your list head
75 #define RB_DLINK_FOREACH(pos, head) for (pos = (head); pos != NULL; pos = pos->next)
78 * Walks forward of a list safely while removing nodes
80 * n is another list head for temporary storage
81 * head is your list head
83 #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 #define RB_DLINK_FOREACH_PREV(pos, head) for (pos = (head); pos != NULL; pos = pos->prev)
88 /* Returns the list length */
89 #define rb_dlink_list_length(list) (list)->length
91 #define rb_dlinkAddAlloc(data, list) rb_dlinkAdd(data, rb_make_rb_dlink_node(), list)
92 #define rb_dlinkAddTailAlloc(data, list) rb_dlinkAddTail(data, rb_make_rb_dlink_node(), list)
93 #define rb_dlinkDestroy(node, list) do { rb_dlinkDelete(node, list); rb_free_rb_dlink_node(node); } while(0)
97 * dlink_ routines are stolen from squid, except for rb_dlinkAddBefore,
103 rb_dlinkMoveNode(rb_dlink_node
*m
, rb_dlink_list
*oldlist
, rb_dlink_list
*newlist
)
105 /* Assumption: If m->next == NULL, then list->tail == m
106 * and: If m->prev == NULL, then list->head == m
109 assert(oldlist
!= NULL
);
110 assert(newlist
!= NULL
);
113 m
->next
->prev
= m
->prev
;
115 oldlist
->tail
= m
->prev
;
118 m
->prev
->next
= m
->next
;
120 oldlist
->head
= m
->next
;
123 m
->next
= newlist
->head
;
124 if(newlist
->head
!= NULL
)
125 newlist
->head
->prev
= m
;
126 else if(newlist
->tail
== NULL
)
135 rb_dlinkAdd(void *data
, rb_dlink_node
*m
, rb_dlink_list
*list
)
137 assert(data
!= NULL
);
139 assert(list
!= NULL
);
143 m
->next
= list
->head
;
145 /* Assumption: If list->tail != NULL, list->head != NULL */
146 if(list
->head
!= NULL
)
147 list
->head
->prev
= m
;
148 else if(list
->tail
== NULL
)
156 rb_dlinkAddBefore(rb_dlink_node
*b
, void *data
, rb_dlink_node
*m
, rb_dlink_list
*list
)
159 assert(data
!= NULL
);
161 assert(list
!= NULL
);
163 /* Shortcut - if its the first one, call rb_dlinkAdd only */
166 rb_dlinkAdd(data
, m
, list
);
180 rb_dlinkMoveTail(rb_dlink_node
*m
, rb_dlink_list
*list
)
185 /* From here assume that m->next != NULL as that can only
186 * be at the tail and assume that the node is on the list
189 m
->next
->prev
= m
->prev
;
192 m
->prev
->next
= m
->next
;
194 list
->head
= m
->next
;
196 list
->tail
->next
= m
;
197 m
->prev
= list
->tail
;
203 rb_dlinkAddTail(void *data
, rb_dlink_node
*m
, rb_dlink_list
*list
)
206 assert(list
!= NULL
);
207 assert(data
!= NULL
);
211 m
->prev
= list
->tail
;
213 /* Assumption: If list->tail != NULL, list->head != NULL */
214 if(list
->tail
!= NULL
)
215 list
->tail
->next
= m
;
216 else if(list
->head
== NULL
)
223 /* Execution profiles show that this function is called the most
224 * often of all non-spontaneous functions. So it had better be
227 rb_dlinkDelete(rb_dlink_node
*m
, rb_dlink_list
*list
)
230 assert(list
!= NULL
);
231 /* Assumption: If m->next == NULL, then list->tail == m
232 * and: If m->prev == NULL, then list->head == m
235 m
->next
->prev
= m
->prev
;
237 list
->tail
= m
->prev
;
240 m
->prev
->next
= m
->next
;
242 list
->head
= m
->next
;
244 m
->next
= m
->prev
= NULL
;
248 static inline rb_dlink_node
*
249 rb_dlinkFindDelete(void *data
, rb_dlink_list
*list
)
252 assert(list
!= NULL
);
253 assert(data
!= NULL
);
254 RB_DLINK_FOREACH(m
, list
->head
)
260 m
->next
->prev
= m
->prev
;
262 list
->tail
= m
->prev
;
265 m
->prev
->next
= m
->next
;
267 list
->head
= m
->next
;
269 m
->next
= m
->prev
= NULL
;
277 rb_dlinkFindDestroy(void *data
, rb_dlink_list
*list
)
281 assert(list
!= NULL
);
282 assert(data
!= NULL
);
283 ptr
= rb_dlinkFindDelete(data
, list
);
287 rb_free_rb_dlink_node(ptr
);
295 * inputs - list to search
297 * output - pointer to link or NULL if not found
298 * side effects - Look for ptr in the linked listed pointed to by link.
300 static inline rb_dlink_node
*
301 rb_dlinkFind(void *data
, rb_dlink_list
*list
)
304 assert(list
!= NULL
);
305 assert(data
!= NULL
);
307 RB_DLINK_FOREACH(ptr
, list
->head
)
309 if(ptr
->data
== data
)
316 rb_dlinkMoveList(rb_dlink_list
*from
, rb_dlink_list
*to
)
318 assert(from
!= NULL
);
321 /* There are three cases */
322 /* case one, nothing in from list */
323 if(from
->head
== NULL
)
326 /* case two, nothing in to list */
329 to
->head
= from
->head
;
330 to
->tail
= from
->tail
;
331 from
->head
= from
->tail
= NULL
;
332 to
->length
= from
->length
;
337 /* third case play with the links */
338 from
->tail
->next
= to
->head
;
339 to
->head
->prev
= from
->tail
;
340 to
->head
= from
->head
;
341 from
->head
= from
->tail
= NULL
;
342 to
->length
+= from
->length
;
346 #endif /* __TOOLS_H__ */