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

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