bprint.h 7.3 KB

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