aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.h
blob: 63d620207c06a9fdb753423121e7a38ed51e6d3d (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. } strbuf;
  11. extern unsigned char strbuf__initbuf[];
  12. extern unsigned char strbuf__oom[];
  13. #define GH_BUF_INIT { 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 strbuf_init(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 strbuf_try_grow(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 strbuf_grow(strbuf *buf, int target_size)
  39. {
  40. return strbuf_try_grow(buf, target_size, true);
  41. }
  42. extern void strbuf_free(strbuf *buf);
  43. extern void strbuf_swap(strbuf *buf_a, 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 strbuf_oom(const strbuf *buf)
  56. {
  57. return (buf->ptr == strbuf__oom);
  58. }
  59. static inline size_t strbuf_len(const strbuf *buf)
  60. {
  61. return buf->size;
  62. }
  63. extern int strbuf_cmp(const strbuf *a, const strbuf *b);
  64. extern void strbuf_attach(strbuf *buf, unsigned char *ptr, int asize);
  65. extern unsigned char *strbuf_detach(strbuf *buf);
  66. extern void strbuf_copy_cstr(char *data, int datasize, const strbuf *buf);
  67. static inline const char *strbuf_cstr(const strbuf *buf)
  68. {
  69. return (char *)buf->ptr;
  70. }
  71. #define 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 strbuf_set(strbuf *buf, const unsigned char *data, int len);
  81. extern int strbuf_sets(strbuf *buf, const char *string);
  82. extern int strbuf_putc(strbuf *buf, int c);
  83. extern int strbuf_put(strbuf *buf, const unsigned char *data, int len);
  84. extern int strbuf_puts(strbuf *buf, const char *string);
  85. extern int strbuf_printf(strbuf *buf, const char *format, ...)
  86. __attribute__((format (printf, 2, 3)));
  87. extern int strbuf_vprintf(strbuf *buf, const char *format, va_list ap);
  88. extern void strbuf_clear(strbuf *buf);
  89. int strbuf_strchr(const strbuf *buf, int c, int pos);
  90. int strbuf_strrchr(const strbuf *buf, int c, int pos);
  91. void strbuf_drop(strbuf *buf, int n);
  92. void strbuf_truncate(strbuf *buf, int len);
  93. void strbuf_rtrim(strbuf *buf);
  94. void strbuf_trim(strbuf *buf);
  95. void strbuf_normalize_whitespace(strbuf *s);
  96. void strbuf_unescape(strbuf *s);
  97. #endif