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 module source.ffmpeg.libavutil.bprint;
21 
22 import ffmpeg.libavutil;
23 
24 import core.stdc.stdint;
25 import core.stdc.stdarg;
26 
27 extern (C) @nogc nothrow:
28 
29 /**
30  * Define a structure with extra padding to a fixed size
31  * This helps ensuring binary compatibility with future versions.
32  */
33 
34 /**
35  * Buffer to print data progressively
36  *
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.
40  *
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]".
45  *
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
48  * length.
49  *
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
52  * enough memory.
53  *
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().
58  *
59  * The size_max field determines several possible behaviours:
60  *
61  * size_max = -1 (= UINT_MAX) or any large value will let the buffer be
62  * reallocated as necessary, with an amortized linear cost.
63  *
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).
68  *
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.
73  */
74 
75 struct AVBPrint;
76 /*{
77     char* str;
78     uint len;
79     uint size;
80     uint size_max;
81     char[FF_PAD_STRUCTURE] reserved_internal_buffer;
82     char[1000] reserved_padding;
83 }*/
84 
85 /**
86  * Convenience macros for special values for av_bprint_init() size_max
87  * parameter.
88  */
89 enum AV_BPRINT_SIZE_UNLIMITED = cast(uint) -1;
90 enum AV_BPRINT_SIZE_AUTOMATIC = 1;
91 enum 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, uint size_init, uint 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, uint size);
118 
119 /**
120  * Append a formatted string to a print buffer.
121  */
122 void av_bprintf (AVBPrint* buf, const(char)* fmt, ...);
123 
124 /**
125  * Append a formatted string to a print buffer.
126  */
127 void av_vbprintf (AVBPrint* buf, const(char)* fmt, va_list vl_arg);
128 
129 /**
130  * Append char c n times to a print buffer.
131  */
132 void av_bprint_chars (AVBPrint* buf, char c, uint n);
133 
134 /**
135  * Append data to a print buffer.
136  *
137  * param buf  bprint buffer to use
138  * param data pointer to data
139  * param size size of data
140  */
141 void av_bprint_append_data (AVBPrint* buf, const(char)* data, uint size);
142 
143 struct tm;
144 /**
145  * Append a formatted date and time to a print buffer.
146  *
147  * param buf  bprint buffer to use
148  * param fmt  date and time format string, see strftime()
149  * param tm   broken-down time structure to translate
150  *
151  * @note due to poor design of the standard strftime function, it may
152  * produce poor results if the format string expands to a very long text and
153  * the bprint buffer is near the limit stated by the size_max option.
154  */
155 void av_bprint_strftime (AVBPrint* buf, const(char)* fmt, const(tm)* tm);
156 
157 /**
158  * Allocate bytes in the buffer for external use.
159  *
160  * @param[in]  buf          buffer structure
161  * @param[in]  size         required size
162  * @param[out] mem          pointer to the memory area
163  * @param[out] actual_size  size of the memory area after allocation;
164  *                          can be larger or smaller than size
165  */
166 void av_bprint_get_buffer (
167     AVBPrint* buf,
168     uint size,
169     ubyte** mem,
170     uint* actual_size);
171 
172 /**
173  * Reset the string to "" but keep internal allocated data.
174  */
175 void av_bprint_clear (AVBPrint* buf);
176 
177 /**
178  * Test if the print buffer is complete (not truncated).
179  *
180  * It may have been truncated due to a memory allocation failure
181  * or the size_max limit (compare size and size_max if necessary).
182  */
183 int av_bprint_is_complete (const(AVBPrint)* buf);
184 
185 /**
186  * Finalize a print buffer.
187  *
188  * The print buffer can no longer be used afterwards,
189  * but the len and size fields are still valid.
190  *
191  * @arg[out] ret_str  if not NULL, used to return a permanent copy of the
192  *                    buffer contents, or NULL if memory allocation fails;
193  *                    if NULL, the buffer is discarded and freed
194  * @return  0 for success or error code (probably AVERROR(ENOMEM))
195  */
196 int av_bprint_finalize (AVBPrint* buf, char** ret_str);
197 
198 /**
199  * Escape the content in src and append it to dstbuf.
200  *
201  * @param dstbuf        already inited destination bprint buffer
202  * @param src           string containing the text to escape
203  * @param special_chars string containing the special characters which
204  *                      need to be escaped, can be NULL
205  * @param mode          escape mode to employ, see AV_ESCAPE_MODE_* macros.
206  *                      Any unknown value for mode will be considered equivalent to
207  *                      AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without
208  *                      notice.
209  * @param flags         flags which control how to escape, see AV_ESCAPE_FLAG_* macros
210  */
211 void av_bprint_escape (
212     AVBPrint* dstbuf,
213     const(char)* src,
214     const(char)* special_chars,
215     AVEscapeMode mode,
216     int flags);
217 
218 /* AVUTIL_BPRINT_H */