blob: 5d4963f0de1b51e196fd69799cc82ae908f56b83 [file] [log] [blame]
Michael Clarkf0d08882007-03-13 08:26:18 +00001/*
Michael Clarkf6a6e482007-03-13 08:26:23 +00002 * $Id: printbuf.h,v 1.4 2006/01/26 02:16:28 mclark Exp $
Michael Clarkf0d08882007-03-13 08:26:18 +00003 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00004 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
Michael Clarkf0d08882007-03-13 08:26:18 +00005 * Michael Clark <michael@metaparadigm.com>
6 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00007 * 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.
Michael Clarkf0d08882007-03-13 08:26:18 +00009 *
Michael Clark95f55a72009-04-27 08:16:58 +000010 *
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)
Michael Clarkf0d08882007-03-13 08:26:18 +000014 */
15
16#ifndef _printbuf_h_
17#define _printbuf_h_
18
Michael Clarkaaec1ef2009-02-25 02:31:32 +000019#ifdef __cplusplus
20extern "C" {
21#endif
22
Michael Clark4504df72007-03-13 08:26:20 +000023#undef PRINTBUF_DEBUG
24
Michael Clarkf0d08882007-03-13 08:26:18 +000025struct printbuf {
26 char *buf;
27 int bpos;
28 int size;
29};
30
31extern struct printbuf*
Michael Clark14862b12007-12-07 02:50:42 +000032printbuf_new(void);
Michael Clarkf0d08882007-03-13 08:26:18 +000033
Brent Millerf8663fc2009-08-20 06:41:32 +000034/* As an optimization, printbuf_memappend_fast is defined as a macro
35 * that handles copying data if the buffer is large enough; otherwise
36 * it invokes printbuf_memappend_real() which performs the heavy
37 * lifting of realloc()ing the buffer and copying data.
38 * Your code should not use printbuf_memappend directly--use
39 * printbuf_memappend_fast instead.
Michael Clark95f55a72009-04-27 08:16:58 +000040 */
Michael Clarkf0d08882007-03-13 08:26:18 +000041extern int
Michael Clark68cafad2009-01-06 22:56:57 +000042printbuf_memappend(struct printbuf *p, const char *buf, int size);
Michael Clarkf0d08882007-03-13 08:26:18 +000043
Michael Clark95f55a72009-04-27 08:16:58 +000044#define printbuf_memappend_fast(p, bufptr, bufsize) \
45do { \
46 if ((p->size - p->bpos) > bufsize) { \
47 memcpy(p->buf + p->bpos, (bufptr), bufsize); \
48 p->bpos += bufsize; \
49 p->buf[p->bpos]= '\0'; \
50 } else { printbuf_memappend(p, (bufptr), bufsize); } \
51} while (0)
52
Michael Clarkf0d08882007-03-13 08:26:18 +000053extern int
54sprintbuf(struct printbuf *p, const char *msg, ...);
55
56extern void
57printbuf_reset(struct printbuf *p);
58
59extern void
60printbuf_free(struct printbuf *p);
61
Michael Clarkaaec1ef2009-02-25 02:31:32 +000062#ifdef __cplusplus
63}
64#endif
65
Michael Clarkf0d08882007-03-13 08:26:18 +000066#endif