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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. struct printbuf {
  21. char *buf;
  22. int bpos;
  23. int size;
  24. };
  25. extern struct printbuf*
  26. printbuf_new(void);
  27. /* As an optimization, printbuf_memappend_fast() is defined as a macro
  28. * that handles copying data if the buffer is large enough; otherwise
  29. * it invokes printbuf_memappend() which performs the heavy
  30. * lifting of realloc()ing the buffer and copying data.
  31. *
  32. * Your code should not use printbuf_memappend() directly unless it
  33. * checks the return code. Use 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. * Results in a compile error if the argument is not a string literal.
  48. */
  49. #define _printbuf_check_literal(mystr) ("" mystr)
  50. /**
  51. * This is an optimization wrapper around printbuf_memappend() that is useful
  52. * for appending string literals. Since the size of string constants is known
  53. * at compile time, using this macro can avoid a costly strlen() call. This is
  54. * especially helpful when a constant string must be appended many times. If
  55. * you got here because of a compilation error caused by passing something
  56. * other than a string literal, use printbuf_memappend_fast() in conjunction
  57. * with strlen().
  58. *
  59. * See also:
  60. * printbuf_memappend_fast()
  61. * printbuf_memappend()
  62. * sprintbuf()
  63. */
  64. #define printbuf_strappend(pb, str) \
  65. printbuf_memappend ((pb), _printbuf_check_literal(str), sizeof(str) - 1)
  66. /**
  67. * Set len bytes of the buffer to charvalue, starting at offset offset.
  68. * Similar to calling memset(x, charvalue, len);
  69. *
  70. * The memory allocated for the buffer is extended as necessary.
  71. *
  72. * If offset is -1, this starts at the end of the current data in the buffer.
  73. */
  74. extern int
  75. printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len);
  76. /**
  77. * Formatted print to printbuf.
  78. *
  79. * This function is the most expensive of the available functions for appending
  80. * string data to a printbuf and should be used only where convenience is more
  81. * important than speed. Avoid using this function in high performance code or
  82. * tight loops; in these scenarios, consider using snprintf() with a static
  83. * buffer in conjunction with one of the printbuf_*append() functions.
  84. *
  85. * See also:
  86. * printbuf_memappend_fast()
  87. * printbuf_memappend()
  88. * printbuf_strappend()
  89. */
  90. extern int
  91. sprintbuf(struct printbuf *p, const char *msg, ...);
  92. extern void
  93. printbuf_reset(struct printbuf *p);
  94. extern void
  95. printbuf_free(struct printbuf *p);
  96. #ifdef __cplusplus
  97. }
  98. #endif
  99. #endif