aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.h
blob: 0d5143e1925606467e0b3c52677d710dd5cf7d00 (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. /**
  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 gh_buf_try_grow(gh_buf *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 gh_buf_grow(gh_buf *buf, int target_size)
  39. {
  40. return gh_buf_try_grow(buf, target_size, true);
  41. }
  42. extern void gh_buf_free(gh_buf *buf);
  43. extern void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b);
  44. /**
  45. * Test if there have been any reallocation failures with this gh_buf.
  46. *
  47. * Any function that writes to a gh_buf can fail due to memory allocation
  48. * issues. If one fails, the gh_buf will be marked with an OOM error and
  49. * further calls to modify the buffer will fail. Check gh_buf_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 gh_buf_oom(const gh_buf *buf)
  56. {
  57. return (buf->ptr == gh_buf__oom);
  58. }
  59. static inline size_t gh_buf_len(const gh_buf *buf)
  60. {
  61. return buf->size;
  62. }
  63. extern int gh_buf_cmp(const gh_buf *a, const gh_buf *b);
  64. extern void gh_buf_attach(gh_buf *buf, unsigned char *ptr, int asize);
  65. extern unsigned char *gh_buf_detach(gh_buf *buf);
  66. extern void gh_buf_copy_cstr(char *data, int datasize, const gh_buf *buf);
  67. static inline const char *gh_buf_cstr(const gh_buf *buf)
  68. {
  69. return (char *)buf->ptr;
  70. }
  71. #define gh_buf_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 gh_buf 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. * gh_buf_oom at the end.
  79. */
  80. extern int gh_buf_set(gh_buf *buf, const unsigned char *data, int len);
  81. extern int gh_buf_sets(gh_buf *buf, const char *string);
  82. extern int gh_buf_putc(gh_buf *buf, int c);
  83. extern int gh_buf_put(gh_buf *buf, const unsigned char *data, int len);
  84. extern int gh_buf_puts(gh_buf *buf, const char *string);
  85. extern int gh_buf_printf(gh_buf *buf, const char *format, ...)
  86. __attribute__((format (printf, 2, 3)));
  87. extern int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap);
  88. extern void gh_buf_clear(gh_buf *buf);
  89. int gh_buf_strchr(const gh_buf *buf, int c, int pos);
  90. int gh_buf_strrchr(const gh_buf *buf, int c, int pos);
  91. void gh_buf_drop(gh_buf *buf, int n);
  92. void gh_buf_truncate(gh_buf *buf, int len);
  93. void gh_buf_trim(gh_buf *buf);
  94. #endif