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

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

No Description

Contributors (1)