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

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