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

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