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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. static int printbuf_extend(struct printbuf *p, int min_size);
  29. struct printbuf *printbuf_new(void)
  30. {
  31. struct printbuf *p;
  32. p = (struct printbuf *)calloc(1, sizeof(struct printbuf));
  33. if (!p)
  34. return NULL;
  35. p->size = 32;
  36. p->bpos = 0;
  37. if (!(p->buf = (char *)malloc(p->size)))
  38. {
  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. char *t;
  56. int new_size;
  57. if (p->size >= min_size)
  58. return 0;
  59. new_size = p->size * 2;
  60. if (new_size < min_size + 8)
  61. new_size = min_size + 8;
  62. #ifdef PRINTBUF_DEBUG
  63. MC_DEBUG("printbuf_memappend: realloc "
  64. "bpos=%d min_size=%d old_size=%d new_size=%d\n",
  65. p->bpos, min_size, p->size, new_size);
  66. #endif /* PRINTBUF_DEBUG */
  67. if (!(t = (char *)realloc(p->buf, new_size)))
  68. return -1;
  69. p->size = new_size;
  70. p->buf = t;
  71. return 0;
  72. }
  73. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  74. {
  75. if (p->size <= p->bpos + size + 1)
  76. {
  77. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  78. return -1;
  79. }
  80. memcpy(p->buf + p->bpos, buf, size);
  81. p->bpos += size;
  82. p->buf[p->bpos] = '\0';
  83. return size;
  84. }
  85. int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
  86. {
  87. int size_needed;
  88. if (offset == -1)
  89. offset = pb->bpos;
  90. size_needed = offset + len;
  91. if (pb->size < size_needed)
  92. {
  93. if (printbuf_extend(pb, size_needed) < 0)
  94. return -1;
  95. }
  96. memset(pb->buf + offset, charvalue, len);
  97. if (pb->bpos < size_needed)
  98. pb->bpos = size_needed;
  99. return 0;
  100. }
  101. int sprintbuf(struct printbuf *p, const char *msg, ...)
  102. {
  103. va_list ap;
  104. char *t;
  105. int size;
  106. char buf[128];
  107. /* user stack buffer first */
  108. va_start(ap, msg);
  109. size = vsnprintf(buf, 128, msg, ap);
  110. va_end(ap);
  111. /* if string is greater than stack buffer, then use dynamic string
  112. * with vasprintf. Note: some implementation of vsnprintf return -1
  113. * if output is truncated whereas some return the number of bytes that
  114. * would have been written - this code handles both cases.
  115. */
  116. if (size == -1 || size > 127)
  117. {
  118. va_start(ap, msg);
  119. if ((size = vasprintf(&t, msg, ap)) < 0)
  120. {
  121. va_end(ap);
  122. return -1;
  123. }
  124. va_end(ap);
  125. printbuf_memappend(p, t, size);
  126. free(t);
  127. return size;
  128. }
  129. else
  130. {
  131. printbuf_memappend(p, buf, size);
  132. return size;
  133. }
  134. }
  135. void printbuf_reset(struct printbuf *p)
  136. {
  137. p->buf[0] = '\0';
  138. p->bpos = 0;
  139. }
  140. void printbuf_free(struct printbuf *p)
  141. {
  142. if (p)
  143. {
  144. free(p->buf);
  145. free(p);
  146. }
  147. }