bprint.h 8.6 KB

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