strbuf.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* strbuf - String buffer routines
  2. *
  3. * Copyright (c) 2010-2012 Mark Pulford <mark@kyne.com.au>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. /* Size: Total bytes allocated to *buf
  27. * Length: String length, excluding optional NULL terminator.
  28. * Increment: Allocation increments when resizing the string buffer.
  29. * Dynamic: True if created via strbuf_new()
  30. */
  31. typedef struct {
  32. char *buf;
  33. int size;
  34. int length;
  35. int increment;
  36. int dynamic;
  37. int reallocs;
  38. int debug;
  39. } strbuf_t;
  40. #ifndef STRBUF_DEFAULT_SIZE
  41. #define STRBUF_DEFAULT_SIZE 1023
  42. #endif
  43. #ifndef STRBUF_DEFAULT_INCREMENT
  44. #define STRBUF_DEFAULT_INCREMENT -2
  45. #endif
  46. /* Initialise */
  47. extern strbuf_t *strbuf_new(int len);
  48. extern void strbuf_init(strbuf_t *s, int len);
  49. extern void strbuf_set_increment(strbuf_t *s, int increment);
  50. /* Release */
  51. extern void strbuf_free(strbuf_t *s);
  52. extern char *strbuf_free_to_string(strbuf_t *s, int *len);
  53. /* Management */
  54. extern void strbuf_resize(strbuf_t *s, int len);
  55. static int strbuf_empty_length(strbuf_t *s);
  56. static int strbuf_length(strbuf_t *s);
  57. static char *strbuf_string(strbuf_t *s, int *len);
  58. static void strbuf_ensure_empty_length(strbuf_t *s, int len);
  59. static char *strbuf_empty_ptr(strbuf_t *s);
  60. static void strbuf_extend_length(strbuf_t *s, int len);
  61. /* Update */
  62. extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...);
  63. extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...);
  64. static void strbuf_append_mem(strbuf_t *s, const char *c, int len);
  65. extern void strbuf_append_string(strbuf_t *s, const char *str);
  66. static void strbuf_append_char(strbuf_t *s, const char c);
  67. static void strbuf_ensure_null(strbuf_t *s);
  68. /* Reset string for before use */
  69. static inline void strbuf_reset(strbuf_t *s)
  70. {
  71. s->length = 0;
  72. }
  73. static inline int strbuf_allocated(strbuf_t *s)
  74. {
  75. return s->buf != NULL;
  76. }
  77. /* Return bytes remaining in the string buffer
  78. * Ensure there is space for a NULL terminator. */
  79. static inline int strbuf_empty_length(strbuf_t *s)
  80. {
  81. return s->size - s->length - 1;
  82. }
  83. static inline void strbuf_ensure_empty_length(strbuf_t *s, int len)
  84. {
  85. if (len > strbuf_empty_length(s))
  86. strbuf_resize(s, s->length + len);
  87. }
  88. static inline char *strbuf_empty_ptr(strbuf_t *s)
  89. {
  90. return s->buf + s->length;
  91. }
  92. static inline void strbuf_extend_length(strbuf_t *s, int len)
  93. {
  94. s->length += len;
  95. }
  96. static inline int strbuf_length(strbuf_t *s)
  97. {
  98. return s->length;
  99. }
  100. static inline void strbuf_append_char(strbuf_t *s, const char c)
  101. {
  102. strbuf_ensure_empty_length(s, 1);
  103. s->buf[s->length++] = c;
  104. }
  105. static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
  106. {
  107. s->buf[s->length++] = c;
  108. }
  109. static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len)
  110. {
  111. strbuf_ensure_empty_length(s, len);
  112. memcpy(s->buf + s->length, c, len);
  113. s->length += len;
  114. }
  115. static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len)
  116. {
  117. memcpy(s->buf + s->length, c, len);
  118. s->length += len;
  119. }
  120. static inline void strbuf_ensure_null(strbuf_t *s)
  121. {
  122. s->buf[s->length] = 0;
  123. }
  124. static inline char *strbuf_string(strbuf_t *s, int *len)
  125. {
  126. if (len)
  127. *len = s->length;
  128. return s->buf;
  129. }
  130. /* vi:ai et sw=4 ts=4:
  131. */