From: Martin Mares Date: Thu, 10 Feb 2005 21:57:17 +0000 (+0000) Subject: Added printf-like function with output to mempool. X-Git-Tag: holmes-import~833 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=70398f11c811ae9acad63c31eec3611b2b7322d9;p=libucw.git Added printf-like function with output to mempool. --- diff --git a/lib/Makefile b/lib/Makefile index cdb02bac..c2d3e485 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -7,7 +7,7 @@ PROGS+=obj/lib/db-tool endif LIBUCW_MODS= \ - alloc alloc_str realloc mempool mempool-str \ + alloc alloc_str realloc mempool mempool-str mempool-fmt \ mmap pagecache partmap hashfunc \ lists sorter bitsig \ log log-file proctitle \ diff --git a/lib/mempool-fmt.c b/lib/mempool-fmt.c new file mode 100644 index 00000000..47bb71eb --- /dev/null +++ b/lib/mempool-fmt.c @@ -0,0 +1,76 @@ +/* + * UCW Library -- Memory Pools (Formatting) + * + * (c) 2005 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" +#include "lib/mempool.h" + +#include +#include +#include + +char * +mp_vprintf(struct mempool *p, char *fmt, va_list args) +{ + char *ret = p->free; + int free = p->last - p->free; + if (!free) + { + ret = mp_alloc(p, 1); + free = p->last - p->free; + } + int cnt = vsnprintf(ret, free, fmt, args); + if (cnt < 0) + { + /* Our C library doesn't support C99 return value of vsnprintf, so we need to iterate */ + uns len = 128; + char *buf; + do + { + len *= 2; + buf = alloca(len); + cnt = vsnprintf(buf, len, fmt, args); + } + while (cnt < 0); + ret = mp_alloc(p, cnt+1); + memcpy(ret, buf, cnt+1); + } + else if (cnt < free) + p->free += cnt + 1; + else + { + ret = mp_alloc(p, cnt+1); + int cnt2 = vsnprintf(ret, cnt+1, fmt, args); + ASSERT(cnt2 == cnt); + } + return ret; +} + +char * +mp_printf(struct mempool *p, char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + char *res = mp_vprintf(p, fmt, args); + va_end(args); + return res; +} + +#ifdef TEST + +int main(void) +{ + struct mempool *mp = mp_new(64); + char *x = mp_printf(mp, "Hello, %s!\n", "World"); + fputs(x, stdout); + x = mp_printf(mp, "Hello, %100s!\n", "World"); + fputs(x, stdout); + return 0; +} + +#endif diff --git a/lib/mempool.h b/lib/mempool.h index 8d964e4d..7290c256 100644 --- a/lib/mempool.h +++ b/lib/mempool.h @@ -1,7 +1,7 @@ /* * UCW Library -- Memory Pools * - * (c) 1997--2004 Martin Mares + * (c) 1997--2005 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -60,7 +60,7 @@ mp_end_string(struct mempool *p, void *stop) p->free = stop; } -/* pool-str.c */ +/* mempool-str.c */ char *mp_strdup(struct mempool *, char *); char *mp_multicat(struct mempool *, ...); @@ -70,4 +70,9 @@ mp_strcat(struct mempool *mp, char *x, char *y) return mp_multicat(mp, x, y, NULL); } +/* mempool-fmt.c */ + +char *mp_printf(struct mempool *p, char *fmt, ...); +char *mp_vprintf(struct mempool *p, char *fmt, va_list args); + #endif