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.h 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * $Id: printbuf.h,v 1.4 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. #ifndef _printbuf_h_
  16. #define _printbuf_h_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #undef PRINTBUF_DEBUG
  21. struct printbuf {
  22. char *buf;
  23. int bpos;
  24. int size;
  25. };
  26. extern struct printbuf*
  27. printbuf_new(void);
  28. /* As an optimization, printbuf_memappend_fast is defined as a macro
  29. * that handles copying data if the buffer is large enough; otherwise
  30. * it invokes printbuf_memappend_real() which performs the heavy
  31. * lifting of realloc()ing the buffer and copying data.
  32. * Your code should not use printbuf_memappend directly--use
  33. * printbuf_memappend_fast instead.
  34. */
  35. extern int
  36. printbuf_memappend(struct printbuf *p, const char *buf, int size);
  37. #define printbuf_memappend_fast(p, bufptr, bufsize) \
  38. do { \
  39. if ((p->size - p->bpos) > bufsize) { \
  40. memcpy(p->buf + p->bpos, (bufptr), bufsize); \
  41. p->bpos += bufsize; \
  42. p->buf[p->bpos]= '\0'; \
  43. } else { printbuf_memappend(p, (bufptr), bufsize); } \
  44. } while (0)
  45. extern int
  46. sprintbuf(struct printbuf *p, const char *msg, ...);
  47. extern void
  48. printbuf_reset(struct printbuf *p);
  49. extern void
  50. printbuf_free(struct printbuf *p);
  51. #ifdef __cplusplus
  52. }
  53. #endif
  54. #endif