]> mj.ucw.cz Git - libucw.git/blob - lib/mempool-str.c
Make autoconf recognize the amd64 architecture.
[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   va_copy(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   va_end(a);
39   va_copy(a, args);
40   while (x = va_arg(a, char *))
41     len += sizes[cnt++] = strlen(x);
42   char *buf = mp_alloc_fast_noalign(p, len);
43   y = buf;
44   va_end(a);
45   cnt = 0;
46   while (x = va_arg(args, char *))
47     {
48       memcpy(y, x, sizes[cnt]);
49       y += sizes[cnt++];
50     }
51   *y = 0;
52   va_end(args);
53   return buf;
54 }
55
56 #ifdef TEST
57
58 #include <stdio.h>
59
60 int main(void)
61 {
62   struct mempool *p = mp_new(64);
63   char *s = mp_strdup(p, "12345");
64   char *c = mp_multicat(p, "<<", s, ">>", NULL);
65   puts(c);
66   return 0;
67 }
68
69 #endif