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
21 #ifndef AVUTIL_BPRINT_H
22 #define AVUTIL_BPRINT_H
24 #include "attributes.h"
27 * Define a structure with extra padding to a fixed size
28 * This helps ensuring binary compatibility with future versions.
30 #define FF_PAD_STRUCTURE(size, ...) \
32 char reserved_padding[size - sizeof(struct { __VA_ARGS__ })];
35 * Buffer to print data progressively
37 * The string buffer grows as necessary and is always 0-terminated.
38 * The content of the string is never accessed, and thus is
39 * encoding-agnostic and can even hold binary data.
41 * Small buffers are kept in the structure itself, and thus require no
42 * memory allocation at all (unless the contents of the buffer is needed
43 * after the structure goes out of scope). This is almost as lightweight as
44 * declaring a local "char buf[512]".
46 * The length of the string can go beyond the allocated size: the buffer is
47 * then truncated, but the functions still keep account of the actual total
50 * In other words, buf->len can be greater than buf->size and records the
51 * total length of what would have been to the buffer if there had been
54 * Append operations do not need to be tested for failure: if a memory
55 * allocation fails, data stop being appended to the buffer, but the length
56 * is still updated. This situation can be tested with
57 * av_bprint_is_complete().
59 * The size_max field determines several possible behaviours:
61 * size_max = -1 (= UINT_MAX) or any large value will let the buffer be
62 * reallocated as necessary, with an amortized linear cost.
64 * size_max = 0 prevents writing anything to the buffer: only the total
65 * length is computed. The write operations can then possibly be repeated in
66 * a buffer with exactly the necessary size
67 * (using size_init = size_max = len + 1).
69 * size_max = 1 is automatically replaced by the exact size available in the
70 * structure itself, thus ensuring no dynamic memory allocation. The
71 * internal buffer is large enough to hold a reasonable paragraph of text,
72 * such as the current paragraph.
74 typedef struct AVBPrint {
75 FF_PAD_STRUCTURE(1024,
76 char *str; /** string so far */
77 unsigned len; /** length so far */
78 unsigned size; /** allocated memory */
79 unsigned size_max; /** maximum allocated memory */
80 char reserved_internal_buffer[1];
85 * Init a print buffer.
87 * @param buf buffer to init
88 * @param size_init initial size (including the final 0)
89 * @param size_max maximum size;
90 * 0 means do not write anything, just count the length;
91 * 1 is replaced by the maximum value for automatic storage
93 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max);
96 * Convenience macros for special values for size_max.
98 #define AV_BPRINT_SIZE_UNLIMITED ((unsigned)-1)
99 #define AV_BPRINT_SIZE_AUTOMATIC 1
100 #define AV_BPRINT_SIZE_COUNT_ONLY 0
103 * Init a print buffer using a pre-existing buffer.
105 * The buffer will not be reallocated.
107 * @param buf buffer structure to init
108 * @param buffer byte buffer to use for the string data
109 * @param size size of buffer
111 void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size);
114 * Append a formated string to a print buffer.
116 void av_bprintf(AVBPrint *buf, const char *fmt, ...) av_printf_format(2, 3);
119 * Append char c n times to a print buffer.
121 void av_bprint_chars(AVBPrint *buf, char c, unsigned n);
124 * Reset the string to "" but keep internal allocated data.
126 void av_bprint_clear(AVBPrint *buf);
129 * Test if the print buffer is complete (not truncated).
131 * It may have been truncated due to a memory allocation failure
132 * or the size_max limit (compare size and size_max if necessary).
134 static inline int av_bprint_is_complete(AVBPrint *buf)
136 return buf->len < buf->size;
140 * Finalize a print buffer.
142 * The print buffer can no longer be used afterwards,
143 * but the len and size fields are still valid.
145 * @arg[out] ret_str if not NULL, used to return a permanent copy of the
146 * buffer contents, or NULL if memory allocation fails;
147 * if NULL, the buffer is discarded and freed
148 * @return 0 for success or error code (probably AVERROR(ENOMEM))
150 int av_bprint_finalize(AVBPrint *buf, char **ret_str);
152 #endif /* AVUTIL_BPRINT_H */