/*1* Copyright (c) 2012 Nicolas George2*3* This file is part of FFmpeg.4*5* FFmpeg is free software; you can redistribute it and/or6* modify it under the terms of the GNU Lesser General Public7* License as published by the Free Software Foundation; either8* version 2.1 of the License, or (at your option) any later version.9*10* FFmpeg is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU13* Lesser General Public License for more details.14*15* You should have received a copy of the GNU Lesser General Public16* License along with FFmpeg; if not, write to the Free Software17* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA18*/1920#ifndef AVUTIL_BPRINT_H21#define AVUTIL_BPRINT_H2223#include <stdarg.h>2425#include "attributes.h"26#include "avstring.h"2728/**29* Define a structure with extra padding to a fixed size30* This helps ensuring binary compatibility with future versions.31*/3233#define FF_PAD_STRUCTURE(name, size, ...) \34struct ff_pad_helper_##name { __VA_ARGS__ }; \35typedef struct name { \36__VA_ARGS__ \37char reserved_padding[size - sizeof(struct ff_pad_helper_##name)]; \38} name;3940/**41* Buffer to print data progressively42*43* The string buffer grows as necessary and is always 0-terminated.44* The content of the string is never accessed, and thus is45* encoding-agnostic and can even hold binary data.46*47* Small buffers are kept in the structure itself, and thus require no48* memory allocation at all (unless the contents of the buffer is needed49* after the structure goes out of scope). This is almost as lightweight as50* declaring a local "char buf[512]".51*52* The length of the string can go beyond the allocated size: the buffer is53* then truncated, but the functions still keep account of the actual total54* length.55*56* In other words, buf->len can be greater than buf->size and records the57* total length of what would have been to the buffer if there had been58* enough memory.59*60* Append operations do not need to be tested for failure: if a memory61* allocation fails, data stop being appended to the buffer, but the length62* is still updated. This situation can be tested with63* av_bprint_is_complete().64*65* The size_max field determines several possible behaviours:66*67* size_max = -1 (= UINT_MAX) or any large value will let the buffer be68* reallocated as necessary, with an amortized linear cost.69*70* size_max = 0 prevents writing anything to the buffer: only the total71* length is computed. The write operations can then possibly be repeated in72* a buffer with exactly the necessary size73* (using size_init = size_max = len + 1).74*75* size_max = 1 is automatically replaced by the exact size available in the76* structure itself, thus ensuring no dynamic memory allocation. The77* internal buffer is large enough to hold a reasonable paragraph of text,78* such as the current paragraph.79*/8081FF_PAD_STRUCTURE(AVBPrint, 1024,82char *str; /**< string so far */83unsigned len; /**< length so far */84unsigned size; /**< allocated memory */85unsigned size_max; /**< maximum allocated memory */86char reserved_internal_buffer[1];87)8889/**90* Convenience macros for special values for av_bprint_init() size_max91* parameter.92*/93#define AV_BPRINT_SIZE_UNLIMITED ((unsigned)-1)94#define AV_BPRINT_SIZE_AUTOMATIC 195#define AV_BPRINT_SIZE_COUNT_ONLY 09697/**98* Init a print buffer.99*100* @param buf buffer to init101* @param size_init initial size (including the final 0)102* @param size_max maximum size;103* 0 means do not write anything, just count the length;104* 1 is replaced by the maximum value for automatic storage;105* any large value means that the internal buffer will be106* reallocated as needed up to that limit; -1 is converted to107* UINT_MAX, the largest limit possible.108* Check also AV_BPRINT_SIZE_* macros.109*/110void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max);111112/**113* Init a print buffer using a pre-existing buffer.114*115* The buffer will not be reallocated.116*117* @param buf buffer structure to init118* @param buffer byte buffer to use for the string data119* @param size size of buffer120*/121void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size);122123/**124* Append a formatted string to a print buffer.125*/126void av_bprintf(AVBPrint *buf, const char *fmt, ...) av_printf_format(2, 3);127128/**129* Append a formatted string to a print buffer.130*/131void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg);132133/**134* Append char c n times to a print buffer.135*/136void av_bprint_chars(AVBPrint *buf, char c, unsigned n);137138/**139* Append data to a print buffer.140*141* param buf bprint buffer to use142* param data pointer to data143* param size size of data144*/145void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size);146147struct tm;148/**149* Append a formatted date and time to a print buffer.150*151* param buf bprint buffer to use152* param fmt date and time format string, see strftime()153* param tm broken-down time structure to translate154*155* @note due to poor design of the standard strftime function, it may156* produce poor results if the format string expands to a very long text and157* the bprint buffer is near the limit stated by the size_max option.158*/159void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm);160161/**162* Allocate bytes in the buffer for external use.163*164* @param[in] buf buffer structure165* @param[in] size required size166* @param[out] mem pointer to the memory area167* @param[out] actual_size size of the memory area after allocation;168* can be larger or smaller than size169*/170void av_bprint_get_buffer(AVBPrint *buf, unsigned size,171unsigned char **mem, unsigned *actual_size);172173/**174* Reset the string to "" but keep internal allocated data.175*/176void av_bprint_clear(AVBPrint *buf);177178/**179* Test if the print buffer is complete (not truncated).180*181* It may have been truncated due to a memory allocation failure182* or the size_max limit (compare size and size_max if necessary).183*/184static inline int av_bprint_is_complete(const AVBPrint *buf)185{186return buf->len < buf->size;187}188189/**190* Finalize a print buffer.191*192* The print buffer can no longer be used afterwards,193* but the len and size fields are still valid.194*195* @arg[out] ret_str if not NULL, used to return a permanent copy of the196* buffer contents, or NULL if memory allocation fails;197* if NULL, the buffer is discarded and freed198* @return 0 for success or error code (probably AVERROR(ENOMEM))199*/200int av_bprint_finalize(AVBPrint *buf, char **ret_str);201202/**203* Escape the content in src and append it to dstbuf.204*205* @param dstbuf already inited destination bprint buffer206* @param src string containing the text to escape207* @param special_chars string containing the special characters which208* need to be escaped, can be NULL209* @param mode escape mode to employ, see AV_ESCAPE_MODE_* macros.210* Any unknown value for mode will be considered equivalent to211* AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without212* notice.213* @param flags flags which control how to escape, see AV_ESCAPE_FLAG_* macros214*/215void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,216enum AVEscapeMode mode, int flags);217218#endif /* AVUTIL_BPRINT_H */219220221