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