annotate ffmpeg/libavutil/bprint.h @ 13:844d341cf643 tip

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