strbuf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #include "strbuf.h"
  29. #include "luat_base.h"
  30. #include "luat_mem.h"
  31. #define LUAT_LOG_TAG "cjson"
  32. #include "luat_log.h"
  33. #ifdef WIN32
  34. #define vsnprintf _vsnprintf
  35. #endif
  36. #define L_MALLOC luat_heap_malloc
  37. #define L_FREE luat_heap_free
  38. #define L_REALLOC luat_heap_realloc
  39. int strbuf_init(strbuf_t *s, int len)
  40. {
  41. int size;
  42. if (len <= 0)
  43. size = STRBUF_DEFAULT_SIZE;
  44. else
  45. size = len + 1; /* \0 terminator */
  46. s->buf = NULL;
  47. s->size = size;
  48. s->length = 0;
  49. s->increment = STRBUF_DEFAULT_INCREMENT;
  50. s->dynamic = 0;
  51. s->is_err = 0;
  52. // s->reallocs = 0;
  53. // s->debug = 0;
  54. s->buf = (char *)L_MALLOC(size);
  55. if (!s->buf)
  56. return -1;
  57. strbuf_ensure_null(s);
  58. return 0;
  59. }
  60. strbuf_t *strbuf_new(int len)
  61. {
  62. strbuf_t *s;
  63. int ret = 0;
  64. s = (strbuf_t *)L_MALLOC(sizeof(strbuf_t));
  65. if (!s)
  66. return NULL;
  67. ret = strbuf_init(s, len);
  68. if (ret) {
  69. L_FREE(s);
  70. return NULL;
  71. }
  72. /* Dynamic strbuf allocation / deallocation */
  73. s->dynamic = 1;
  74. return s;
  75. }
  76. /* If strbuf_t has not been dynamically allocated, strbuf_free() can
  77. * be called any number of times strbuf_init() */
  78. void strbuf_free(strbuf_t *s)
  79. {
  80. // debug_stats(s);
  81. if (s->buf) {
  82. L_FREE (s->buf);
  83. s->buf = NULL;
  84. }
  85. if (s->dynamic)
  86. L_FREE (s);
  87. }
  88. static int calculate_new_size(strbuf_t *s, int len)
  89. {
  90. int reqsize;
  91. // if (len <= 0)
  92. // die("BUG: Invalid strbuf length requested");
  93. /* Ensure there is room for optional NULL termination */
  94. reqsize = len + 1;
  95. /* If the user has requested to shrink the buffer, do it exactly */
  96. if (s->size > reqsize)
  97. return reqsize;
  98. // newsize = s->size;
  99. if (reqsize - s->size < 1023) {
  100. reqsize = s->size + 1023;
  101. }
  102. return reqsize + 1;
  103. }
  104. /* Ensure strbuf can handle a string length bytes long (ignoring NULL
  105. * optional termination). */
  106. void strbuf_resize(strbuf_t *s, int len)
  107. {
  108. int newsize;
  109. void* ptr;
  110. if (s->is_err)
  111. return;
  112. newsize = calculate_new_size(s, len);
  113. ptr = (char *)L_REALLOC(s->buf, newsize);
  114. if (ptr == NULL) {
  115. s->is_err = 1;
  116. }
  117. else {
  118. s->size = newsize;
  119. s->buf = ptr;
  120. }
  121. }
  122. void strbuf_append_string(strbuf_t *s, const char *str)
  123. {
  124. int space, i;
  125. if (s->is_err)
  126. return;
  127. space = strbuf_empty_length(s);
  128. for (i = 0; str[i]; i++) {
  129. if (space < 1) {
  130. strbuf_resize(s, s->length + 1);
  131. space = strbuf_empty_length(s);
  132. }
  133. s->buf[s->length] = str[i];
  134. s->length++;
  135. space--;
  136. }
  137. }