aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.h
blob: 40e3d11aaa553a4c00bbaec505722605c5290377 (plain)
  1. #ifndef INCLUDE_buffer_h__
  2. #define INCLUDE_buffer_h__
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include <stdarg.h>
  6. #include <sys/types.h>
  7. #include "cmark_export.h"
  8. typedef struct {
  9. unsigned char *ptr;
  10. int asize, size;
  11. } cmark_strbuf;
  12. CMARK_EXPORT
  13. extern unsigned char cmark_strbuf__initbuf[];
  14. CMARK_EXPORT
  15. extern unsigned char cmark_strbuf__oom[];
  16. #define CMARK_GH_BUF_INIT { cmark_strbuf__initbuf, 0, 0 }
  17. /**
  18. * Initialize a strbuf structure.
  19. *
  20. * For the cases where GH_BUF_INIT cannot be used to do static
  21. * initialization.
  22. */
  23. CMARK_EXPORT
  24. void cmark_strbuf_init(cmark_strbuf *buf, int initial_size);
  25. /**
  26. * Attempt to grow the buffer to hold at least `target_size` bytes.
  27. *
  28. * If the allocation fails, this will return an error. If mark_oom is true,
  29. * this will mark the buffer as invalid for future operations; if false,
  30. * existing buffer content will be preserved, but calling code must handle
  31. * that buffer was not expanded.
  32. */
  33. CMARK_EXPORT
  34. int cmark_strbuf_try_grow(cmark_strbuf *buf, int target_size, bool mark_oom);
  35. /**
  36. * Grow the buffer to hold at least `target_size` bytes.
  37. *
  38. * If the allocation fails, this will return an error and the buffer will be
  39. * marked as invalid for future operations, invaliding contents.
  40. *
  41. * @return 0 on success or -1 on failure
  42. */
  43. CMARK_EXPORT
  44. inline int cmark_strbuf_grow(cmark_strbuf *buf, int target_size)
  45. {
  46. return cmark_strbuf_try_grow(buf, target_size, true);
  47. }
  48. CMARK_EXPORT
  49. void cmark_strbuf_free(cmark_strbuf *buf);
  50. CMARK_EXPORT
  51. void cmark_strbuf_swap(cmark_strbuf *buf_a, cmark_strbuf *buf_b);
  52. /**
  53. * Test if there have been any reallocation failures with this strbuf.
  54. *
  55. * Any function that writes to a strbuf can fail due to memory allocation
  56. * issues. If one fails, the strbuf will be marked with an OOM error and
  57. * further calls to modify the buffer will fail. Check strbuf_oom() at the
  58. * end of your sequence and it will be true if you ran out of memory at any
  59. * point with that buffer.
  60. *
  61. * @return false if no error, true if allocation error
  62. */
  63. CMARK_EXPORT
  64. inline bool cmark_strbuf_oom(const cmark_strbuf *buf)
  65. {
  66. return (buf->ptr == cmark_strbuf__oom);
  67. }
  68. CMARK_EXPORT
  69. inline size_t cmark_strbuf_len(const cmark_strbuf *buf)
  70. {
  71. return buf->size;
  72. }
  73. CMARK_EXPORT
  74. int cmark_strbuf_cmp(const cmark_strbuf *a, const cmark_strbuf *b);
  75. CMARK_EXPORT
  76. void cmark_strbuf_attach(cmark_strbuf *buf, unsigned char *ptr, int asize);
  77. CMARK_EXPORT
  78. unsigned char *cmark_strbuf_detach(cmark_strbuf *buf);
  79. CMARK_EXPORT
  80. void cmark_strbuf_copy_cstr(char *data, int datasize, const cmark_strbuf *buf);
  81. static inline const char *cmark_strbuf_cstr(const cmark_strbuf *buf)
  82. {
  83. return (char *)buf->ptr;
  84. }
  85. #define cmark_strbuf_at(buf, n) ((buf)->ptr[n])
  86. /*
  87. * Functions below that return int value error codes will return 0 on
  88. * success or -1 on failure (which generally means an allocation failed).
  89. * Using a strbuf where the allocation has failed with result in -1 from
  90. * all further calls using that buffer. As a result, you can ignore the
  91. * return code of these functions and call them in a series then just call
  92. * strbuf_oom at the end.
  93. */
  94. CMARK_EXPORT
  95. int cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data, int len);
  96. CMARK_EXPORT
  97. int cmark_strbuf_sets(cmark_strbuf *buf, const char *string);
  98. CMARK_EXPORT
  99. int cmark_strbuf_putc(cmark_strbuf *buf, int c);
  100. CMARK_EXPORT
  101. int cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data, int len);
  102. CMARK_EXPORT
  103. int cmark_strbuf_puts(cmark_strbuf *buf, const char *string);
  104. CMARK_EXPORT
  105. int cmark_strbuf_printf(cmark_strbuf *buf, const char *format, ...)
  106. __attribute__((format (printf, 2, 3)));
  107. CMARK_EXPORT
  108. int cmark_strbuf_vprintf(cmark_strbuf *buf, const char *format, va_list ap);
  109. CMARK_EXPORT
  110. void cmark_strbuf_clear(cmark_strbuf *buf);
  111. CMARK_EXPORT
  112. int cmark_strbuf_strchr(const cmark_strbuf *buf, int c, int pos);
  113. CMARK_EXPORT
  114. int cmark_strbuf_strrchr(const cmark_strbuf *buf, int c, int pos);
  115. CMARK_EXPORT
  116. void cmark_strbuf_drop(cmark_strbuf *buf, int n);
  117. CMARK_EXPORT
  118. void cmark_strbuf_truncate(cmark_strbuf *buf, int len);
  119. CMARK_EXPORT
  120. void cmark_strbuf_rtrim(cmark_strbuf *buf);
  121. CMARK_EXPORT
  122. void cmark_strbuf_trim(cmark_strbuf *buf);
  123. CMARK_EXPORT
  124. void cmark_strbuf_normalize_whitespace(cmark_strbuf *s);
  125. CMARK_EXPORT
  126. void cmark_strbuf_unescape(cmark_strbuf *s);
  127. #ifndef CMARK_NO_SHORT_NAMES
  128. #define strbuf cmark_strbuf
  129. #define strbuf__initbuf cmark_strbuf__initbuf
  130. #define strbuf__oom cmark_strbuf__oom
  131. #define GH_BUF_INIT CMARK_GH_BUF_INIT
  132. #define strbuf_init cmark_strbuf_init
  133. #define strbuf_try_grow cmark_strbuf_try_grow
  134. #define strbuf_grow cmark_strbuf_grow
  135. #define strbuf_free cmark_strbuf_free
  136. #define strbuf_swap cmark_strbuf_swap
  137. #define strbuf_oom cmark_strbuf_oom
  138. #define strbuf_len cmark_strbuf_len
  139. #define strbuf_cmp cmark_strbuf_cmp
  140. #define strbuf_attach cmark_strbuf_attach
  141. #define strbuf_detach cmark_strbuf_detach
  142. #define strbuf_copy_cstr cmark_strbuf_copy_cstr
  143. #define strbuf_cstr cmark_strbuf_cstr
  144. #define strbuf_at cmark_strbuf_at
  145. #define strbuf_set cmark_strbuf_set
  146. #define strbuf_sets cmark_strbuf_sets
  147. #define strbuf_putc cmark_strbuf_putc
  148. #define strbuf_put cmark_strbuf_put
  149. #define strbuf_puts cmark_strbuf_puts
  150. #define strbuf_printf cmark_strbuf_printf
  151. #define strbuf_vprintf cmark_strbuf_vprintf
  152. #define strbuf_clear cmark_strbuf_clear
  153. #define strbuf_strchr cmark_strbuf_strchr
  154. #define strbuf_strrchr cmark_strbuf_strrchr
  155. #define strbuf_drop cmark_strbuf_drop
  156. #define strbuf_truncate cmark_strbuf_truncate
  157. #define strbuf_rtrim cmark_strbuf_rtrim
  158. #define strbuf_trim cmark_strbuf_trim
  159. #define strbuf_normalize_whitespace cmark_strbuf_normalize_whitespace
  160. #define strbuf_unescape cmark_strbuf_unescape
  161. #endif
  162. #endif