]> mj.ucw.cz Git - libucw.git/blob - lib/mempool-str.c
Added bit_array_assign(), replaced BIT_ARRAY_ALLOC by functions.
[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 void *
26 mp_memdup(struct mempool *p, void *s, uns len)
27 {
28   void *t = mp_alloc_fast(p, len);
29   memcpy(t, s, len);
30   return t;
31 }
32
33 char *
34 mp_multicat(struct mempool *p, ...)
35 {
36   va_list args, a;
37   va_start(args, p);
38   char *x, *y;
39   uns cnt = 0;
40   va_copy(a, args);
41   while (x = va_arg(a, char *))
42     cnt++;
43   uns *sizes = alloca(cnt * sizeof(uns));
44   uns len = 1;
45   cnt = 0;
46   va_end(a);
47   va_copy(a, args);
48   while (x = va_arg(a, char *))
49     len += sizes[cnt++] = strlen(x);
50   char *buf = mp_alloc_fast_noalign(p, len);
51   y = buf;
52   va_end(a);
53   cnt = 0;
54   while (x = va_arg(args, char *))
55     {
56       memcpy(y, x, sizes[cnt]);
57       y += sizes[cnt++];
58     }
59   *y = 0;
60   va_end(args);
61   return buf;
62 }
63
64 #ifdef TEST
65
66 #include <stdio.h>
67
68 int main(void)
69 {
70   struct mempool *p = mp_new(64);
71   char *s = mp_strdup(p, "12345");
72   char *c = mp_multicat(p, "<<", s, ">>", NULL);
73   puts(c);
74   return 0;
75 }
76
77 #endif