Path: blob/master/dep/ffmpeg/include/libavutil/bprint.h
4216 views
/*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/**21* @file22* @ingroup lavu_avbprint23* AVBPrint public header24*/2526#ifndef AVUTIL_BPRINT_H27#define AVUTIL_BPRINT_H2829#include <stdarg.h>3031#include "attributes.h"32#include "avstring.h"3334/**35* @defgroup lavu_avbprint AVBPrint36* @ingroup lavu_data37*38* A buffer to print data progressively39* @{40*/4142/**43* Define a structure with extra padding to a fixed size44* This helps ensuring binary compatibility with future versions.45*/4647#define FF_PAD_STRUCTURE(name, size, ...) \48struct ff_pad_helper_##name { __VA_ARGS__ }; \49typedef struct name { \50__VA_ARGS__ \51char reserved_padding[size - sizeof(struct ff_pad_helper_##name)]; \52} name;5354/**55* Buffer to print data progressively56*57* The string buffer grows as necessary and is always 0-terminated.58* The content of the string is never accessed, and thus is59* encoding-agnostic and can even hold binary data.60*61* Small buffers are kept in the structure itself, and thus require no62* memory allocation at all (unless the contents of the buffer is needed63* after the structure goes out of scope). This is almost as lightweight as64* declaring a local `char buf[512]`.65*66* The length of the string can go beyond the allocated size: the buffer is67* then truncated, but the functions still keep account of the actual total68* length.69*70* In other words, AVBPrint.len can be greater than AVBPrint.size and records71* the total length of what would have been to the buffer if there had been72* enough memory.73*74* Append operations do not need to be tested for failure: if a memory75* allocation fails, data stop being appended to the buffer, but the length76* is still updated. This situation can be tested with77* av_bprint_is_complete().78*79* The AVBPrint.size_max field determines several possible behaviours:80* - `size_max = -1` (= `UINT_MAX`) or any large value will let the buffer be81* reallocated as necessary, with an amortized linear cost.82* - `size_max = 0` prevents writing anything to the buffer: only the total83* length is computed. The write operations can then possibly be repeated in84* a buffer with exactly the necessary size85* (using `size_init = size_max = len + 1`).86* - `size_max = 1` is automatically replaced by the exact size available in the87* structure itself, thus ensuring no dynamic memory allocation. The88* internal buffer is large enough to hold a reasonable paragraph of text,89* such as the current paragraph.90*/9192FF_PAD_STRUCTURE(AVBPrint, 1024,93char *str; /**< string so far */94unsigned len; /**< length so far */95unsigned size; /**< allocated memory */96unsigned size_max; /**< maximum allocated memory */97char reserved_internal_buffer[1];98)99100/**101* @name Max size special values102* Convenience macros for special values for av_bprint_init() size_max103* parameter.104* @{105*/106107/**108* Buffer will be reallocated as necessary, with an amortized linear cost.109*/110#define AV_BPRINT_SIZE_UNLIMITED ((unsigned)-1)111/**112* Use the exact size available in the AVBPrint structure itself.113*114* Thus ensuring no dynamic memory allocation. The internal buffer is large115* enough to hold a reasonable paragraph of text, such as the current paragraph.116*/117#define AV_BPRINT_SIZE_AUTOMATIC 1118/**119* Do not write anything to the buffer, only calculate the total length.120*121* The write operations can then possibly be repeated in a buffer with122* exactly the necessary size (using `size_init = size_max = AVBPrint.len + 1`).123*/124#define AV_BPRINT_SIZE_COUNT_ONLY 0125/** @} */126127/**128* Init a print buffer.129*130* @param buf buffer to init131* @param size_init initial size (including the final 0)132* @param size_max maximum size;133* - `0` means do not write anything, just count the length134* - `1` is replaced by the maximum value for automatic storage135* any large value means that the internal buffer will be136* reallocated as needed up to that limit137* - `-1` is converted to `UINT_MAX`, the largest limit possible.138* Check also `AV_BPRINT_SIZE_*` macros.139*/140void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max);141142/**143* Init a print buffer using a pre-existing buffer.144*145* The buffer will not be reallocated.146* In case size equals zero, the AVBPrint will be initialized to use147* the internal buffer as if using AV_BPRINT_SIZE_COUNT_ONLY with148* av_bprint_init().149*150* @param buf buffer structure to init151* @param buffer byte buffer to use for the string data152* @param size size of buffer153*/154void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size);155156/**157* Append a formatted string to a print buffer.158*/159void av_bprintf(AVBPrint *buf, const char *fmt, ...) av_printf_format(2, 3);160161/**162* Append a formatted string to a print buffer.163*/164void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg);165166/**167* Append char c n times to a print buffer.168*/169void av_bprint_chars(AVBPrint *buf, char c, unsigned n);170171/**172* Append data to a print buffer.173*174* @param buf bprint buffer to use175* @param data pointer to data176* @param size size of data177*/178void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size);179180struct tm;181/**182* Append a formatted date and time to a print buffer.183*184* @param buf bprint buffer to use185* @param fmt date and time format string, see strftime()186* @param tm broken-down time structure to translate187*188* @note due to poor design of the standard strftime function, it may189* produce poor results if the format string expands to a very long text and190* the bprint buffer is near the limit stated by the size_max option.191*/192void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm);193194/**195* Allocate bytes in the buffer for external use.196*197* @param[in] buf buffer structure198* @param[in] size required size199* @param[out] mem pointer to the memory area200* @param[out] actual_size size of the memory area after allocation;201* can be larger or smaller than size202*/203void av_bprint_get_buffer(AVBPrint *buf, unsigned size,204unsigned char **mem, unsigned *actual_size);205206/**207* Reset the string to "" but keep internal allocated data.208*/209void av_bprint_clear(AVBPrint *buf);210211/**212* Test if the print buffer is complete (not truncated).213*214* It may have been truncated due to a memory allocation failure215* or the size_max limit (compare size and size_max if necessary).216*/217static inline int av_bprint_is_complete(const AVBPrint *buf)218{219return buf->len < buf->size;220}221222/**223* Finalize a print buffer.224*225* The print buffer can no longer be used afterwards,226* but the len and size fields are still valid.227*228* @arg[out] ret_str if not NULL, used to return a permanent copy of the229* buffer contents, or NULL if memory allocation fails;230* if NULL, the buffer is discarded and freed231* @return 0 for success or error code (probably AVERROR(ENOMEM))232*/233int av_bprint_finalize(AVBPrint *buf, char **ret_str);234235/**236* Escape the content in src and append it to dstbuf.237*238* @param dstbuf already inited destination bprint buffer239* @param src string containing the text to escape240* @param special_chars string containing the special characters which241* need to be escaped, can be NULL242* @param mode escape mode to employ, see AV_ESCAPE_MODE_* macros.243* Any unknown value for mode will be considered equivalent to244* AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without245* notice.246* @param flags flags which control how to escape, see AV_ESCAPE_FLAG_* macros247*/248void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,249enum AVEscapeMode mode, int flags);250251/** @} */252253#endif /* AVUTIL_BPRINT_H */254255256