Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 2 | * $Id: printbuf.h,v 1.4 2006/01/26 02:16:28 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 5 | * Michael Clark <michael@metaparadigm.com> |
| 6 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 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. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 9 | * |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 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) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #ifndef _printbuf_h_ |
| 17 | #define _printbuf_h_ |
| 18 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 23 | struct printbuf { |
| 24 | char *buf; |
| 25 | int bpos; |
| 26 | int size; |
| 27 | }; |
| 28 | |
| 29 | extern struct printbuf* |
Michael Clark | 14862b1 | 2007-12-07 02:50:42 +0000 | [diff] [blame] | 30 | printbuf_new(void); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 31 | |
Brent Miller | f8663fc | 2009-08-20 06:41:32 +0000 | [diff] [blame] | 32 | /* 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 Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 38 | */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 39 | extern int |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 40 | printbuf_memappend(struct printbuf *p, const char *buf, int size); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 41 | |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 42 | #define printbuf_memappend_fast(p, bufptr, bufsize) \ |
| 43 | do { \ |
| 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 Haszlakiewicz | 2d48543 | 2012-04-02 15:39:55 -0500 | [diff] [blame] | 51 | #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 | */ |
| 61 | extern int |
| 62 | printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len); |
| 63 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 64 | extern int |
| 65 | sprintbuf(struct printbuf *p, const char *msg, ...); |
| 66 | |
| 67 | extern void |
| 68 | printbuf_reset(struct printbuf *p); |
| 69 | |
| 70 | extern void |
| 71 | printbuf_free(struct printbuf *p); |
| 72 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 73 | #ifdef __cplusplus |
| 74 | } |
| 75 | #endif |
| 76 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 77 | #endif |