printbuf.h 3.8 KB

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