2 * UCW Library -- Memory Pools (Formatting)
4 * (c) 2005 Martin Mares <mj@ucw.cz>
5 * (c) 2007 Pavel Charvat <pchar@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include "ucw/mempool.h"
19 mp_vprintf_at(struct mempool *mp, uns ofs, const char *fmt, va_list args)
21 char *ret = mp_grow(mp, ofs + 1) + ofs;
24 int cnt = vsnprintf(ret, mp_avail(mp) - ofs, fmt, args2);
28 /* Our C library doesn't support C99 return value of vsnprintf, so we need to iterate */
31 ret = mp_expand(mp) + ofs;
33 cnt = vsnprintf(ret, mp_avail(mp) - ofs, fmt, args2);
38 else if ((uns)cnt >= mp_avail(mp) - ofs)
40 ret = mp_grow(mp, ofs + cnt + 1) + ofs;
42 int cnt2 = vsnprintf(ret, cnt + 1, fmt, args2);
46 mp_end(mp, ret + cnt + 1);
51 mp_vprintf(struct mempool *mp, const char *fmt, va_list args)
54 return mp_vprintf_at(mp, 0, fmt, args);
58 mp_printf(struct mempool *p, const char *fmt, ...)
62 char *res = mp_vprintf(p, fmt, args);
68 mp_vprintf_append(struct mempool *mp, char *ptr, const char *fmt, va_list args)
70 uns ofs = mp_open(mp, ptr);
71 ASSERT(ofs && !ptr[ofs - 1]);
72 return mp_vprintf_at(mp, ofs - 1, fmt, args);
76 mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...)
80 char *res = mp_vprintf_append(mp, ptr, fmt, args);
89 struct mempool *mp = mp_new(64);
90 char *x = mp_printf(mp, "<Hello, %s!>", "World");
92 x = mp_printf_append(mp, x, "<Appended>");
94 x = mp_printf(mp, "<Hello, %50s!>\n", "World");