strbuf.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifdef _MSC_VER
  69. #define snprintf _snprintf
  70. #undef inline
  71. #define inline __inline
  72. #endif
  73. /* Reset string for before use */
  74. static inline void strbuf_reset(strbuf_t *s)
  75. {
  76. s->length = 0;
  77. }
  78. static inline int strbuf_allocated(strbuf_t *s)
  79. {
  80. return s->buf != NULL;
  81. }
  82. /* Return bytes remaining in the string buffer
  83. * Ensure there is space for a NULL terminator. */
  84. static inline int strbuf_empty_length(strbuf_t *s)
  85. {
  86. return s->size - s->length - 1;
  87. }
  88. static inline void strbuf_ensure_empty_length(strbuf_t *s, int len)
  89. {
  90. if (len > strbuf_empty_length(s))
  91. strbuf_resize(s, s->length + len);
  92. }
  93. static inline char *strbuf_empty_ptr(strbuf_t *s)
  94. {
  95. return s->buf + s->length;
  96. }
  97. static inline void strbuf_extend_length(strbuf_t *s, int len)
  98. {
  99. s->length += len;
  100. }
  101. static inline int strbuf_length(strbuf_t *s)
  102. {
  103. return s->length;
  104. }
  105. static inline void strbuf_append_char(strbuf_t *s, const char c)
  106. {
  107. strbuf_ensure_empty_length(s, 1);
  108. s->buf[s->length++] = c;
  109. }
  110. static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
  111. {
  112. s->buf[s->length++] = c;
  113. }
  114. static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len)
  115. {
  116. strbuf_ensure_empty_length(s, len);
  117. memcpy(s->buf + s->length, c, len);
  118. s->length += len;
  119. }
  120. static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len)
  121. {
  122. memcpy(s->buf + s->length, c, len);
  123. s->length += len;
  124. }
  125. static inline void strbuf_ensure_null(strbuf_t *s)
  126. {
  127. s->buf[s->length] = 0;
  128. }
  129. static inline char *strbuf_string(strbuf_t *s, int *len)
  130. {
  131. if (len)
  132. *len = s->length;
  133. return s->buf;
  134. }
  135. /* vi:ai et sw=4 ts=4:
  136. */