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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #define PRINTBUF_DEFAULT_SIZE (32)
  29. static int printbuf_extend(struct printbuf *p, int min_size);
  30. struct printbuf* printbuf_new(void)
  31. {
  32. struct printbuf *p;
  33. p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
  34. if(!p) return NULL;
  35. p->size = PRINTBUF_DEFAULT_SIZE;
  36. p->bpos = 0;
  37. p->buf = (char*)calloc(1, p->size);
  38. if(p->buf == NULL) {
  39. free(p);
  40. return NULL;
  41. }
  42. p->buf[0]= '\0';
  43. return p;
  44. }
  45. /**
  46. * Extend the buffer p so it has a size of at least min_size.
  47. *
  48. * If the current size is large enough, nothing is changed.
  49. *
  50. * Note: this does not check the available space! The caller
  51. * is responsible for performing those calculations.
  52. */
  53. static int printbuf_extend(struct printbuf *p, int min_size)
  54. {
  55. #define PRINTBUF_EXTEND_BY_BYTES_MIN (8)
  56. char *t = NULL;
  57. int new_size;
  58. if(
  59. (p != NULL) &&
  60. (p->buf != NULL) &&
  61. (p->size >= min_size)
  62. )
  63. return 0;
  64. new_size = p->size * 2;
  65. if (new_size < (min_size + PRINTBUF_EXTEND_BY_BYTES_MIN))
  66. new_size = min_size + PRINTBUF_EXTEND_BY_BYTES_MIN;
  67. #ifdef PRINTBUF_DEBUG
  68. MC_DEBUG("printbuf_memappend: realloc "
  69. "bpos=%d min_size=%d old_size=%d new_size=%d\n",
  70. p->bpos, min_size, p->size, new_size);
  71. #endif /* PRINTBUF_DEBUG */
  72. if (p != NULL)
  73. {
  74. t = (char*)calloc(1, new_size);
  75. if ( (t != NULL) &&
  76. (p->buf != NULL))
  77. {
  78. memcpy(t, p->buf, p->size);
  79. }
  80. }
  81. if (t == NULL)
  82. return -1;
  83. p->size = new_size;
  84. p->buf = t;
  85. return 0;
  86. }
  87. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  88. {
  89. if ( (p->size > 0) && (p->buf == NULL)) {
  90. int size_wanted = p->size;
  91. p->size = 0;
  92. if (printbuf_extend(p, size_wanted) < 0)
  93. return -1;
  94. }
  95. if (p->size <= p->bpos + size + 1) {
  96. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  97. return -2;
  98. }
  99. memcpy(p->buf + p->bpos, buf, size);
  100. p->bpos += size;
  101. p->buf[p->bpos]= '\0';
  102. return size;
  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. int sprintbuf(struct printbuf *p, const char *msg, ...)
  121. {
  122. va_list ap;
  123. char *t;
  124. long int size;
  125. #define PRINTBUF_DEFAULT_SIZE_BUF ((PRINTBUF_DEFAULT_SIZE<<2) > +1)
  126. char buf[PRINTBUF_DEFAULT_SIZE_BUF];
  127. /* user stack buffer first */
  128. va_start(ap, msg);
  129. size = (long int)vsnprintf(buf, sizeof(buf), msg, ap);
  130. va_end(ap);
  131. /* if string is greater than stack buffer, then use dynamic string
  132. with vasprintf. Note: some implementation of vsnprintf return -1
  133. if output is truncated whereas some return the number of bytes that
  134. would have been written - this code handles both cases. */
  135. if(size == -1 || size > (long int)sizeof(buf)) {
  136. va_start(ap, msg);
  137. if((size = (long int)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. if ( (p->size > 0) &&
  152. (p->buf != NULL)
  153. )
  154. {
  155. p->buf[0] = '\0';
  156. }
  157. p->bpos = 0;
  158. }
  159. }
  160. void printbuf_free(struct printbuf *p)
  161. {
  162. if(p) {
  163. if (p->buf != NULL)
  164. free(p->buf);
  165. p->buf = NULL;
  166. free(p);
  167. p = NULL;
  168. }
  169. }