strbuf.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* strbuf - string buffer routines
  2. *
  3. * Copyright (c) 2010-2011 Mark Pulford <[email protected]>
  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 <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #include "strbuf.h"
  29. void die(const char *fmt, ...)
  30. {
  31. va_list arg;
  32. va_start(arg, fmt);
  33. vfprintf(stderr, fmt, arg);
  34. va_end(arg);
  35. fprintf(stderr, "\n");
  36. exit(-1);
  37. }
  38. void strbuf_init(strbuf_t *s, int len)
  39. {
  40. int size;
  41. if (len <= 0)
  42. size = STRBUF_DEFAULT_SIZE;
  43. else
  44. size = len + 1; /* \0 terminator */
  45. s->buf = NULL;
  46. s->size = size;
  47. s->length = 0;
  48. s->increment = STRBUF_DEFAULT_INCREMENT;
  49. s->dynamic = 0;
  50. s->reallocs = 0;
  51. s->debug = 0;
  52. s->buf = malloc(size);
  53. if (!s->buf)
  54. die("Out of memory");
  55. strbuf_ensure_null(s);
  56. }
  57. strbuf_t *strbuf_new(int len)
  58. {
  59. strbuf_t *s;
  60. s = malloc(sizeof(strbuf_t));
  61. if (!s)
  62. die("Out of memory");
  63. strbuf_init(s, len);
  64. /* Dynamic strbuf allocation / deallocation */
  65. s->dynamic = 1;
  66. return s;
  67. }
  68. void strbuf_set_increment(strbuf_t *s, int increment)
  69. {
  70. /* Increment > 0: Linear buffer growth rate
  71. * Increment < -1: Exponential buffer growth rate */
  72. if (increment == 0 || increment == -1)
  73. die("BUG: Invalid string increment");
  74. s->increment = increment;
  75. }
  76. static inline void debug_stats(strbuf_t *s)
  77. {
  78. if (s->debug) {
  79. fprintf(stderr, "strbuf(%lx) reallocs: %d, length: %d, size: %d\n",
  80. (long)s, s->reallocs, s->length, s->size);
  81. }
  82. }
  83. void strbuf_free(strbuf_t *s)
  84. {
  85. debug_stats(s);
  86. if (s->buf)
  87. free(s->buf);
  88. if (s->dynamic)
  89. free(s);
  90. }
  91. char *strbuf_free_to_string(strbuf_t *s, int *len)
  92. {
  93. char *buf;
  94. debug_stats(s);
  95. strbuf_ensure_null(s);
  96. buf = s->buf;
  97. if (len)
  98. *len = s->length;
  99. if (s->dynamic)
  100. free(s);
  101. return buf;
  102. }
  103. static int calculate_new_size(strbuf_t *s, int len)
  104. {
  105. int reqsize, newsize;
  106. if (len <= 0)
  107. die("BUG: Invalid strbuf length requested");
  108. /* Ensure there is room for optional NULL termination */
  109. reqsize = len + 1;
  110. /* If the user has requested to shrink the buffer, do it exactly */
  111. if (s->size > reqsize)
  112. return reqsize;
  113. newsize = s->size;
  114. if (s->increment < 0) {
  115. /* Exponential sizing */
  116. while (newsize < reqsize)
  117. newsize *= -s->increment;
  118. } else {
  119. /* Linear sizing */
  120. newsize = ((newsize + s->increment - 1) / s->increment) * s->increment;
  121. }
  122. return newsize;
  123. }
  124. /* Ensure strbuf can handle a string length bytes long (ignoring NULL
  125. * optional termination). */
  126. void strbuf_resize(strbuf_t *s, int len)
  127. {
  128. int newsize;
  129. newsize = calculate_new_size(s, len);
  130. if (s->debug > 1) {
  131. fprintf(stderr, "strbuf(%lx) resize: %d => %d\n",
  132. (long)s, s->size, newsize);
  133. }
  134. s->size = newsize;
  135. s->buf = realloc(s->buf, s->size);
  136. if (!s->buf)
  137. die("Out of memory");
  138. s->reallocs++;
  139. }
  140. void strbuf_append_string(strbuf_t *s, const char *str)
  141. {
  142. int space, i;
  143. space = strbuf_empty_length(s);
  144. for (i = 0; str[i]; i++) {
  145. if (space < 1) {
  146. strbuf_resize(s, s->length + 1);
  147. space = strbuf_empty_length(s);
  148. }
  149. s->buf[s->length] = str[i];
  150. s->length++;
  151. space--;
  152. }
  153. }
  154. void strbuf_append_number(strbuf_t *s, double number)
  155. {
  156. int len;
  157. /* Lowest double printed with %.14g is 21 characters long:
  158. * -1.7976931348623e+308
  159. *
  160. * Use 32 to include the \0, and a few extra just in case..
  161. */
  162. strbuf_ensure_empty_length(s, 32);
  163. len = sprintf(s->buf + s->length, "%.14g", number);
  164. if (len < 0)
  165. die("BUG: Unable to convert number"); /* This should never happen.. */
  166. s->length += len;
  167. }
  168. void strbuf_append_fmt(strbuf_t *s, const char *fmt, ...)
  169. {
  170. va_list arg;
  171. int fmt_len, try;
  172. int empty_len;
  173. /* If the first attempt to append fails, resize the buffer appropriately
  174. * and try again */
  175. for (try = 0; ; try++) {
  176. va_start(arg, fmt);
  177. /* Append the new formatted string */
  178. /* fmt_len is the length of the string required, excluding the
  179. * trailing NULL */
  180. empty_len = strbuf_empty_length(s);
  181. /* Add 1 since there is also space to store the terminating NULL. */
  182. fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg);
  183. va_end(arg);
  184. if (fmt_len <= empty_len)
  185. break; /* SUCCESS */
  186. if (try > 0)
  187. die("BUG: length of formatted string changed");
  188. strbuf_resize(s, s->length + fmt_len);
  189. }
  190. s->length += fmt_len;
  191. }
  192. /* vi:ai et sw=4 ts=4:
  193. */