2 * Copyright (c) 2012 Nicolas George
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
30 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
32 static int av_bprint_alloc(AVBPrint *buf, unsigned room)
34 char *old_str, *new_str;
35 unsigned min_size, new_size;
37 if (buf->size == buf->size_max)
39 if (!av_bprint_is_complete(buf))
40 return AVERROR_INVALIDDATA; /* it is already truncated anyway */
41 min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
42 new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
43 if (new_size < min_size)
44 new_size = FFMIN(buf->size_max, min_size);
45 old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
46 new_str = av_realloc(old_str, new_size);
48 return AVERROR(ENOMEM);
50 memcpy(new_str, buf->str, buf->len + 1);
56 static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
58 /* arbitrary margin to avoid small overflows */
59 extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
60 buf->len += extra_len;
62 buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
65 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
67 unsigned size_auto = (char *)buf + sizeof(*buf) -
68 buf->reserved_internal_buffer;
72 buf->str = buf->reserved_internal_buffer;
74 buf->size = FFMIN(size_auto, size_max);
75 buf->size_max = size_max;
77 if (size_init > buf->size)
78 av_bprint_alloc(buf, size_init - 1);
81 void av_bprintf(AVBPrint *buf, const char *fmt, ...)
89 room = av_bprint_room(buf);
90 dst = room ? buf->str + buf->len : NULL;
92 extra_len = vsnprintf(dst, room, fmt, vl);
98 if (av_bprint_alloc(buf, extra_len))
101 av_bprint_grow(buf, extra_len);
104 void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
106 unsigned room, real_n;
109 room = av_bprint_room(buf);
112 if (av_bprint_alloc(buf, n))
116 real_n = FFMIN(n, room - 1);
117 memset(buf->str + buf->len, c, real_n);
119 av_bprint_grow(buf, n);
122 void av_bprint_clear(AVBPrint *buf)
130 int av_bprint_finalize(AVBPrint *buf, char **ret_str)
132 unsigned real_size = FFMIN(buf->len + 1, buf->size);
137 if (av_bprint_is_allocated(buf)) {
138 str = av_realloc(buf->str, real_size);
143 str = av_malloc(real_size);
145 memcpy(str, buf->str, real_size);
147 ret = AVERROR(ENOMEM);
151 if (av_bprint_is_allocated(buf))
154 buf->size = real_size;
162 static void bprint_pascal(AVBPrint *b, unsigned size)
164 unsigned p[size + 1], i, j;
167 av_bprintf(b, "%8d\n", 1);
168 for (i = 1; i <= size; i++) {
170 for (j = i - 1; j > 0; j--)
171 p[j] = p[j] + p[j - 1];
172 for (j = 0; j <= i; j++)
173 av_bprintf(b, "%8d", p[j]);
182 av_bprint_init(&b, 0, -1);
183 bprint_pascal(&b, 5);
184 printf("Short text in unlimited buffer: %zu/%u\n", strlen(b.str), b.len);
185 printf("%s\n", b.str);
186 av_bprint_finalize(&b, NULL);
188 av_bprint_init(&b, 0, -1);
189 bprint_pascal(&b, 25);
190 printf("Long text in unlimited buffer: %zu/%u\n", strlen(b.str), b.len);
191 av_bprint_finalize(&b, NULL);
193 av_bprint_init(&b, 0, 2048);
194 bprint_pascal(&b, 25);
195 printf("Long text in limited buffer: %zu/%u\n", strlen(b.str), b.len);
196 av_bprint_finalize(&b, NULL);
198 av_bprint_init(&b, 0, 1);
199 bprint_pascal(&b, 5);
200 printf("Short text in automatic buffer: %zu/%u\n", strlen(b.str), b.len);
202 av_bprint_init(&b, 0, 1);
203 bprint_pascal(&b, 25);
204 printf("Long text in automatic buffer: %zu/%u\n", strlen(b.str), b.len);
205 /* Note that the size of the automatic buffer is arch-dependant. */
207 av_bprint_init(&b, 0, 0);
208 bprint_pascal(&b, 25);
209 printf("Long text count only buffer: %zu/%u\n", strlen(b.str), b.len);