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.5 kB

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