]> mj.ucw.cz Git - libucw.git/blob - lib/ff-printf.c
bugfix in image scaling (select the correct strategy)
[libucw.git] / lib / ff-printf.c
1 /*
2  *      UCW Library -- Printf on Fastbuf Streams
3  *
4  *      (c) 2002--2005 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, char *msg, va_list args)
17 {
18   byte *buf;
19   int len, r;
20   va_list args2;
21
22   len = bdirect_write_prepare(b, &buf);
23   if (len >= 16)
24     {
25       va_copy(args2, args);
26       r = vsnprintf(buf, len, msg, args2);
27       va_end(args2);
28       if (r < 0)
29         len = 256;
30       else if (r < len)
31         {
32           bdirect_write_commit(b, buf+r);
33           return r;
34         }
35       else
36         len = r+1;
37     }
38   else
39     len = 256;
40
41   while (1)
42     {
43       buf = alloca(len);
44       va_copy(args2, args);
45       r = vsnprintf(buf, len, msg, args2);
46       va_end(args2);
47       if (r < 0)
48         len += len;
49       else if (r < len)
50         {
51           bwrite(b, buf, r);
52           return r;
53         }
54       else
55         len = r+1;
56     }
57 }
58
59 int
60 bprintf(struct fastbuf *b, char *msg, ...)
61 {
62   va_list args;
63   int res;
64
65   va_start(args, msg);
66   res = vbprintf(b, msg, args);
67   va_end(args);
68   return res;
69 }
70
71 #ifdef TEST
72
73 int main(void)
74 {
75   struct fastbuf *b = bfdopen_shared(1, 65536);
76   for (int i=0; i<10000; i++)
77     bprintf(b, "13=%d str=<%s> msg=%m\n", 13, "str");
78   bclose(b);
79   return 0;
80 }
81
82 #endif