]> mj.ucw.cz Git - libucw.git/blob - ucw/mempool-fmt.c
Logging: Implemented log_switch().
[libucw.git] / ucw / mempool-fmt.c
1 /*
2  *      UCW Library -- Memory Pools (Formatting)
3  *
4  *      (c) 2005 Martin Mares <mj@ucw.cz>
5  *      (c) 2007 Pavel Charvat <pchar@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "ucw/lib.h"
12 #include "ucw/mempool.h"
13
14 #include <alloca.h>
15 #include <stdio.h>
16 #include <string.h>
17
18 static char *
19 mp_vprintf_at(struct mempool *mp, uns ofs, const char *fmt, va_list args)
20 {
21   char *ret = mp_grow(mp, ofs + 1) + ofs;
22   va_list args2;
23   va_copy(args2, args);
24   int cnt = vsnprintf(ret, mp_avail(mp) - ofs, fmt, args2);
25   va_end(args2);
26   if (cnt < 0)
27     {
28       /* Our C library doesn't support C99 return value of vsnprintf, so we need to iterate */
29       do
30         {
31           ret = mp_expand(mp) + ofs;
32           va_copy(args2, args);
33           cnt = vsnprintf(ret, mp_avail(mp) - ofs, fmt, args2);
34           va_end(args2);
35         }
36       while (cnt < 0);
37     }
38   else if ((uns)cnt >= mp_avail(mp) - ofs)
39     {
40       ret = mp_grow(mp, ofs + cnt + 1) + ofs;
41       va_copy(args2, args);
42       int cnt2 = vsnprintf(ret, cnt + 1, fmt, args2);
43       va_end(args2);
44       ASSERT(cnt2 == cnt);
45     }
46   mp_end(mp, ret + cnt + 1);
47   return ret - ofs;
48 }
49
50 char *
51 mp_vprintf(struct mempool *mp, const char *fmt, va_list args)
52 {
53   mp_start(mp, 1);
54   return mp_vprintf_at(mp, 0, fmt, args);
55 }
56
57 char *
58 mp_printf(struct mempool *p, const char *fmt, ...)
59 {
60   va_list args;
61   va_start(args, fmt);
62   char *res = mp_vprintf(p, fmt, args);
63   va_end(args);
64   return res;
65 }
66
67 char *
68 mp_vprintf_append(struct mempool *mp, char *ptr, const char *fmt, va_list args)
69 {
70   uns ofs = mp_open(mp, ptr);
71   ASSERT(ofs && !ptr[ofs - 1]);
72   return mp_vprintf_at(mp, ofs - 1, fmt, args);
73 }
74
75 char *
76 mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...)
77 {
78   va_list args;
79   va_start(args, fmt);
80   char *res = mp_vprintf_append(mp, ptr, fmt, args);
81   va_end(args);
82   return res;
83 }
84
85 #ifdef TEST
86
87 int main(void)
88 {
89   struct mempool *mp = mp_new(64);
90   char *x = mp_printf(mp, "<Hello, %s!>", "World");
91   fputs(x, stdout);
92   x = mp_printf_append(mp, x, "<Appended>");
93   fputs(x, stdout);
94   x = mp_printf(mp, "<Hello, %50s!>\n", "World");
95   fputs(x, stdout);
96   return 0;
97 }
98
99 #endif