Home | History | Annotate | Line # | Download | only in fmt
buffer.h revision 1.3
      1 /*	$NetBSD: buffer.h,v 1.3 2006/01/04 22:05:26 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <stddef.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <err.h>
     43 
     44 #define BUF_SIZE	BUFSIZ
     45 struct buffer {
     46 	char *ptr;
     47 	char *bptr;
     48 	char *eptr;
     49 };
     50 
     51 static void
     52 buf_init(struct buffer *buf)
     53 {
     54 	buf->ptr = buf->bptr = malloc(BUF_SIZE);
     55 	if (buf->ptr == NULL)
     56 		err(1, "Cannot allocate buffer");
     57 	buf->eptr = buf->ptr + BUF_SIZE;
     58 }
     59 
     60 static void
     61 buf_end(struct buffer *buf)
     62 {
     63 	free(buf->bptr);
     64 }
     65 
     66 static void
     67 buf_grow(struct buffer *buf, size_t minsize)
     68 {
     69 	ptrdiff_t diff;
     70 	size_t len = (buf->eptr - buf->bptr) +
     71 	    (minsize > BUF_SIZE ? minsize : BUF_SIZE);
     72 	char *nptr = realloc(buf->bptr, len);
     73 
     74 	if (nptr == NULL)
     75 		err(1, "Cannot grow buffer");
     76 
     77 	if (nptr == buf->bptr) {
     78 		buf->eptr = buf->bptr + len;
     79 		return;
     80 	}
     81 
     82 	diff = nptr - buf->bptr;
     83 	buf->bptr += diff;
     84 	buf->eptr = buf->bptr + len;
     85 	buf->ptr += diff;
     86 }
     87 
     88 static inline void
     89 buf_putc(struct buffer *buf, char c)
     90 {
     91 	if (buf->ptr >= buf->eptr)
     92 		buf_grow(buf, 1);
     93 	*buf->ptr++ = c;
     94 }
     95 
     96 static inline void
     97 buf_reset(struct buffer *buf)
     98 {
     99 	buf->ptr = buf->bptr;
    100 }
    101 
    102 static inline char
    103 buf_unputc(struct buffer *buf)
    104 {
    105 	return buf->ptr > buf->bptr ? *--buf->ptr : '\0';
    106 }
    107