strbuf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "c_stdio.h"
  25. #include "c_stdlib.h"
  26. #include "c_stdarg.h"
  27. #include "c_string.h"
  28. #include "strbuf.h"
  29. #include "cjson_mem.h"
  30. int strbuf_init(strbuf_t *s, int len)
  31. {
  32. int size;
  33. if (len <= 0)
  34. size = STRBUF_DEFAULT_SIZE;
  35. else
  36. size = len + 1; /* \0 terminator */
  37. s->buf = NULL;
  38. s->size = size;
  39. s->length = 0;
  40. s->increment = STRBUF_DEFAULT_INCREMENT;
  41. s->dynamic = 0;
  42. s->reallocs = 0;
  43. s->debug = 0;
  44. s->buf = (char *)cjson_mem_malloc(size);
  45. if (!s->buf){
  46. NODE_ERR("not enough memory\n");
  47. return -1;
  48. }
  49. strbuf_ensure_null(s);
  50. return 0;
  51. }
  52. strbuf_t *strbuf_new(int len)
  53. {
  54. strbuf_t *s;
  55. s = (strbuf_t *)cjson_mem_malloc(sizeof(strbuf_t));
  56. if (!s){
  57. NODE_ERR("not enough memory\n");
  58. return NULL;
  59. }
  60. strbuf_init(s, len);
  61. /* Dynamic strbuf allocation / deallocation */
  62. s->dynamic = 1;
  63. return s;
  64. }
  65. int strbuf_set_increment(strbuf_t *s, int increment)
  66. {
  67. /* Increment > 0: Linear buffer growth rate
  68. * Increment < -1: Exponential buffer growth rate */
  69. if (increment == 0 || increment == -1){
  70. NODE_ERR("BUG: Invalid string increment");
  71. return -1;
  72. }
  73. s->increment = increment;
  74. return 0;
  75. }
  76. static inline void debug_stats(strbuf_t *s)
  77. {
  78. if (s->debug) {
  79. NODE_ERR("strbuf(%lx) reallocs: %d, length: %d, size: %d\n",
  80. (long)s, s->reallocs, s->length, s->size);
  81. }
  82. }
  83. /* If strbuf_t has not been dynamically allocated, strbuf_free() can
  84. * be called any number of times strbuf_init() */
  85. void strbuf_free(strbuf_t *s)
  86. {
  87. debug_stats(s);
  88. if (s->buf) {
  89. c_free(s->buf);
  90. s->buf = NULL;
  91. }
  92. if (s->dynamic)
  93. c_free(s);
  94. }
  95. char *strbuf_free_to_string(strbuf_t *s, int *len)
  96. {
  97. char *buf;
  98. debug_stats(s);
  99. strbuf_ensure_null(s);
  100. buf = s->buf;
  101. if (len)
  102. *len = s->length;
  103. if (s->dynamic)
  104. c_free(s);
  105. return buf;
  106. }
  107. static int calculate_new_size(strbuf_t *s, int len)
  108. {
  109. int reqsize, newsize;
  110. if (len <= 0){
  111. NODE_ERR("BUG: Invalid strbuf length requested");
  112. return 0;
  113. }
  114. /* Ensure there is room for optional NULL termination */
  115. reqsize = len + 1;
  116. /* If the user has requested to shrink the buffer, do it exactly */
  117. if (s->size > reqsize)
  118. return reqsize;
  119. newsize = s->size;
  120. if (s->increment < 0) {
  121. /* Exponential sizing */
  122. while (newsize < reqsize)
  123. newsize *= -s->increment;
  124. } else {
  125. /* Linear sizing */
  126. newsize = (((reqsize -1) / s->increment) + 1) * s->increment;
  127. }
  128. return newsize;
  129. }
  130. /* Ensure strbuf can handle a string length bytes long (ignoring NULL
  131. * optional termination). */
  132. int strbuf_resize(strbuf_t *s, int len)
  133. {
  134. int newsize;
  135. newsize = calculate_new_size(s, len);
  136. if (s->debug > 1) {
  137. NODE_ERR("strbuf(%lx) resize: %d => %d\n",
  138. (long)s, s->size, newsize);
  139. }
  140. s->buf = (char *)cjson_mem_realloc(s->buf, newsize);
  141. if (!s->buf){
  142. NODE_ERR("not enough memory");
  143. return -1;
  144. }
  145. s->size = newsize;
  146. s->reallocs++;
  147. return 0;
  148. }
  149. void strbuf_append_string(strbuf_t *s, const char *str)
  150. {
  151. int space, i;
  152. space = strbuf_empty_length(s);
  153. for (i = 0; str[i]; i++) {
  154. if (space < 1) {
  155. strbuf_resize(s, s->length + 1);
  156. space = strbuf_empty_length(s);
  157. }
  158. s->buf[s->length] = str[i];
  159. s->length++;
  160. space--;
  161. }
  162. }
  163. #if 0
  164. /* strbuf_append_fmt() should only be used when an upper bound
  165. * is known for the output string. */
  166. void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...)
  167. {
  168. va_list arg;
  169. int fmt_len;
  170. strbuf_ensure_empty_length(s, len);
  171. va_start(arg, fmt);
  172. fmt_len = vsnprintf(s->buf + s->length, len, fmt, arg);
  173. va_end(arg);
  174. if (fmt_len < 0)
  175. die("BUG: Unable to convert number"); /* This should never happen.. */
  176. s->length += fmt_len;
  177. }
  178. /* strbuf_append_fmt_retry() can be used when the there is no known
  179. * upper bound for the output string. */
  180. void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...)
  181. {
  182. va_list arg;
  183. int fmt_len, try;
  184. int empty_len;
  185. /* If the first attempt to append fails, resize the buffer appropriately
  186. * and try again */
  187. for (try = 0; ; try++) {
  188. va_start(arg, fmt);
  189. /* Append the new formatted string */
  190. /* fmt_len is the length of the string required, excluding the
  191. * trailing NULL */
  192. empty_len = strbuf_empty_length(s);
  193. /* Add 1 since there is also space to store the terminating NULL. */
  194. fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg);
  195. va_end(arg);
  196. if (fmt_len <= empty_len)
  197. break; /* SUCCESS */
  198. if (try > 0)
  199. die("BUG: length of formatted string changed");
  200. strbuf_resize(s, s->length + fmt_len);
  201. }
  202. s->length += fmt_len;
  203. }
  204. #endif
  205. /* vi:ai et sw=4 ts=4:
  206. */