blob: b1bde7f9f8525d892ab0e7197d176a4dd32ea732 [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 Clarkf0d08882007-03-13 08:26:18 +000023struct printbuf {
24 char *buf;
25 int bpos;
26 int size;
27};
28
29extern struct printbuf*
Michael Clark14862b12007-12-07 02:50:42 +000030printbuf_new(void);
Michael Clarkf0d08882007-03-13 08:26:18 +000031
Brent Millerf8663fc2009-08-20 06:41:32 +000032/* As an optimization, printbuf_memappend_fast is defined as a macro
33 * that handles copying data if the buffer is large enough; otherwise
34 * it invokes printbuf_memappend_real() which performs the heavy
35 * lifting of realloc()ing the buffer and copying data.
36 * Your code should not use printbuf_memappend directly--use
37 * printbuf_memappend_fast instead.
Michael Clark95f55a72009-04-27 08:16:58 +000038 */
Michael Clarkf0d08882007-03-13 08:26:18 +000039extern int
Michael Clark68cafad2009-01-06 22:56:57 +000040printbuf_memappend(struct printbuf *p, const char *buf, int size);
Michael Clarkf0d08882007-03-13 08:26:18 +000041
Michael Clark95f55a72009-04-27 08:16:58 +000042#define printbuf_memappend_fast(p, bufptr, bufsize) \
43do { \
44 if ((p->size - p->bpos) > bufsize) { \
45 memcpy(p->buf + p->bpos, (bufptr), bufsize); \
46 p->bpos += bufsize; \
47 p->buf[p->bpos]= '\0'; \
48 } else { printbuf_memappend(p, (bufptr), bufsize); } \
49} while (0)
50
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050051#define printbuf_length(p) ((p)->bpos)
52
53/**
54 * Set len bytes of the buffer to charvalue, starting at offset offset.
55 * Similar to calling memset(x, charvalue, len);
56 *
57 * The memory allocated for the buffer is extended as necessary.
58 *
59 * If offset is -1, this starts at the end of the current data in the buffer.
60 */
61extern int
62printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len);
63
Michael Clarkf0d08882007-03-13 08:26:18 +000064extern int
65sprintbuf(struct printbuf *p, const char *msg, ...);
66
67extern void
68printbuf_reset(struct printbuf *p);
69
70extern void
71printbuf_free(struct printbuf *p);
72
Michael Clarkaaec1ef2009-02-25 02:31:32 +000073#ifdef __cplusplus
74}
75#endif
76
Michael Clarkf0d08882007-03-13 08:26:18 +000077#endif