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