From: Martin Mares Date: Fri, 2 Mar 2012 14:12:16 +0000 (+0100) Subject: Mempools: Implemented mp_str_from_mem() X-Git-Tag: v5.99~188 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=8f5f490eafa86470c6ab1285d13f8f300d53b227;p=libucw.git Mempools: Implemented mp_str_from_mem() --- diff --git a/ucw/mempool-str.c b/ucw/mempool-str.c index 970c9f4e..9391cbb6 100644 --- a/ucw/mempool-str.c +++ b/ucw/mempool-str.c @@ -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 @@ -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; } diff --git a/ucw/mempool.h b/ucw/mempool.h index bcc22bd3..350054e3 100644 --- a/ucw/mempool.h +++ b/ucw/mempool.h @@ -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; /*** diff --git a/ucw/mempool.t b/ucw/mempool.t index 3aeb2558..61956d49 100644 --- a/ucw/mempool.t +++ b/ucw/mempool.t @@ -9,3 +9,4 @@ Run: ../obj/ucw/mempool-str-t Out: <<12345>> bugs.gnats.insects bugsgnatsinsects + 23