You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.3KB

  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. /**
  24. * Define a structure with extra padding to a fixed size
  25. * This helps ensuring binary compatibility with future versions.
  26. */
  27. #define FF_PAD_STRUCTURE(size, ...) \
  28. __VA_ARGS__ \
  29. char reserved_padding[size - sizeof(struct { __VA_ARGS__ })];
  30. /**
  31. * Buffer to print data progressively
  32. *
  33. * The string buffer grows as necessary and is always 0-terminated.
  34. * The content of the string is never accessed, and thus is
  35. * encoding-agnostic and can even hold binary data.
  36. *
  37. * Small buffers are kept in the structure itself, and thus require no
  38. * memory allocation at all (unless the contents of the buffer is needed
  39. * after the structure goes out of scope). This is almost as lightweight as
  40. * declaring a local "char buf[512]".
  41. *
  42. * The length of the string can go beyond the allocated size: the buffer is
  43. * then truncated, but the functions still keep account of the actual total
  44. * length.
  45. *
  46. * In other words, buf->len can be greater than buf->size and records the
  47. * total length of what would have been to the buffer if there had been
  48. * enough memory.
  49. *
  50. * Append operations do not need to be tested for failure: if a memory
  51. * allocation fails, data stop being appended to the buffer, but the length
  52. * is still updated. This situation can be tested with
  53. * av_bprint_is_complete().
  54. *
  55. * The size_max field determines several possible behaviours:
  56. *
  57. * size_max = -1 (= UINT_MAX) or any large value will let the buffer be
  58. * reallocated as necessary, with an amortized linear cost.
  59. *
  60. * size_max = 0 prevents writing anything to the buffer: only the total
  61. * length is computed. The write operations can then possibly be repeated in
  62. * a buffer with exactly the necessary size
  63. * (using size_init = size_max = len + 1).
  64. *
  65. * size_max = 1 is automatically replaced by the exact size available in the
  66. * structure itself, thus ensuring no dynamic memory allocation. The
  67. * internal buffer is large enough to hold a reasonable paragraph of text,
  68. * such as the current paragraph.
  69. */
  70. typedef struct AVBPrint {
  71. FF_PAD_STRUCTURE(1024,
  72. char *str; /** string so far */
  73. unsigned len; /** length so far */
  74. unsigned size; /** allocated memory */
  75. unsigned size_max; /** maximum allocated memory */
  76. char reserved_internal_buffer[1];
  77. )
  78. } AVBPrint;
  79. /**
  80. * Convenience macros for special values for av_bprint_init() size_max
  81. * parameter.
  82. */
  83. #define AV_BPRINT_SIZE_UNLIMITED ((unsigned)-1)
  84. #define AV_BPRINT_SIZE_AUTOMATIC 1
  85. #define AV_BPRINT_SIZE_COUNT_ONLY 0
  86. /**
  87. * Init a print buffer.
  88. *
  89. * @param buf buffer to init
  90. * @param size_init initial size (including the final 0)
  91. * @param size_max maximum size;
  92. * any large value means that the internal buffer will be
  93. * reallocated as needed up to that limit; -1 is converted to
  94. * UINT_MAX, the largest limit possible.
  95. * Check also AV_BPRINT_SIZE_* macros.
  96. */
  97. void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max);
  98. /**
  99. * Init a print buffer using a pre-existing buffer.
  100. *
  101. * The buffer will not be reallocated.
  102. *
  103. * @param buf buffer structure to init
  104. * @param buffer byte buffer to use for the string data
  105. * @param size size of buffer
  106. */
  107. void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size);
  108. /**
  109. * Append a formated string to a print buffer.
  110. */
  111. void av_bprintf(AVBPrint *buf, const char *fmt, ...) av_printf_format(2, 3);
  112. /**
  113. * Append char c n times to a print buffer.
  114. */
  115. void av_bprint_chars(AVBPrint *buf, char c, unsigned n);
  116. /**
  117. * Reset the string to "" but keep internal allocated data.
  118. */
  119. void av_bprint_clear(AVBPrint *buf);
  120. /**
  121. * Test if the print buffer is complete (not truncated).
  122. *
  123. * It may have been truncated due to a memory allocation failure
  124. * or the size_max limit (compare size and size_max if necessary).
  125. */
  126. static inline int av_bprint_is_complete(AVBPrint *buf)
  127. {
  128. return buf->len < buf->size;
  129. }
  130. /**
  131. * Finalize a print buffer.
  132. *
  133. * The print buffer can no longer be used afterwards,
  134. * but the len and size fields are still valid.
  135. *
  136. * @arg[out] ret_str if not NULL, used to return a permanent copy of the
  137. * buffer contents, or NULL if memory allocation fails;
  138. * if NULL, the buffer is discarded and freed
  139. * @return 0 for success or error code (probably AVERROR(ENOMEM))
  140. */
  141. int av_bprint_finalize(AVBPrint *buf, char **ret_str);
  142. #endif /* AVUTIL_BPRINT_H */