31 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
32 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
36 char *old_str, *new_str;
37 unsigned min_size, new_size;
39 if (buf->size == buf->size_max)
43 min_size = buf->len + 1 +
FFMIN(UINT_MAX - buf->len - 1, room);
44 new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
45 if (new_size < min_size)
46 new_size =
FFMIN(buf->size_max, min_size);
52 memcpy(new_str, buf->str, buf->len + 1);
61 extra_len =
FFMIN(extra_len, UINT_MAX - 5 - buf->len);
62 buf->len += extra_len;
64 buf->str[
FFMIN(buf->len, buf->size - 1)] = 0;
69 unsigned size_auto = (
char *)buf +
sizeof(*buf) -
70 buf->reserved_internal_buffer;
74 buf->str = buf->reserved_internal_buffer;
76 buf->size =
FFMIN(size_auto, size_max);
77 buf->size_max = size_max;
79 if (size_init > buf->size)
101 dst = room ? buf->str + buf->len :
NULL;
103 extra_len =
vsnprintf(dst, room, fmt, vl);
107 if (extra_len < room)
117 unsigned room, real_n;
127 real_n =
FFMIN(n, room - 1);
128 memset(buf->str + buf->len, c, real_n);
142 if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
146 room = !room ? strlen(fmt) + 1 :
147 room <= INT_MAX / 2 ? room * 2 : INT_MAX;
156 if ((l = strftime(buf2,
sizeof(buf2), fmt, tm))) {
164 static const char txt[] =
"[truncated strftime output]";
165 memset(buf->str + buf->len,
'!', room);
166 memcpy(buf->str + buf->len, txt,
FFMIN(
sizeof(txt) - 1, room));
176 unsigned char **mem,
unsigned *actual_size)
181 *mem = *actual_size ? buf->str + buf->len :
NULL;
194 unsigned real_size =
FFMIN(buf->len + 1, buf->size);
207 memcpy(str, buf->str, real_size);
216 buf->size = real_size;
233 for (i = 1; i <=
size; i++) {
235 for (j = i - 1; j > 0; j--)
236 p[j] = p[j] + p[j - 1];
237 for (j = 0; j <= i; j++)
247 struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
250 bprint_pascal(&b, 5);
251 printf(
"Short text in unlimited buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
252 printf(
"%s\n", b.str);
256 bprint_pascal(&b, 25);
257 printf(
"Long text in unlimited buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
261 bprint_pascal(&b, 25);
262 printf(
"Long text in limited buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
266 bprint_pascal(&b, 5);
267 printf(
"Short text in automatic buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
270 bprint_pascal(&b, 25);
271 printf(
"Long text in automatic buffer: %u/%u\n", (
unsigned)strlen(b.str)/8*8, b.len);
275 bprint_pascal(&b, 25);
276 printf(
"Long text count only buffer: %u/%u\n", (
unsigned)strlen(b.str), b.len);
279 bprint_pascal(&b, 25);
280 printf(
"Long text count only buffer: %u/%u\n", (
unsigned)strlen(buf), b.len);
284 printf(
"strftime full: %u/%u \"%s\"\n", (
unsigned)strlen(buf), b.len, b.str);
289 printf(
"strftime truncated: %u/%u \"%s\"\n", (
unsigned)strlen(buf), b.len, b.str);