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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #define printbuf_length(p) ((p)->bpos)
  46. /**
  47. * Set len bytes of the buffer to charvalue, starting at offset offset.
  48. * Similar to calling memset(x, charvalue, len);
  49. *
  50. * The memory allocated for the buffer is extended as necessary.
  51. *
  52. * If offset is -1, this starts at the end of the current data in the buffer.
  53. */
  54. extern int
  55. printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len);
  56. extern int
  57. sprintbuf(struct printbuf *p, const char *msg, ...);
  58. extern void
  59. printbuf_reset(struct printbuf *p);
  60. extern void
  61. printbuf_free(struct printbuf *p);
  62. #ifdef __cplusplus
  63. }
  64. #endif
  65. #endif