]> mj.ucw.cz Git - libucw.git/blob - lib/mempool-str.c
Moved endianity settings etc. to the per-CPU section.
[libucw.git] / lib / mempool-str.c
1 /*
2  *      UCW 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/mempool.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 }
53
54 #ifdef TEST
55
56 #include <stdio.h>
57
58 int main(void)
59 {
60   struct mempool *p = mp_new(64);
61   char *s = mp_strdup(p, "12345");
62   char *c = mp_multicat(p, "<<", s, ">>", NULL);
63   puts(c);
64   return 0;
65 }
66
67 #endif