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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #if 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 "bits.h"
  25. #include "debug.h"
  26. #include "printbuf.h"
  27. struct printbuf* printbuf_new(void)
  28. {
  29. struct printbuf *p;
  30. p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
  31. if(!p) return NULL;
  32. p->size = 32;
  33. p->bpos = 0;
  34. if(!(p->buf = (char*)malloc(p->size))) {
  35. free(p);
  36. return NULL;
  37. }
  38. return p;
  39. }
  40. int printbuf_memappend(struct printbuf *p, const char *buf, int size)
  41. {
  42. char *t;
  43. if(p->size - p->bpos <= size) {
  44. int new_size = json_max(p->size * 2, p->bpos + size + 8);
  45. #ifdef PRINTBUF_DEBUG
  46. MC_DEBUG("printbuf_memappend: realloc "
  47. "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
  48. p->bpos, size, p->size, new_size);
  49. #endif /* PRINTBUF_DEBUG */
  50. if(!(t = (char*)realloc(p->buf, new_size))) return -1;
  51. p->size = new_size;
  52. p->buf = t;
  53. }
  54. memcpy(p->buf + p->bpos, buf, size);
  55. p->bpos += size;
  56. p->buf[p->bpos]= '\0';
  57. return size;
  58. }
  59. #if !HAVE_VSNPRINTF && defined(WIN32)
  60. # define vsnprintf _vsnprintf
  61. #elif !HAVE_VSNPRINTF /* !HAVE_VSNPRINTF */
  62. # error Need vsnprintf!
  63. #endif /* !HAVE_VSNPRINTF && defined(WIN32) */
  64. #if !HAVE_VASPRINTF
  65. /* CAW: compliant version of vasprintf */
  66. static int vasprintf(char **buf, const char *fmt, va_list ap)
  67. {
  68. #ifndef WIN32
  69. static char _T_emptybuffer = '\0';
  70. #endif /* !defined(WIN32) */
  71. int chars;
  72. char *b;
  73. if(!buf) { return -1; }
  74. #ifdef WIN32
  75. chars = _vscprintf(fmt, ap)+1;
  76. #else /* !defined(WIN32) */
  77. /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
  78. our buffer like on some 64bit sun systems.... but hey, its time to move on */
  79. chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
  80. if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
  81. #endif /* defined(WIN32) */
  82. b = (char*)malloc(sizeof(char)*chars);
  83. if(!b) { return -1; }
  84. if((chars = vsprintf(b, fmt, ap)) < 0)
  85. {
  86. free(b);
  87. } else {
  88. *buf = b;
  89. }
  90. return chars;
  91. }
  92. #endif /* !HAVE_VASPRINTF */
  93. int sprintbuf(struct printbuf *p, const char *msg, ...)
  94. {
  95. va_list ap;
  96. char *t;
  97. int size;
  98. char buf[128];
  99. /* user stack buffer first */
  100. va_start(ap, msg);
  101. size = vsnprintf(buf, 128, msg, ap);
  102. va_end(ap);
  103. /* if string is greater than stack buffer, then use dynamic string
  104. with vasprintf. Note: some implementation of vsnprintf return -1
  105. if output is truncated whereas some return the number of bytes that
  106. would have been written - this code handles both cases. */
  107. if(size == -1 || size > 127) {
  108. va_start(ap, msg);
  109. if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
  110. va_end(ap);
  111. printbuf_memappend(p, t, size);
  112. free(t);
  113. return size;
  114. } else {
  115. printbuf_memappend(p, buf, size);
  116. return size;
  117. }
  118. }
  119. void printbuf_reset(struct printbuf *p)
  120. {
  121. p->buf[0] = '\0';
  122. p->bpos = 0;
  123. }
  124. void printbuf_free(struct printbuf *p)
  125. {
  126. if(p) {
  127. free(p->buf);
  128. free(p);
  129. }
  130. }