]> jfr.im git - solanum.git/blob - librb/src/tools.c
librb: remove unnecessary NULL from the end of rb_string_to_array output
[solanum.git] / librb / src / tools.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * tools.c: Various functions needed here and there.
4 *
5 * Copyright (C) 1996-2002 Hybrid Development Team
6 * Copyright (C) 2002-2005 ircd-ratbox development team
7 *
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.
12 *
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.
17 *
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
21 * USA
22 *
23 *
24 * Here is the original header:
25 *
26 * Useful stuff, ripped from places ..
27 * adrian chadd <adrian@creative.net.au>
28 *
29 * The TOOLS_C define builds versions of the functions in tools.h
30 * so that they end up in the resulting object files. If its not
31 * defined, tools.h will build inlined versions of the functions
32 * on supported compilers
33 */
34
35 #define _GNU_SOURCE 1
36 #include <librb_config.h>
37 #include <rb_lib.h>
38 #include <rb_tools.h>
39
40
41 /*
42 * init_rb_dlink_nodes
43 *
44 */
45 static rb_bh *dnode_heap;
46 void
47 rb_init_rb_dlink_nodes(size_t dh_size)
48 {
49
50 dnode_heap = rb_bh_create(sizeof(rb_dlink_node), dh_size, "librb_dnode_heap");
51 if(dnode_heap == NULL)
52 rb_outofmemory();
53 }
54
55 /*
56 * make_rb_dlink_node
57 *
58 * inputs - NONE
59 * output - pointer to new rb_dlink_node
60 * side effects - NONE
61 */
62 rb_dlink_node *
63 rb_make_rb_dlink_node(void)
64 {
65 return (rb_bh_alloc(dnode_heap));
66 }
67
68 /*
69 * free_rb_dlink_node
70 *
71 * inputs - pointer to rb_dlink_node
72 * output - NONE
73 * side effects - free given rb_dlink_node
74 */
75 void
76 rb_free_rb_dlink_node(rb_dlink_node *ptr)
77 {
78 assert(ptr != NULL);
79 rb_bh_free(dnode_heap, ptr);
80 }
81
82 /* rb_string_to_array()
83 * Changes a given buffer into an array of parameters.
84 * Taken from ircd-ratbox.
85 *
86 * inputs - string to parse, array to put in (size >= maxpara)
87 * outputs - number of parameters
88 */
89 int
90 rb_string_to_array(char *string, char **parv, int maxpara)
91 {
92 char *p, *xbuf = string;
93 int x = 0;
94
95 if(string == NULL || string[0] == '\0')
96 return x;
97
98 while(*xbuf == ' ') /* skip leading spaces */
99 xbuf++;
100 if(*xbuf == '\0') /* ignore all-space args */
101 return x;
102
103 do
104 {
105 if(*xbuf == ':') /* Last parameter */
106 {
107 xbuf++;
108 parv[x++] = xbuf;
109 return x;
110 }
111 else
112 {
113 parv[x++] = xbuf;
114 if((p = strchr(xbuf, ' ')) != NULL)
115 {
116 *p++ = '\0';
117 xbuf = p;
118 }
119 else
120 return x;
121 }
122 while(*xbuf == ' ')
123 xbuf++;
124 if(*xbuf == '\0')
125 return x;
126 }
127 while(x < maxpara - 1);
128
129 if(*p == ':')
130 p++;
131
132 parv[x++] = p;
133 return x;
134 }
135
136 #ifndef HAVE_STRCASECMP
137 #ifndef _WIN32
138 /* Fallback taken from FreeBSD. --Elizafox */
139 int
140 rb_strcasecmp(const char *s1, const char *s2)
141 {
142 const unsigned char *us1 = (const unsigned char *)s1;
143 const unsigned char *us2 = (const unsigned char *)s2;
144
145 while (tolower(*us1) == tolower(*us2++))
146 {
147 if (*us1++ == '\0')
148 return 0;
149 }
150
151 return (tolower(*us1) - tolower(*--us2));
152 }
153 #else /* _WIN32 */
154 int
155 rb_strcasecmp(const char *s1, const char *s2)
156 {
157 return stricmp(s1, s2);
158 }
159 #endif /* _WIN32 */
160 #else /* HAVE_STRCASECMP */
161 int
162 rb_strcasecmp(const char *s1, const char *s2)
163 {
164 return strcasecmp(s1, s2);
165 }
166 #endif
167
168 #ifndef HAVE_STRNCASECMP
169 #ifndef _WIN32
170 /* Fallback taken from FreeBSD. --Elizafox */
171 int
172 rb_strncasecmp(const char *s1, const char *s2, size_t n)
173 {
174 if (n != 0)
175 {
176 const unsigned char *us1 = (const unsigned char *)s1;
177 const unsigned char *us2 = (const unsigned char *)s2;
178
179 do
180 {
181 if (tolower(*us1) != tolower(*us2++))
182 return (tolower(*us1) - tolower(*--us2));
183 if (*us1++ == '\0')
184 break;
185 } while (--n != 0);
186 }
187 return 0;
188 }
189 #else /* _WIN32 */
190 int
191 rb_strncasecmp(const char *s1, const char *s2, size_t n)
192 {
193 return strnicmp(s1, s2, n);
194 }
195 #endif /* _WIN32 */
196 #else /* HAVE_STRNCASECMP */
197 int
198 rb_strncasecmp(const char *s1, const char *s2, size_t n)
199 {
200 return strncasecmp(s1, s2, n);
201 }
202 #endif
203
204 #ifndef HAVE_STRCASESTR
205 /* Fallback taken from FreeBSD. --Elizafox */
206 char *
207 rb_strcasestr(const char *s, const char *find)
208 {
209 char c, sc;
210 size_t len;
211
212 if ((c = *find++) != 0) {
213 c = tolower((unsigned char)c);
214 len = strlen(find);
215 do {
216 do {
217 if ((sc = *s++) == 0)
218 return (NULL);
219 } while ((char)tolower((unsigned char)sc) != c);
220 } while (rb_strncasecmp(s, find, len) != 0);
221 s--;
222 }
223 return ((char *)s);
224 }
225 #else
226 char *
227 rb_strcasestr(const char *s, const char *find)
228 {
229 return strcasestr(s, find);
230 }
231 #endif
232
233 #ifndef HAVE_STRLCAT
234 size_t
235 rb_strlcat(char *dest, const char *src, size_t count)
236 {
237 size_t dsize = strlen(dest);
238 size_t len = strlen(src);
239 size_t res = dsize + len;
240
241 dest += dsize;
242 count -= dsize;
243 if(len >= count)
244 len = count - 1;
245 memcpy(dest, src, len);
246 dest[len] = 0;
247 return res;
248 }
249 #else
250 size_t
251 rb_strlcat(char *dest, const char *src, size_t count)
252 {
253 return strlcat(dest, src, count);
254 }
255 #endif
256
257 #ifndef HAVE_STRLCPY
258 size_t
259 rb_strlcpy(char *dest, const char *src, size_t size)
260 {
261 size_t ret = strlen(src);
262
263 if(size)
264 {
265 size_t len = (ret >= size) ? size - 1 : ret;
266 memcpy(dest, src, len);
267 dest[len] = '\0';
268 }
269 return ret;
270 }
271 #else
272 size_t
273 rb_strlcpy(char *dest, const char *src, size_t size)
274 {
275 return strlcpy(dest, src, size);
276 }
277 #endif
278
279
280 #ifndef HAVE_STRNLEN
281 size_t
282 rb_strnlen(const char *s, size_t count)
283 {
284 const char *sc;
285 for(sc = s; count-- && *sc != '\0'; ++sc)
286 ;
287 return sc - s;
288 }
289 #else
290 size_t
291 rb_strnlen(const char *s, size_t count)
292 {
293 return strnlen(s, count);
294 }
295 #endif
296
297 /*
298 * rb_snprintf_append()
299 * appends snprintf formatted string to the end of the buffer but not
300 * exceeding len
301 */
302 int
303 rb_snprintf_append(char *str, size_t len, const char *format, ...)
304 {
305 if(len == 0)
306 return 0;
307
308 size_t x = strlen(str);
309
310 if(len < x)
311 {
312 str[len - 1] = '\0';
313 return (int)len - 1;
314 }
315
316 va_list ap;
317 va_start(ap, format);
318 int y = (vsnprintf(str + x, len - x, format, ap) + (int)x);
319 va_end(ap);
320
321 return (y);
322 }
323
324 /* rb_basename
325 *
326 * input -
327 * output -
328 * side effects -
329 */
330 char *
331 rb_basename(const char *path)
332 {
333 const char *s;
334
335 if(!(s = strrchr(path, '/')))
336 s = path;
337 else
338 s++;
339 return rb_strdup(s);
340 }
341
342 /*
343 * rb_dirname
344 */
345
346 char *
347 rb_dirname (const char *path)
348 {
349 char *s;
350
351 s = strrchr(path, '/');
352 if(s == NULL)
353 {
354 return rb_strdup(".");
355 }
356
357 /* remove extra slashes */
358 while(s > path && *s == '/')
359 --s;
360
361 return rb_strndup(path, ((uintptr_t)s - (uintptr_t)path) + 2);
362 }
363
364 size_t rb_zstring_serialized(rb_zstring_t *zs, void **buf, size_t *buflen)
365 {
366 uint8_t *p;
367 size_t alloclen = sizeof(uint16_t) + zs->len;
368
369 p = rb_malloc(sizeof(alloclen));
370 memcpy(p, &zs->len, sizeof(uint16_t));
371 p += sizeof(uint16_t);
372 memcpy(p, zs->data, zs->len);
373 return alloclen;
374 }
375
376 size_t rb_zstring_deserialize(rb_zstring_t *zs, void *buf)
377 {
378 uint8_t *p = (uint8_t *)buf;
379
380 memcpy(&zs->len, p, sizeof(uint16_t));
381 p += sizeof(uint16_t);
382 if(zs->len == 0)
383 {
384 zs->data = NULL;
385 return sizeof(uint16_t);
386 }
387 zs->data = rb_malloc(zs->len);
388 memcpy(zs->data, p, zs->len);
389 return zs->len + sizeof(uint16_t);
390 }
391
392 void rb_zstring_free(rb_zstring_t *zs)
393 {
394 rb_free(zs->data);
395 rb_free(zs);
396
397 }
398
399 rb_zstring_t *rb_zstring_alloc(void)
400 {
401 rb_zstring_t *zs = rb_malloc(sizeof(rb_zstring_t));
402 return zs;
403 }
404
405 rb_zstring_t *rb_zstring_from_c_len(const char *buf, size_t len)
406 {
407 rb_zstring_t *zs;
408
409 if(len > UINT16_MAX-1)
410 return NULL;
411
412 zs = rb_zstring_alloc();
413 zs->alloclen = zs->len = (uint16_t)len;
414 zs->alloclen = (uint16_t)len;
415 if(zs->alloclen < 128)
416 zs->alloclen = 128;
417 zs->data = rb_malloc(zs->alloclen);
418 memcpy(zs->data, buf, zs->len);
419 return(zs);
420 }
421
422 rb_zstring_t *rb_zstring_from_c(const char *buf)
423 {
424 return rb_zstring_from_c_len(buf, strlen(buf));
425 }
426
427 size_t rb_zstring_len(rb_zstring_t *zs)
428 {
429 return zs->len;
430 }
431
432 void rb_zstring_append_from_zstring(rb_zstring_t *dst_zs, rb_zstring_t *src_zs)
433 {
434 void *ep;
435 size_t nlen = dst_zs->len + src_zs->len;
436
437 if(nlen > dst_zs->alloclen)
438 {
439 dst_zs->alloclen += src_zs->len + 64;
440 dst_zs->data = rb_realloc(dst_zs->data, dst_zs->alloclen);
441 }
442
443 ep = dst_zs->data + dst_zs->len;
444 memcpy(ep, src_zs->data, src_zs->len);
445 }
446
447 void rb_zstring_append_from_c(rb_zstring_t *zs, const char *buf, size_t len)
448 {
449 void *ep;
450 size_t nlen = zs->len + len;
451
452 if(nlen > zs->alloclen)
453 {
454 zs->alloclen += len + 64;
455 zs->data = rb_realloc(zs->data, zs->alloclen);
456 }
457 ep = zs->data + zs->len;
458 zs->len += len;
459 memcpy(ep, buf, len);
460 }
461
462 char *rb_zstring_to_c(rb_zstring_t *zs, char *buf, size_t len)
463 {
464 size_t cpylen;
465 if(len < zs->len)
466 cpylen = len - 1;
467 else
468 cpylen = zs->len;
469 buf[cpylen] = '\0';
470 memcpy(buf, zs->data, cpylen);
471 return buf;
472 }
473
474
475 char *rb_zstring_to_c_alloc(rb_zstring_t *zs)
476 {
477 char *p;
478 p = rb_malloc(zs->len+1);
479 memcpy(p, zs->data, zs->len);
480 return p;
481 }
482
483 size_t rb_zstring_to_ptr(rb_zstring_t *zs, void **ptr)
484 {
485 *ptr = (void *)zs->data;
486 return zs->len;
487 }