annotate lib/libavutil/bprint.h @ 2221:c69013c186c7

for video
author Ritor1
date Tue, 18 Feb 2014 16:18:20 +0600
parents 992d2e6f907d
children
rev   line source
2134
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
1 /*
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
2 * Copyright (c) 2012 Nicolas George
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
3 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
4 * This file is part of FFmpeg.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
5 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
6 * FFmpeg is free software; you can redistribute it and/or
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
9 * version 2.1 of the License, or (at your option) any later version.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
10 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
11 * FFmpeg is distributed in the hope that it will be useful,
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
14 * Lesser General Public License for more details.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
15 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
17 * License along with FFmpeg; if not, write to the Free Software
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
19 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
20
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
21 #ifndef AVUTIL_BPRINT_H
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
22 #define AVUTIL_BPRINT_H
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
23
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
24 #include "attributes.h"
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
25 #include "avstring.h"
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
26
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
27 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
28 * Define a structure with extra padding to a fixed size
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
29 * This helps ensuring binary compatibility with future versions.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
30 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
31 #define FF_PAD_STRUCTURE(size, ...) \
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
32 __VA_ARGS__ \
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
33 char reserved_padding[size - sizeof(struct { __VA_ARGS__ })];
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
34
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
35 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
36 * Buffer to print data progressively
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
37 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
38 * The string buffer grows as necessary and is always 0-terminated.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
39 * The content of the string is never accessed, and thus is
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
40 * encoding-agnostic and can even hold binary data.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
41 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
42 * Small buffers are kept in the structure itself, and thus require no
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
43 * memory allocation at all (unless the contents of the buffer is needed
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
44 * after the structure goes out of scope). This is almost as lightweight as
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
45 * declaring a local "char buf[512]".
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
46 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
47 * The length of the string can go beyond the allocated size: the buffer is
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
48 * then truncated, but the functions still keep account of the actual total
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
49 * length.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
50 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
51 * In other words, buf->len can be greater than buf->size and records the
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
52 * total length of what would have been to the buffer if there had been
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
53 * enough memory.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
54 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
55 * Append operations do not need to be tested for failure: if a memory
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
56 * allocation fails, data stop being appended to the buffer, but the length
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
57 * is still updated. This situation can be tested with
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
58 * av_bprint_is_complete().
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
59 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
60 * The size_max field determines several possible behaviours:
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
61 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
62 * size_max = -1 (= UINT_MAX) or any large value will let the buffer be
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
63 * reallocated as necessary, with an amortized linear cost.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
64 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
65 * size_max = 0 prevents writing anything to the buffer: only the total
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
66 * length is computed. The write operations can then possibly be repeated in
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
67 * a buffer with exactly the necessary size
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
68 * (using size_init = size_max = len + 1).
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
69 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
70 * size_max = 1 is automatically replaced by the exact size available in the
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
71 * structure itself, thus ensuring no dynamic memory allocation. The
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
72 * internal buffer is large enough to hold a reasonable paragraph of text,
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
73 * such as the current paragraph.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
74 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
75 typedef struct AVBPrint {
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
76 FF_PAD_STRUCTURE(1024,
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
77 char *str; /** string so far */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
78 unsigned len; /** length so far */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
79 unsigned size; /** allocated memory */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
80 unsigned size_max; /** maximum allocated memory */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
81 char reserved_internal_buffer[1];
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
82 )
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
83 } AVBPrint;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
84
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
85 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
86 * Convenience macros for special values for av_bprint_init() size_max
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
87 * parameter.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
88 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
89 #define AV_BPRINT_SIZE_UNLIMITED ((unsigned)-1)
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
90 #define AV_BPRINT_SIZE_AUTOMATIC 1
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
91 #define AV_BPRINT_SIZE_COUNT_ONLY 0
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
92
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
93 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
94 * Init a print buffer.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
95 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
96 * @param buf buffer to init
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
97 * @param size_init initial size (including the final 0)
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
98 * @param size_max maximum size;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
99 * 0 means do not write anything, just count the length;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
100 * 1 is replaced by the maximum value for automatic storage;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
101 * any large value means that the internal buffer will be
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
102 * reallocated as needed up to that limit; -1 is converted to
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
103 * UINT_MAX, the largest limit possible.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
104 * Check also AV_BPRINT_SIZE_* macros.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
105 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
106 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
107
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
108 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
109 * Init a print buffer using a pre-existing buffer.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
110 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
111 * The buffer will not be reallocated.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
112 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
113 * @param buf buffer structure to init
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
114 * @param buffer byte buffer to use for the string data
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
115 * @param size size of buffer
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
116 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
117 void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
118
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
119 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
120 * Append a formatted string to a print buffer.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
121 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
122 void av_bprintf(AVBPrint *buf, const char *fmt, ...) av_printf_format(2, 3);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
123
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
124 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
125 * Append char c n times to a print buffer.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
126 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
127 void av_bprint_chars(AVBPrint *buf, char c, unsigned n);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
128
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
129 struct tm;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
130 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
131 * Append a formatted date and time to a print buffer.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
132 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
133 * param buf bprint buffer to use
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
134 * param fmt date and time format string, see strftime()
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
135 * param tm broken-down time structure to translate
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
136 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
137 * @note due to poor design of the standard strftime function, it may
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
138 * produce poor results if the format string expands to a very long text and
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
139 * the bprint buffer is near the limit stated by the size_max option.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
140 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
141 void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
142
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
143 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
144 * Allocate bytes in the buffer for external use.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
145 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
146 * @param[in] buf buffer structure
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
147 * @param[in] size required size
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
148 * @param[out] mem pointer to the memory area
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
149 * @param[out] actual_size size of the memory area after allocation;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
150 * can be larger or smaller than size
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
151 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
152 void av_bprint_get_buffer(AVBPrint *buf, unsigned size,
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
153 unsigned char **mem, unsigned *actual_size);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
154
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
155 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
156 * Reset the string to "" but keep internal allocated data.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
157 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
158 void av_bprint_clear(AVBPrint *buf);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
159
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
160 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
161 * Test if the print buffer is complete (not truncated).
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
162 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
163 * It may have been truncated due to a memory allocation failure
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
164 * or the size_max limit (compare size and size_max if necessary).
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
165 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
166 static inline int av_bprint_is_complete(AVBPrint *buf)
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
167 {
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
168 return buf->len < buf->size;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
169 }
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
170
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
171 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
172 * Finalize a print buffer.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
173 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
174 * The print buffer can no longer be used afterwards,
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
175 * but the len and size fields are still valid.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
176 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
177 * @arg[out] ret_str if not NULL, used to return a permanent copy of the
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
178 * buffer contents, or NULL if memory allocation fails;
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
179 * if NULL, the buffer is discarded and freed
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
180 * @return 0 for success or error code (probably AVERROR(ENOMEM))
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
181 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
182 int av_bprint_finalize(AVBPrint *buf, char **ret_str);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
183
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
184 /**
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
185 * Escape the content in src and append it to dstbuf.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
186 *
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
187 * @param dstbuf already inited destination bprint buffer
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
188 * @param src string containing the text to escape
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
189 * @param special_chars string containing the special characters which
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
190 * need to be escaped, can be NULL
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
191 * @param mode escape mode to employ, see AV_ESCAPE_MODE_* macros.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
192 * Any unknown value for mode will be considered equivalent to
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
193 * AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
194 * notice.
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
195 * @param flags flags which control how to escape, see AV_ESCAPE_FLAG_* macros
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
196 */
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
197 void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
198 enum AVEscapeMode mode, int flags);
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
199
992d2e6f907d preparation for libavcodec
zipi
parents:
diff changeset
200 #endif /* AVUTIL_BPRINT_H */