printbuf.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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) return NULL;
  34. p->size = 32;
  35. p->bpos = 0;
  36. if(!(p->buf = (char*)malloc(p->size))) {
  37. free(p);
  38. return NULL;
  39. }
  40. p->buf[0]= '\0';
  41. return p;
  42. }
  43. /**
  44. * Extend the buffer p so it has a size of at least min_size.
  45. *
  46. * If the current size is large enough, nothing is changed.
  47. *
  48. * Note: this does not check the available space! The caller
  49. * is responsible for performing those calculations.
  50. */
  51. static int printbuf_extend(struct printbuf *p, int min_size)
  52. {
  53. char *t;
  54. int new_size;
  55. if (p->size >= min_size)
  56. return 0;
  57. new_size = p->size * 2;
  58. if (new_size < min_size + 8)
  59. new_size = min_size + 8;
  60. #ifdef PRINTBUF_DEBUG
  61. MC_DEBUG("printbuf_memappend: realloc "
  62. "bpos=%d min_size=%d old_size=%d new_size=%d\n",
  63. p->bpos, min_size, p->size, new_size);
  64. #endif /* PRINTBUF_DEBUG */
  65. if(!(t = (char*)realloc(p->buf, new_size)))
  66. return -1;
  67. p->size = new_size;
  68. p->buf = t;
  69. return 0;
  70. }
  71. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  72. {
  73. if (p->size <= p->bpos + size + 1) {
  74. if (printbuf_extend(p, p->bpos + size + 1) < 0)
  75. return -1;
  76. }
  77. memcpy(p->buf + p->bpos, buf, size);
  78. p->bpos += size;
  79. p->buf[p->bpos]= '\0';
  80. return size;
  81. }
  82. int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
  83. {
  84. int size_needed;
  85. if (offset == -1)
  86. offset = pb->bpos;
  87. size_needed = offset + len;
  88. if (pb->size < size_needed)
  89. {
  90. if (printbuf_extend(pb, size_needed) < 0)
  91. return -1;
  92. }
  93. memset(pb->buf + offset, charvalue, len);
  94. if (pb->bpos < size_needed)
  95. pb->bpos = size_needed;
  96. return 0;
  97. }
  98. int sprintbuf(struct printbuf *p, const char *msg, ...)
  99. {
  100. va_list ap;
  101. char *t;
  102. int size;
  103. char buf[128];
  104. /* user stack buffer first */
  105. va_start(ap, msg);
  106. size = vsnprintf(buf, 128, msg, ap);
  107. va_end(ap);
  108. /* if string is greater than stack buffer, then use dynamic string
  109. with vasprintf. Note: some implementation of vsnprintf return -1
  110. if output is truncated whereas some return the number of bytes that
  111. would have been written - this code handles both cases. */
  112. if(size == -1 || size > 127) {
  113. va_start(ap, msg);
  114. if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
  115. va_end(ap);
  116. printbuf_memappend(p, t, size);
  117. free(t);
  118. return size;
  119. } else {
  120. printbuf_memappend(p, buf, size);
  121. return size;
  122. }
  123. }
  124. void printbuf_reset(struct printbuf *p)
  125. {
  126. p->buf[0] = '\0';
  127. p->bpos = 0;
  128. }
  129. void printbuf_free(struct printbuf *p)
  130. {
  131. if(p) {
  132. free(p->buf);
  133. free(p);
  134. }
  135. }