You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

printbuf.c 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. *
  11. * Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
  12. * The copyrights to the contents of this file are licensed under the MIT License
  13. * (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. #include "config.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #ifdef HAVE_STDARG_H
  20. # include <stdarg.h>
  21. #else /* !HAVE_STDARG_H */
  22. # error Not enough var arg support!
  23. #endif /* HAVE_STDARG_H */
  24. #include "debug.h"
  25. #include "printbuf.h"
  26. #include "snprintf_compat.h"
  27. #include "vasprintf_compat.h"
  28. /* Default starting size of buffer and
  29. * sprintbuf buffer
  30. * */
  31. #define PRINTBUF_DEFAULT_SIZE (32)
  32. #define PRINTBUF_DEFAULT_SIZE_BUF (128)
  33. static int printbuf_extend(struct printbuf *p, int min_size);
  34. struct printbuf* printbuf_new(void)
  35. {
  36. struct printbuf *p;
  37. p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
  38. if(!p) return NULL;
  39. p->size = PRINTBUF_DEFAULT_SIZE;
  40. p->bpos = 0;
  41. p->buf = (char*)calloc(1, p->size);
  42. if(p->buf == NULL) {
  43. free(p);
  44. return NULL;
  45. }
  46. p->buf[0]= '\0';
  47. return p;
  48. }
  49. /**
  50. * Extend the buffer p so it has a size of at least min_size.
  51. *
  52. * If the current size is large enough, nothing is changed.
  53. *
  54. * Note: this does not check the available space! The caller
  55. * is responsible for performing those calculations.
  56. */
  57. static int printbuf_extend(struct printbuf *p, int min_size)
  58. {
  59. #define PRINTBUF_EXTEND_BY_BYTES_MIN (8)
  60. char *t = NULL;
  61. int new_size;
  62. if(
  63. (p != NULL) &&
  64. (p->buf != NULL) &&
  65. (p->size >= min_size)
  66. )
  67. return 0;
  68. new_size = p->size * 2;
  69. if (new_size < (min_size + PRINTBUF_EXTEND_BY_BYTES_MIN))
  70. new_size = min_size + PRINTBUF_EXTEND_BY_BYTES_MIN;
  71. #ifdef PRINTBUF_DEBUG
  72. MC_DEBUG("printbuf_memappend: realloc "
  73. "bpos=%d min_size=%d old_size=%d new_size=%d\n",
  74. p->bpos, min_size, p->size, new_size);
  75. #endif /* PRINTBUF_DEBUG */
  76. t = (char*)realloc(p->buf, new_size);
  77. if (t == NULL)
  78. return -1;
  79. p->size = new_size;
  80. p->buf = t;
  81. return 0;
  82. }
  83. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  84. {
  85. /* If, for some reason, buf is NULL, but
  86. * size is set -> extend to size
  87. */
  88. if ( (p->size > 0) && (p->buf == NULL)) {
  89. int size_wanted = p->size;
  90. p->size = 0;
  91. if (printbuf_extend(p, size_wanted) < 0)
  92. return -1;
  93. }
  94. if (p->size <= p->bpos + size + 1) {
  95. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  96. return -2;
  97. }
  98. memcpy(p->buf + p->bpos, buf, size);
  99. p->bpos += size;
  100. p->buf[p->bpos]= '\0';
  101. return size;
  102. }
  103. int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
  104. {
  105. int size_needed;
  106. if (offset == -1)
  107. offset = pb->bpos;
  108. size_needed = offset + len;
  109. if (pb->size < size_needed)
  110. {
  111. if (printbuf_extend(pb, size_needed) < 0)
  112. return -1;
  113. }
  114. memset(pb->buf + offset, charvalue, len);
  115. if (pb->bpos < size_needed)
  116. pb->bpos = size_needed;
  117. return 0;
  118. }
  119. int sprintbuf(struct printbuf *p, const char *msg, ...)
  120. {
  121. va_list ap;
  122. char *t;
  123. int size;
  124. char buf[PRINTBUF_DEFAULT_SIZE_BUF];
  125. /* user stack buffer first */
  126. va_start(ap, msg);
  127. size = vsnprintf(buf, sizeof(buf), msg, ap);
  128. va_end(ap);
  129. /* if string is greater than stack buffer, then use dynamic string
  130. with vasprintf. Note: some implementation of vsnprintf return -1
  131. if output is truncated whereas some return the number of bytes that
  132. would have been written - this code handles both cases.
  133. LJA: long unsigned int cast is done cause sizeof returns that type.
  134. */
  135. if(size == -1 || (long unsigned int)size > sizeof(buf)) {
  136. va_start(ap, msg);
  137. if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
  138. va_end(ap);
  139. printbuf_memappend(p, t, size);
  140. free(t);
  141. return size;
  142. } else {
  143. printbuf_memappend(p, buf, size);
  144. return size;
  145. }
  146. }
  147. void printbuf_reset(struct printbuf *p)
  148. {
  149. if (p != NULL)
  150. {
  151. /* safety checks */
  152. if (
  153. (p->size > 0) &&
  154. (p->buf != NULL)
  155. )
  156. {
  157. p->buf[0] = '\0';
  158. }
  159. p->bpos = 0;
  160. }
  161. }
  162. void printbuf_free(struct printbuf *p)
  163. {
  164. if(p) {
  165. if (p->buf != NULL)
  166. free(p->buf);
  167. p->buf = NULL;
  168. free(p);
  169. p = NULL;
  170. }
  171. }