]> mj.ucw.cz Git - libucw.git/blob - lib/pool-str.c
Added mp_strcat() and mp_multicat().
[libucw.git] / lib / pool-str.c
1 /*
2  *      Sherlock Library -- Memory Pools (String Operations)
3  *
4  *      (c) 2004 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/pools.h"
12
13 #include <alloca.h>
14 #include <string.h>
15
16 char *
17 mp_strdup(struct mempool *p, char *s)
18 {
19   uns l = strlen(s) + 1;
20   char *t = mp_alloc_fast_noalign(p, l);
21   memcpy(t, s, l);
22   return t;
23 }
24
25 char *
26 mp_multicat(struct mempool *p, ...)
27 {
28   va_list args, a;
29   va_start(args, p);
30   char *x, *y;
31   uns cnt = 0;
32   a = args;
33   while (x = va_arg(a, char *))
34     cnt++;
35   uns *sizes = alloca(cnt * sizeof(uns));
36   uns len = 1;
37   cnt = 0;
38   a = args;
39   while (x = va_arg(a, char *))
40     len += sizes[cnt++] = strlen(x);
41   char *buf = mp_alloc_fast_noalign(p, len);
42   y = buf;
43   a = args;
44   cnt = 0;
45   while (x = va_arg(a, char *))
46     {
47       memcpy(y, x, sizes[cnt]);
48       y += sizes[cnt++];
49     }
50   *y = 0;
51   return buf;
52 }