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