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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #define PRINTBUF_DEBUG 1
  16. #pragma GCC optimize ("O0")
  17. #include "config.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef HAVE_STDARG_H
  22. # include <stdarg.h>
  23. #else /* !HAVE_STDARG_H */
  24. # error Not enough var arg support!
  25. #endif /* HAVE_STDARG_H */
  26. #include "debug.h"
  27. #include "printbuf.h"
  28. #include "snprintf_compat.h"
  29. #include "vasprintf_compat.h"
  30. #define PRINTBUF_DEFAULT_SIZE (256)
  31. static int printbuf_extend(struct printbuf *p, int min_size);
  32. struct printbuf* printbuf_new(void)
  33. {
  34. struct printbuf *p;
  35. p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
  36. if(!p) return NULL;
  37. p->size = PRINTBUF_DEFAULT_SIZE;
  38. p->bpos = 0;
  39. p->buf = (char*)calloc(1, p->size);
  40. if(p->buf == NULL) {
  41. free(p);
  42. return NULL;
  43. }
  44. p->buf[0]= '\0';
  45. return p;
  46. }
  47. /**
  48. * Extend the buffer p so it has a size of at least min_size.
  49. *
  50. * If the current size is large enough, nothing is changed.
  51. *
  52. * Note: this does not check the available space! The caller
  53. * is responsible for performing those calculations.
  54. */
  55. static int printbuf_extend(struct printbuf *p, int min_size)
  56. {
  57. #define PRINTBUF_EXTEND_BY_BYTES_MIN (8)
  58. char *t = NULL;
  59. int new_size;
  60. if(
  61. (p != NULL) &&
  62. (p->buf != NULL) &&
  63. (p->size >= min_size)
  64. )
  65. return 0;
  66. new_size = p->size * 2;
  67. if (new_size < (min_size + PRINTBUF_EXTEND_BY_BYTES_MIN))
  68. new_size = min_size + PRINTBUF_EXTEND_BY_BYTES_MIN;
  69. #ifdef PRINTBUF_DEBUG
  70. MC_DEBUG("printbuf_memappend: realloc "
  71. "bpos=%d min_size=%d old_size=%d new_size=%d\n",
  72. p->bpos, min_size, p->size, new_size);
  73. #endif /* PRINTBUF_DEBUG */
  74. if (p != NULL)
  75. {
  76. t = (char*)calloc(1, new_size);
  77. if ( (t != NULL) &&
  78. (p->buf != NULL))
  79. {
  80. memcpy(t, p->buf, p->size);
  81. }
  82. }
  83. if (t == NULL)
  84. return -1;
  85. p->size = new_size;
  86. p->buf = t;
  87. return 0;
  88. }
  89. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  90. {
  91. if ( (p->size > 0) && (p->buf == NULL)) {
  92. int size_wanted = p->size;
  93. p->size = 0;
  94. if (printbuf_extend(p, size_wanted) < 0)
  95. return -1;
  96. }
  97. if (p->size <= p->bpos + size + 1) {
  98. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  99. return -2;
  100. }
  101. memcpy(p->buf + p->bpos, buf, size);
  102. p->bpos += size;
  103. p->buf[p->bpos]= '\0';
  104. return size;
  105. }
  106. int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
  107. {
  108. int size_needed;
  109. if (offset == -1)
  110. offset = pb->bpos;
  111. size_needed = offset + len;
  112. if (pb->size < size_needed)
  113. {
  114. if (printbuf_extend(pb, size_needed) < 0)
  115. return -1;
  116. }
  117. memset(pb->buf + offset, charvalue, len);
  118. if (pb->bpos < size_needed)
  119. pb->bpos = size_needed;
  120. return 0;
  121. }
  122. int sprintbuf(struct printbuf *p, const char *msg, ...)
  123. {
  124. va_list ap;
  125. char *t;
  126. long int size;
  127. #define PRINTBUF_DEFAULT_SIZE_BUF ((PRINTBUF_DEFAULT_SIZE<<2) > +1)
  128. char buf[PRINTBUF_DEFAULT_SIZE_BUF];
  129. /* user stack buffer first */
  130. va_start(ap, msg);
  131. size = (long int)vsnprintf(buf, sizeof(buf), msg, ap);
  132. va_end(ap);
  133. /* if string is greater than stack buffer, then use dynamic string
  134. with vasprintf. Note: some implementation of vsnprintf return -1
  135. if output is truncated whereas some return the number of bytes that
  136. would have been written - this code handles both cases. */
  137. if(size == -1 || size > (long int)sizeof(buf)) {
  138. va_start(ap, msg);
  139. if((size = (long int)vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
  140. va_end(ap);
  141. printbuf_memappend(p, t, size);
  142. free(t);
  143. return size;
  144. } else {
  145. printbuf_memappend(p, buf, size);
  146. return size;
  147. }
  148. }
  149. void printbuf_reset(struct printbuf *p)
  150. {
  151. if (p != NULL)
  152. {
  153. if ( (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. }