aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.h
blob: c827ea8ab1adf4ca391b2174d4196598d95898ff (plain)
  1. #ifndef CMARK_BUFFER_H
  2. #define CMARK_BUFFER_H
  3. #include <stddef.h>
  4. #include <stdarg.h>
  5. #include "config.h"
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. typedef struct {
  10. unsigned char *ptr;
  11. int asize, size;
  12. } strbuf;
  13. extern unsigned char strbuf__initbuf[];
  14. extern unsigned char strbuf__oom[];
  15. #define GH_BUF_INIT { 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. void strbuf_init(strbuf *buf, int initial_size);
  23. /**
  24. * Attempt to grow the buffer to hold at least `target_size` bytes.
  25. *
  26. * If the allocation fails, this will return an error. If mark_oom is true,
  27. * this will mark the buffer as invalid for future operations; if false,
  28. * existing buffer content will be preserved, but calling code must handle
  29. * that buffer was not expanded.
  30. */
  31. int strbuf_try_grow(strbuf *buf, int target_size, bool mark_oom);
  32. /**
  33. * Grow the buffer to hold at least `target_size` bytes.
  34. *
  35. * If the allocation fails, this will return an error and the buffer will be
  36. * marked as invalid for future operations, invaliding contents.
  37. *
  38. * @return 0 on success or -1 on failure
  39. */
  40. int strbuf_grow(strbuf *buf, int target_size);
  41. void strbuf_free(strbuf *buf);
  42. void strbuf_swap(strbuf *buf_a, strbuf *buf_b);
  43. /**
  44. * Test if there have been any reallocation failures with this strbuf.
  45. *
  46. * Any function that writes to a strbuf can fail due to memory allocation
  47. * issues. If one fails, the strbuf will be marked with an OOM error and
  48. * further calls to modify the buffer will fail. Check strbuf_oom() at the
  49. * end of your sequence and it will be true if you ran out of memory at any
  50. * point with that buffer.
  51. *
  52. * @return false if no error, true if allocation error
  53. */
  54. bool strbuf_oom(const strbuf *buf);
  55. size_t strbuf_len(const strbuf *buf);
  56. int strbuf_cmp(const strbuf *a, const strbuf *b);
  57. void strbuf_attach(strbuf *buf, unsigned char *ptr, int asize);
  58. unsigned char *strbuf_detach(strbuf *buf);
  59. void strbuf_copy_cstr(char *data, int datasize, const strbuf *buf);
  60. static inline const char *strbuf_cstr(const strbuf *buf)
  61. {
  62. return (char *)buf->ptr;
  63. }
  64. #define strbuf_at(buf, n) ((buf)->ptr[n])
  65. /*
  66. * Functions below that return int value error codes will return 0 on
  67. * success or -1 on failure (which generally means an allocation failed).
  68. * Using a strbuf where the allocation has failed with result in -1 from
  69. * all further calls using that buffer. As a result, you can ignore the
  70. * return code of these functions and call them in a series then just call
  71. * strbuf_oom at the end.
  72. */
  73. int strbuf_set(strbuf *buf, const unsigned char *data, int len);
  74. int strbuf_sets(strbuf *buf, const char *string);
  75. int strbuf_putc(strbuf *buf, int c);
  76. int strbuf_put(strbuf *buf, const unsigned char *data, int len);
  77. int strbuf_puts(strbuf *buf, const char *string);
  78. int strbuf_printf(strbuf *buf, const char *format, ...)
  79. CMARK_ATTRIBUTE((format (printf, 2, 3)));
  80. int strbuf_vprintf(strbuf *buf, const char *format, va_list ap);
  81. void strbuf_clear(strbuf *buf);
  82. int strbuf_strchr(const strbuf *buf, int c, int pos);
  83. int strbuf_strrchr(const strbuf *buf, int c, int pos);
  84. void strbuf_drop(strbuf *buf, int n);
  85. void strbuf_truncate(strbuf *buf, int len);
  86. void strbuf_rtrim(strbuf *buf);
  87. void strbuf_trim(strbuf *buf);
  88. void strbuf_normalize_whitespace(strbuf *s);
  89. void strbuf_unescape(strbuf *s);
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif