]>
jfr.im git - solanum.git/blob - libratbox/src/tools.c
2 * ircd-ratbox: A slightly useful ircd.
3 * tools.c: Various functions needed here and there.
5 * Copyright (C) 1996-2002 Hybrid Development Team
6 * Copyright (C) 2002-2005 ircd-ratbox development team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23 * $Id: tools.c 26170 2008-10-26 20:59:07Z androsyn $
25 * Here is the original header:
27 * Useful stuff, ripped from places ..
28 * adrian chadd <adrian@creative.net.au>
30 * The TOOLS_C define builds versions of the functions in tools.h
31 * so that they end up in the resulting object files. If its not
32 * defined, tools.h will build inlined versions of the functions
33 * on supported compilers
36 #include <libratbox_config.h>
37 #include <ratbox_lib.h>
45 static rb_bh
*dnode_heap
;
47 rb_init_rb_dlink_nodes(size_t dh_size
)
50 dnode_heap
= rb_bh_create(sizeof(rb_dlink_node
), dh_size
, "librb_dnode_heap");
51 if(dnode_heap
== NULL
)
59 * output - pointer to new rb_dlink_node
63 rb_make_rb_dlink_node(void)
65 return (rb_bh_alloc(dnode_heap
));
71 * inputs - pointer to rb_dlink_node
73 * side effects - free given rb_dlink_node
76 rb_free_rb_dlink_node(rb_dlink_node
*ptr
)
79 rb_bh_free(dnode_heap
, ptr
);
82 /* rb_string_to_array()
83 * Changes a given buffer into an array of parameters.
84 * Taken from ircd-ratbox.
86 * inputs - string to parse, array to put in
87 * outputs - number of parameters
90 rb_string_to_array(char *string
, char **parv
, int maxpara
)
92 char *p
, *xbuf
= string
;
97 if(string
== NULL
|| string
[0] == '\0')
100 while(*xbuf
== ' ') /* skip leading spaces */
102 if(*xbuf
== '\0') /* ignore all-space args */
107 if(*xbuf
== ':') /* Last parameter */
118 if((p
= strchr(xbuf
, ' ')) != NULL
)
131 while(x
< maxpara
- 1);
143 rb_strlcat(char *dest
, const char *src
, size_t count
)
145 size_t dsize
= strlen(dest
);
146 size_t len
= strlen(src
);
147 size_t res
= dsize
+ len
;
153 memcpy(dest
, src
, len
);
159 rb_strlcat(char *dest
, const char *src
, size_t count
)
161 return strlcat(dest
, src
, count
);
167 rb_strlcpy(char *dest
, const char *src
, size_t size
)
169 size_t ret
= strlen(src
);
173 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
174 memcpy(dest
, src
, len
);
181 rb_strlcpy(char *dest
, const char *src
, size_t size
)
183 return strlcpy(dest
, src
, size
);
190 rb_strnlen(const char *s
, size_t count
)
193 for(sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
199 rb_strnlen(const char *s
, size_t count
)
201 return strnlen(s
, count
);
206 * rb_snprintf_append()
207 * appends snprintf formatted string to the end of the buffer but not
211 rb_snprintf_append(char *str
, size_t len
, const char *format
, ...)
227 va_start(ap
, format
);
228 x
= (vsnprintf(str
+ x
, len
- x
, format
, ap
) + (int)x
);
241 rb_basename(const char *path
)
245 if(!(s
= strrchr(path
, '/')))
257 rb_dirname (const char *path
)
261 s
= strrchr(path
, '/');
264 return rb_strdup(".");
267 /* remove extra slashes */
268 while(s
> path
&& *s
== '/')
271 return rb_strndup(path
, ((uintptr_t)s
- (uintptr_t)path
) + 2);
274 size_t rb_zstring_serialized(rb_zstring_t
*zs
, void **buf
, size_t *buflen
)
277 size_t alloclen
= sizeof(uint16_t) + zs
->len
;
279 p
= rb_malloc(sizeof(alloclen
));
280 memcpy(p
, &zs
->len
, sizeof(uint16_t));
281 p
+= sizeof(uint16_t);
282 memcpy(p
, zs
->data
, zs
->len
);
286 size_t rb_zstring_deserialize(rb_zstring_t
*zs
, void *buf
)
288 uint8_t *p
= (uint8_t *)buf
;
290 memcpy(&zs
->len
, p
, sizeof(uint16_t));
291 p
+= sizeof(uint16_t);
295 return sizeof(uint16_t);
297 zs
->data
= rb_malloc(zs
->len
);
298 memcpy(zs
->data
, p
, zs
->len
);
299 return zs
->len
+ sizeof(uint16_t);
302 void rb_zstring_free(rb_zstring_t
*zs
)
309 rb_zstring_t
*rb_zstring_alloc(void)
311 rb_zstring_t
*zs
= rb_malloc(sizeof(rb_zstring_t
));
315 rb_zstring_t
*rb_zstring_from_c_len(const char *buf
, size_t len
)
319 if(len
> UINT16_MAX
-1)
322 zs
= rb_zstring_alloc();
323 zs
->alloclen
= zs
->len
= (uint16_t)len
;
324 zs
->alloclen
= (uint16_t)len
;
325 if(zs
->alloclen
< 128)
327 zs
->data
= rb_malloc(zs
->alloclen
);
328 memcpy(zs
->data
, buf
, zs
->len
);
332 rb_zstring_t
*rb_zstring_from_c(const char *buf
)
334 return rb_zstring_from_c_len(buf
, strlen(buf
));
337 size_t rb_zstring_len(rb_zstring_t
*zs
)
342 void rb_zstring_append_from_zstring(rb_zstring_t
*dst_zs
, rb_zstring_t
*src_zs
)
345 size_t nlen
= dst_zs
->len
+ src_zs
->len
;
347 if(nlen
> dst_zs
->alloclen
)
349 dst_zs
->alloclen
+= src_zs
->len
+ 64;
350 dst_zs
->data
= rb_realloc(dst_zs
->data
, dst_zs
->alloclen
);
353 ep
= dst_zs
->data
+ dst_zs
->len
;
354 memcpy(ep
, src_zs
->data
, src_zs
->len
);
357 void rb_zstring_append_from_c(rb_zstring_t
*zs
, const char *buf
, size_t len
)
360 size_t nlen
= zs
->len
+ len
;
362 if(nlen
> zs
->alloclen
)
364 zs
->alloclen
+= len
+ 64;
365 zs
->data
= rb_realloc(zs
->data
, zs
->alloclen
);
367 ep
= zs
->data
+ zs
->len
;
369 memcpy(ep
, buf
, len
);
372 char *rb_zstring_to_c(rb_zstring_t
*zs
, char *buf
, size_t len
)
380 memcpy(buf
, zs
->data
, cpylen
);
385 char *rb_zstring_to_c_alloc(rb_zstring_t
*zs
)
388 p
= rb_malloc(zs
->len
+1);
389 memcpy(p
, zs
->data
, zs
->len
);
393 size_t rb_zstring_to_ptr(rb_zstring_t
*zs
, void **ptr
)
395 *ptr
= (void *)zs
->data
;