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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 is defined as a macro that
  29. * handles copying data if the buffer is large enough; otherwise it
  30. * invokes printbuf_memappend_real() which performs the heavy lifting
  31. * of realloc()ing the buffer and copying data.
  32. */
  33. extern int
  34. printbuf_memappend(struct printbuf *p, const char *buf, int size);
  35. #define printbuf_memappend_fast(p, bufptr, bufsize) \
  36. do { \
  37. if ((p->size - p->bpos) > bufsize) { \
  38. memcpy(p->buf + p->bpos, (bufptr), bufsize); \
  39. p->bpos += bufsize; \
  40. p->buf[p->bpos]= '\0'; \
  41. } else { printbuf_memappend(p, (bufptr), bufsize); } \
  42. } while (0)
  43. extern int
  44. sprintbuf(struct printbuf *p, const char *msg, ...);
  45. extern void
  46. printbuf_reset(struct printbuf *p);
  47. extern void
  48. printbuf_free(struct printbuf *p);
  49. #ifdef __cplusplus
  50. }
  51. #endif
  52. #endif