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