]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/mempool-str.c
xtypes: added first shot on unit parser
[libucw.git] / ucw / mempool-str.c
index cb2466304a902147f7be3b52fd028257ba06a2d1..1072e3dacb7fde29a0deceef692d77732478b6fa 100644 (file)
@@ -7,8 +7,8 @@
  *     of the GNU Lesser General Public License.
  */
 
-#include "ucw/lib.h"
-#include "ucw/mempool.h"
+#include <ucw/lib.h>
+#include <ucw/mempool.h>
 
 #include <alloca.h>
 #include <string.h>
@@ -18,14 +18,14 @@ mp_strdup(struct mempool *p, const char *s)
 {
   if (!s)
     return NULL;
-  uns l = strlen(s) + 1;
+  size_t l = strlen(s) + 1;
   char *t = mp_alloc_fast_noalign(p, l);
   memcpy(t, s, l);
   return t;
 }
 
 void *
-mp_memdup(struct mempool *p, const void *s, uns len)
+mp_memdup(struct mempool *p, const void *s, size_t len)
 {
   void *t = mp_alloc_fast(p, len);
   memcpy(t, s, len);
@@ -38,12 +38,12 @@ mp_multicat(struct mempool *p, ...)
   va_list args, a;
   va_start(args, p);
   char *x, *y;
-  uns cnt = 0;
+  uint cnt = 0;
   va_copy(a, args);
   while (x = va_arg(a, char *))
     cnt++;
-  uns *sizes = alloca(cnt * sizeof(uns));
-  uns len = 1;
+  size_t *sizes = alloca(cnt * sizeof(*sizes));
+  size_t len = 1;
   cnt = 0;
   va_end(a);
   va_copy(a, args);
@@ -64,17 +64,17 @@ mp_multicat(struct mempool *p, ...)
 }
 
 char *
-mp_strjoin(struct mempool *p, char **a, uns n, uns sep)
+mp_strjoin(struct mempool *p, char **a, uint n, uint sep)
 {
-  uns sizes[n];
-  uns len = 1;
-  for (uns i=0; i<n; i++)
+  size_t sizes[n];
+  size_t len = 1;
+  for (uint i=0; i<n; i++)
     len += sizes[i] = strlen(a[i]);
   if (sep && n)
     len += n-1;
   char *dest = mp_alloc_fast_noalign(p, len);
   char *d = dest;
-  for (uns i=0; i<n; i++)
+  for (uint i=0; i<n; i++)
     {
       if (sep && i)
        *d++ = sep;
@@ -85,6 +85,15 @@ mp_strjoin(struct mempool *p, char **a, uns n, uns sep)
   return dest;
 }
 
+char *
+mp_str_from_mem(struct mempool *a, const void *mem, size_t len)
+{
+  char *str = mp_alloc_noalign(a, len+1);
+  memcpy(str, mem, len);
+  str[len] = 0;
+  return str;
+}
+
 #ifdef TEST
 
 #include <stdio.h>
@@ -98,6 +107,7 @@ int main(void)
   char *a[] = { "bugs", "gnats", "insects" };
   puts(mp_strjoin(p, a, 3, '.'));
   puts(mp_strjoin(p, a, 3, 0));
+  puts(mp_str_from_mem(p, s+1, 2));
   return 0;
 }