]> mj.ucw.cz Git - libucw.git/blob - lib/ff-printf.c
enlarge MAX_ATTR_SIZE
[libucw.git] / lib / ff-printf.c
1 /*
2  *      Sherlock Library -- Printf on Fastbuf Streams
3  *
4  *      (c) 2002 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12
13 #include <alloca.h>
14
15 int
16 vbprintf(struct fastbuf *b, byte *msg, va_list args)
17 {
18   byte *buf;
19   int len, r;
20
21   len = bdirect_write_prepare(b, &buf);
22   if (len >= 16)
23     {
24       r = vsnprintf(buf, len, msg, args);
25       if (r < 0)
26         len = 256;
27       else if (r < len)
28         {
29           bdirect_write_commit(b, buf+r);
30           return r;
31         }
32       else
33         len = r+1;
34     }
35   else
36     len = 256;
37
38   while (1)
39     {
40       buf = alloca(len);
41       r = vsnprintf(buf, len, msg, args);
42       if (r < 0)
43         len += len;
44       else if (r < len)
45         {
46           bwrite(b, buf, r);
47           return r;
48         }
49       else
50         len = r+1;
51     }
52 }
53
54 int
55 bprintf(struct fastbuf *b, byte *msg, ...)
56 {
57   va_list args;
58   int res;
59
60   va_start(args, msg);
61   res = vbprintf(b, msg, args);
62   va_end(args);
63   return res;
64 }
65
66 #ifdef TEST
67
68 int main(void)
69 {
70   struct fastbuf *b = bfdopen_shared(1, 65536);
71   for (int i=0; i<10000; i++)
72     bprintf(b, "13=%d str=<%s> msg=%m\n", 13, "str");
73   bclose(b);
74   return 0;
75 }
76
77 #endif