]> mj.ucw.cz Git - libucw.git/commitdiff
Mempools: Implemented mp_str_from_mem()
authorMartin Mares <mj@ucw.cz>
Fri, 2 Mar 2012 14:12:16 +0000 (15:12 +0100)
committerMartin Mares <mj@ucw.cz>
Fri, 2 Mar 2012 14:12:16 +0000 (15:12 +0100)
ucw/mempool-str.c
ucw/mempool.h
ucw/mempool.t

index 970c9f4e826439d11e9eeaa1c3a689926bd1ff13..9391cbb6eb1db3b8739d74ed364a5a619c7c71bb 100644 (file)
@@ -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, uns 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;
 }
 
index bcc22bd35bcf755e0aca15ad05e15655bd66332b..350054e3fc481d0eb8a8879daabb1c1d021f7178 100644 (file)
@@ -410,6 +410,11 @@ static inline char *LIKE_MALLOC mp_strcat(struct mempool *mp, const char *x, con
  * tells how many there is of them.
  **/
 char *mp_strjoin(struct mempool *p, char **a, uns n, uns sep) LIKE_MALLOC;
+/**
+ * Convert memory block to a string. Makes a copy of the given memory block
+ * in the mempool @p, adding an extra terminating zero byte at the end.
+ **/
+char *mp_str_from_mem(struct mempool *p, const void *mem, uns len) LIKE_MALLOC;
 
 
 /***
index 3aeb2558a62ebbea98fa4ddbdd8aa0dcfdaf8f8f..61956d49483e16d5b2e332aebaf225e523d9ee11 100644 (file)
@@ -9,3 +9,4 @@ Run:    ../obj/ucw/mempool-str-t
 Out:   <<12345>>
        bugs.gnats.insects
        bugsgnatsinsects
+       23