aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.h
blob: 2581ee3afb1128457f1b9d23b3470089465b9c4d (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. } gh_buf;
  11. extern unsigned char gh_buf__initbuf[];
  12. extern unsigned char gh_buf__oom[];
  13. #define GH_BUF_INIT { gh_buf__initbuf, 0, 0 }
  14. /**
  15. * Initialize a gh_buf structure.
  16. *
  17. * For the cases where GH_BUF_INIT cannot be used to do static
  18. * initialization.
  19. */
  20. extern void gh_buf_init(gh_buf *buf, int initial_size);
  21. static inline void gh_buf_static(gh_buf *buf, unsigned char *source)
  22. {
  23. buf->ptr = source;
  24. buf->size = strlen(source);
  25. buf->asize = -1;
  26. }
  27. /**
  28. * Attempt to grow the buffer to hold at least `target_size` bytes.
  29. *
  30. * If the allocation fails, this will return an error. If mark_oom is true,
  31. * this will mark the buffer as invalid for future operations; if false,
  32. * existing buffer content will be preserved, but calling code must handle
  33. * that buffer was not expanded.
  34. */
  35. extern int gh_buf_try_grow(gh_buf *buf, int target_size, bool mark_oom);
  36. /**
  37. * Grow the buffer to hold at least `target_size` bytes.
  38. *
  39. * If the allocation fails, this will return an error and the buffer will be
  40. * marked as invalid for future operations, invaliding contents.
  41. *
  42. * @return 0 on success or -1 on failure
  43. */
  44. static inline int gh_buf_grow(gh_buf *buf, int target_size)
  45. {
  46. return gh_buf_try_grow(buf, target_size, true);
  47. }
  48. extern void gh_buf_free(gh_buf *buf);
  49. extern void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b);
  50. /**
  51. * Test if there have been any reallocation failures with this gh_buf.
  52. *
  53. * Any function that writes to a gh_buf can fail due to memory allocation
  54. * issues. If one fails, the gh_buf will be marked with an OOM error and
  55. * further calls to modify the buffer will fail. Check gh_buf_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 gh_buf_oom(const gh_buf *buf)
  62. {
  63. return (buf->ptr == gh_buf__oom);
  64. }
  65. static inline size_t gh_buf_len(const gh_buf *buf)
  66. {
  67. return buf->size;
  68. }
  69. extern int gh_buf_cmp(const gh_buf *a, const gh_buf *b);
  70. extern void gh_buf_attach(gh_buf *buf, char *ptr, int asize);
  71. extern char *gh_buf_detach(gh_buf *buf);
  72. extern void gh_buf_copy_cstr(char *data, int datasize, const gh_buf *buf);
  73. static inline const char *gh_buf_cstr(const gh_buf *buf)
  74. {
  75. return buf->ptr;
  76. }
  77. #define gh_buf_at(buf, n) ((buf)->ptr[n])
  78. /*
  79. * Functions below that return int value error codes will return 0 on
  80. * success or -1 on failure (which generally means an allocation failed).
  81. * Using a gh_buf where the allocation has failed with result in -1 from
  82. * all further calls using that buffer. As a result, you can ignore the
  83. * return code of these functions and call them in a series then just call
  84. * gh_buf_oom at the end.
  85. */
  86. extern int gh_buf_set(gh_buf *buf, const char *data, int len);
  87. extern int gh_buf_sets(gh_buf *buf, const char *string);
  88. extern int gh_buf_putc(gh_buf *buf, char c);
  89. extern int gh_buf_put(gh_buf *buf, const char *data, int len);
  90. extern int gh_buf_puts(gh_buf *buf, const char *string);
  91. extern int gh_buf_printf(gh_buf *buf, const char *format, ...)
  92. __attribute__((format (printf, 2, 3)));
  93. extern int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap);
  94. extern void gh_buf_clear(gh_buf *buf);
  95. int gh_buf_strchr(const gh_buf *buf, int c, int pos);
  96. int gh_buf_strrchr(const gh_buf *buf, int c, int pos);
  97. void gh_buf_truncate(gh_buf *buf, int len);
  98. void gh_buf_ltruncate(gh_buf *buf, int len);
  99. void gh_buf_trim(gh_buf *buf);
  100. #endif