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