strbuf.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #define inline
  27. /* Size: Total bytes allocated to *buf
  28. * Length: String length, excluding optional NULL terminator.
  29. * Increment: Allocation increments when resizing the string buffer.
  30. * Dynamic: True if created via strbuf_new()
  31. */
  32. typedef struct {
  33. char *buf;
  34. int size;
  35. int length;
  36. int increment;
  37. int dynamic;
  38. int is_err;
  39. // int reallocs;
  40. // int debug;
  41. } strbuf_t;
  42. #ifndef STRBUF_DEFAULT_SIZE
  43. #define STRBUF_DEFAULT_SIZE 1023
  44. #endif
  45. #ifndef STRBUF_DEFAULT_INCREMENT
  46. #define STRBUF_DEFAULT_INCREMENT -2
  47. #endif
  48. /* Initialise */
  49. extern strbuf_t *strbuf_new(int len);
  50. extern int strbuf_init(strbuf_t *s, int len);
  51. extern void strbuf_set_increment(strbuf_t *s, int increment);
  52. /* Release */
  53. extern void strbuf_free(strbuf_t *s);
  54. extern char *strbuf_free_to_string(strbuf_t *s, int *len);
  55. /* Management */
  56. extern void strbuf_resize(strbuf_t *s, int len);
  57. static int strbuf_empty_length(strbuf_t *s);
  58. static int strbuf_length(strbuf_t *s);
  59. static char *strbuf_string(strbuf_t *s, int *len);
  60. static void strbuf_ensure_empty_length(strbuf_t *s, int len);
  61. static char *strbuf_empty_ptr(strbuf_t *s);
  62. static void strbuf_extend_length(strbuf_t *s, int len);
  63. /* Update */
  64. extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...);
  65. extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...);
  66. static void strbuf_append_mem(strbuf_t *s, const char *c, int len);
  67. extern void strbuf_append_string(strbuf_t *s, const char *str);
  68. static void strbuf_append_char(strbuf_t *s, const char c);
  69. static void strbuf_ensure_null(strbuf_t *s);
  70. /* Reset string for before use */
  71. static inline void strbuf_reset(strbuf_t *s)
  72. {
  73. s->length = 0;
  74. }
  75. static inline int strbuf_allocated(strbuf_t *s)
  76. {
  77. return s->buf != NULL;
  78. }
  79. /* Return bytes remaining in the string buffer
  80. * Ensure there is space for a NULL terminator. */
  81. static inline int strbuf_empty_length(strbuf_t *s)
  82. {
  83. return s->size - s->length - 1;
  84. }
  85. static inline void strbuf_ensure_empty_length(strbuf_t *s, int len)
  86. {
  87. if (len > strbuf_empty_length(s))
  88. strbuf_resize(s, s->length + len);
  89. }
  90. static inline char *strbuf_empty_ptr(strbuf_t *s)
  91. {
  92. return s->buf + s->length;
  93. }
  94. static inline void strbuf_extend_length(strbuf_t *s, int len)
  95. {
  96. s->length += len;
  97. }
  98. #if 0
  99. static inline int strbuf_length(strbuf_t *s)
  100. {
  101. return s->length;
  102. }
  103. #endif
  104. static inline void strbuf_append_char(strbuf_t *s, const char c)
  105. {
  106. strbuf_ensure_empty_length(s, 1);
  107. if (s->is_err) return;
  108. s->buf[s->length++] = c;
  109. }
  110. static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
  111. {
  112. if (s->is_err) return;
  113. s->buf[s->length++] = c;
  114. }
  115. static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len)
  116. {
  117. strbuf_ensure_empty_length(s, len);
  118. if (s->is_err) return;
  119. memcpy(s->buf + s->length, c, len);
  120. s->length += len;
  121. }
  122. static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len)
  123. {
  124. if (s->is_err) return;
  125. memcpy(s->buf + s->length, c, len);
  126. s->length += len;
  127. }
  128. static inline void strbuf_ensure_null(strbuf_t *s)
  129. {
  130. if (s->is_err) return;
  131. s->buf[s->length] = 0;
  132. }
  133. static inline char *strbuf_string(strbuf_t *s, int *len)
  134. {
  135. if (len)
  136. *len = s->length;
  137. return s->buf;
  138. }
  139. /* vi:ai et sw=4 ts=4:
  140. */