2 * $Id: printbuf.h,v 1.4 2006/01/26 02:16:28 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
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.
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)
18 * @brief Internal string buffer handing. Unless you're writing a
19 * json_object_to_json_string_fn implementation for use with
20 * json_object_set_serializer() direct use of this is not
35 typedef struct printbuf printbuf;
37 extern struct printbuf*
40 /* As an optimization, printbuf_memappend_fast() is defined as a macro
41 * that handles copying data if the buffer is large enough; otherwise
42 * it invokes printbuf_memappend() which performs the heavy
43 * lifting of realloc()ing the buffer and copying data.
45 * Your code should not use printbuf_memappend() directly unless it
46 * checks the return code. Use printbuf_memappend_fast() instead.
49 printbuf_memappend(struct printbuf *p, const char *buf, int size);
51 #define printbuf_memappend_fast(p, bufptr, bufsize) \
53 if ((p->size - p->bpos) > bufsize) { \
54 memcpy(p->buf + p->bpos, (bufptr), bufsize); \
56 p->buf[p->bpos]= '\0'; \
57 } else { printbuf_memappend(p, (bufptr), bufsize); } \
60 #define printbuf_length(p) ((p)->bpos)
63 * Results in a compile error if the argument is not a string literal.
65 #define _printbuf_check_literal(mystr) ("" mystr)
68 * This is an optimization wrapper around printbuf_memappend() that is useful
69 * for appending string literals. Since the size of string constants is known
70 * at compile time, using this macro can avoid a costly strlen() call. This is
71 * especially helpful when a constant string must be appended many times. If
72 * you got here because of a compilation error caused by passing something
73 * other than a string literal, use printbuf_memappend_fast() in conjunction
77 * printbuf_memappend_fast()
78 * printbuf_memappend()
81 #define printbuf_strappend(pb, str) \
82 printbuf_memappend ((pb), _printbuf_check_literal(str), sizeof(str) - 1)
85 * Set len bytes of the buffer to charvalue, starting at offset offset.
86 * Similar to calling memset(x, charvalue, len);
88 * The memory allocated for the buffer is extended as necessary.
90 * If offset is -1, this starts at the end of the current data in the buffer.
93 printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len);
96 * Formatted print to printbuf.
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.
105 * printbuf_memappend_fast()
106 * printbuf_memappend()
107 * printbuf_strappend()
110 sprintbuf(struct printbuf *p, const char *msg, ...);
113 printbuf_reset(struct printbuf *p);
116 printbuf_free(struct printbuf *p);