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 | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 23 | #undef PRINTBUF_DEBUG |
| 24 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 25 | struct printbuf { |
| 26 | char *buf; |
| 27 | int bpos; |
| 28 | int size; |
| 29 | }; |
| 30 | |
| 31 | extern struct printbuf* |
Michael Clark | 14862b1 | 2007-12-07 02:50:42 +0000 | [diff] [blame] | 32 | printbuf_new(void); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 33 | |
Brent Miller | f8663fc | 2009-08-20 06:41:32 +0000 | [diff] [blame^] | 34 | /* 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 Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 40 | */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 41 | extern int |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 42 | printbuf_memappend(struct printbuf *p, const char *buf, int size); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 43 | |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 44 | #define printbuf_memappend_fast(p, bufptr, bufsize) \ |
| 45 | do { \ |
| 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 53 | extern int |
| 54 | sprintbuf(struct printbuf *p, const char *msg, ...); |
| 55 | |
| 56 | extern void |
| 57 | printbuf_reset(struct printbuf *p); |
| 58 | |
| 59 | extern void |
| 60 | printbuf_free(struct printbuf *p); |
| 61 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 62 | #ifdef __cplusplus |
| 63 | } |
| 64 | #endif |
| 65 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 66 | #endif |