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 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. static int printbuf_extend(struct printbuf *p, int min_size);
  27. struct printbuf* printbuf_new(void)
  28. {
  29. struct printbuf *p;
  30. p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
  31. if(!p) return NULL;
  32. p->size = 32;
  33. p->bpos = 0;
  34. if(!(p->buf = (char*)malloc(p->size))) {
  35. free(p);
  36. return NULL;
  37. }
  38. return p;
  39. }
  40. /**
  41. * Extend the buffer p so it has a size of at least min_size.
  42. *
  43. * If the current size is large enough, nothing is changed.
  44. *
  45. * Note: this does not check the available space! The caller
  46. * is responsible for performing those calculations.
  47. */
  48. static int printbuf_extend(struct printbuf *p, int min_size)
  49. {
  50. char *t;
  51. int new_size;
  52. if (p->size >= min_size)
  53. return 0;
  54. new_size = p->size * 2;
  55. if (new_size < min_size + 8)
  56. new_size = min_size + 8;
  57. #ifdef PRINTBUF_DEBUG
  58. MC_DEBUG("printbuf_memappend: realloc "
  59. "bpos=%d min_size=%d old_size=%d new_size=%d\n",
  60. p->bpos, min_size, p->size, new_size);
  61. #endif /* PRINTBUF_DEBUG */
  62. if(!(t = (char*)realloc(p->buf, new_size)))
  63. return -1;
  64. p->size = new_size;
  65. p->buf = t;
  66. return 0;
  67. }
  68. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  69. {
  70. if (p->size <= p->bpos + size + 1) {
  71. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  72. return -1;
  73. }
  74. if(size > 1)
  75. memcpy(p->buf + p->bpos, buf, size);
  76. else
  77. p->buf[p->bpos]= *buf;
  78. p->bpos += size;
  79. p->buf[p->bpos]= '\0';
  80. return size;
  81. }
  82. /* same as printbuf_memappend(), but contains some performance enhancements */
  83. int printbuf_memappend_no_nul(struct printbuf *p, const char *buf, const int size)
  84. {
  85. if (p->size <= p->bpos + size) {
  86. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  87. return -1;
  88. }
  89. if(size > 1)
  90. memcpy(p->buf + p->bpos, buf, size);
  91. else
  92. p->buf[p->bpos]= *buf;
  93. p->bpos += size;
  94. return 1;
  95. }
  96. void printbuf_terminate_string(struct printbuf *const p)
  97. {
  98. if (p->size <= p->bpos + 1) {
  99. if (printbuf_extend(p, p->bpos + 1) < 0)
  100. --p->bpos; /* overwrite last byte, best we can do */
  101. }
  102. p->buf[p->bpos]= '\0';
  103. }
  104. int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
  105. {
  106. int size_needed;
  107. if (offset == -1)
  108. offset = pb->bpos;
  109. size_needed = offset + len;
  110. if (pb->size < size_needed)
  111. {
  112. if (printbuf_extend(pb, size_needed) < 0)
  113. return -1;
  114. }
  115. memset(pb->buf + offset, charvalue, len);
  116. if (pb->bpos < size_needed)
  117. pb->bpos = size_needed;
  118. return 0;
  119. }
  120. #if !defined(HAVE_VSNPRINTF) && defined(_MSC_VER)
  121. # define vsnprintf _vsnprintf
  122. #elif !defined(HAVE_VSNPRINTF) /* !HAVE_VSNPRINTF */
  123. # error Need vsnprintf!
  124. #endif /* !HAVE_VSNPRINTF && defined(WIN32) */
  125. #if !defined(HAVE_VASPRINTF)
  126. /* CAW: compliant version of vasprintf */
  127. static int vasprintf(char **buf, const char *fmt, va_list ap)
  128. {
  129. #ifndef WIN32
  130. static char _T_emptybuffer = '\0';
  131. #endif /* !defined(WIN32) */
  132. int chars;
  133. char *b;
  134. if(!buf) { return -1; }
  135. #ifdef WIN32
  136. chars = _vscprintf(fmt, ap)+1;
  137. #else /* !defined(WIN32) */
  138. /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
  139. our buffer like on some 64bit sun systems.... but hey, its time to move on */
  140. chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
  141. if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
  142. #endif /* defined(WIN32) */
  143. b = (char*)malloc(sizeof(char)*chars);
  144. if(!b) { return -1; }
  145. if((chars = vsprintf(b, fmt, ap)) < 0)
  146. {
  147. free(b);
  148. } else {
  149. *buf = b;
  150. }
  151. return chars;
  152. }
  153. #endif /* !HAVE_VASPRINTF */
  154. int sprintbuf(struct printbuf *p, const char *msg, ...)
  155. {
  156. va_list ap;
  157. char *t;
  158. int size;
  159. char buf[128];
  160. /* user stack buffer first */
  161. va_start(ap, msg);
  162. size = vsnprintf(buf, 128, msg, ap);
  163. va_end(ap);
  164. /* if string is greater than stack buffer, then use dynamic string
  165. with vasprintf. Note: some implementation of vsnprintf return -1
  166. if output is truncated whereas some return the number of bytes that
  167. would have been written - this code handles both cases. */
  168. if(size == -1 || size > 127) {
  169. va_start(ap, msg);
  170. if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
  171. va_end(ap);
  172. printbuf_memappend(p, t, size);
  173. free(t);
  174. return size;
  175. } else {
  176. printbuf_memappend(p, buf, size);
  177. return size;
  178. }
  179. }
  180. void printbuf_reset(struct printbuf *p)
  181. {
  182. p->buf[0] = '\0';
  183. p->bpos = 0;
  184. }
  185. void printbuf_free(struct printbuf *p)
  186. {
  187. if(p) {
  188. free(p->buf);
  189. free(p);
  190. }
  191. }