2
0

bprint.h 7.6 KB

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