xref: /netbsd-src/usr.bin/fmt/buffer.h (revision d40754b094fab3343b249f59ade092ca3bddf330)
1*d40754b0Schristos /*	$NetBSD: buffer.h,v 1.5 2017/10/13 00:11:56 christos Exp $	*/
29ac95763Schristos 
39ac95763Schristos /*-
49ac95763Schristos  * Copyright (c) 2005 The NetBSD Foundation, Inc.
59ac95763Schristos  * All rights reserved.
69ac95763Schristos  *
79ac95763Schristos  * This code is derived from software contributed to The NetBSD Foundation
89ac95763Schristos  * by Christos Zoulas.
99ac95763Schristos  *
109ac95763Schristos  * Redistribution and use in source and binary forms, with or without
119ac95763Schristos  * modification, are permitted provided that the following conditions
129ac95763Schristos  * are met:
139ac95763Schristos  * 1. Redistributions of source code must retain the above copyright
149ac95763Schristos  *    notice, this list of conditions and the following disclaimer.
159ac95763Schristos  * 2. Redistributions in binary form must reproduce the above copyright
169ac95763Schristos  *    notice, this list of conditions and the following disclaimer in the
179ac95763Schristos  *    documentation and/or other materials provided with the distribution.
189ac95763Schristos  *
199ac95763Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209ac95763Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219ac95763Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229ac95763Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239ac95763Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249ac95763Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259ac95763Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269ac95763Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279ac95763Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289ac95763Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299ac95763Schristos  * POSSIBILITY OF SUCH DAMAGE.
309ac95763Schristos  */
319ac95763Schristos 
329ac95763Schristos #include <stddef.h>
339ac95763Schristos #include <stdio.h>
349ac95763Schristos #include <stdlib.h>
35*d40754b0Schristos #include <wchar.h>
369ac95763Schristos #include <err.h>
379ac95763Schristos 
384d09f9adSchristos #define BUF_SIZE	BUFSIZ
399ac95763Schristos struct buffer {
40*d40754b0Schristos 	wchar_t *ptr;
41*d40754b0Schristos 	wchar_t *bptr;
42*d40754b0Schristos 	wchar_t *eptr;
439ac95763Schristos };
449ac95763Schristos 
459ac95763Schristos static void
buf_init(struct buffer * buf)469ac95763Schristos buf_init(struct buffer *buf)
479ac95763Schristos {
48*d40754b0Schristos 	buf->ptr = buf->bptr = calloc(BUF_SIZE, sizeof(*buf->ptr));
499ac95763Schristos 	if (buf->ptr == NULL)
509ac95763Schristos 		err(1, "Cannot allocate buffer");
514d09f9adSchristos 	buf->eptr = buf->ptr + BUF_SIZE;
529ac95763Schristos }
539ac95763Schristos 
549ac95763Schristos static void
buf_end(struct buffer * buf)559ac95763Schristos buf_end(struct buffer *buf)
569ac95763Schristos {
579ac95763Schristos 	free(buf->bptr);
589ac95763Schristos }
599ac95763Schristos 
609ac95763Schristos static void
buf_grow(struct buffer * buf,size_t minsize)619ac95763Schristos buf_grow(struct buffer *buf, size_t minsize)
629ac95763Schristos {
639ac95763Schristos 	ptrdiff_t diff;
649ac95763Schristos 	size_t len = (buf->eptr - buf->bptr) +
654d09f9adSchristos 	    (minsize > BUF_SIZE ? minsize : BUF_SIZE);
66*d40754b0Schristos 	wchar_t *nptr = realloc(buf->bptr, len * sizeof(*buf->ptr));
679ac95763Schristos 
689ac95763Schristos 	if (nptr == NULL)
699ac95763Schristos 		err(1, "Cannot grow buffer");
709ac95763Schristos 
714d09f9adSchristos 	if (nptr == buf->bptr) {
724d09f9adSchristos 		buf->eptr = buf->bptr + len;
739ac95763Schristos 		return;
744d09f9adSchristos 	}
759ac95763Schristos 
769ac95763Schristos 	diff = nptr - buf->bptr;
779ac95763Schristos 	buf->bptr += diff;
789ac95763Schristos 	buf->eptr = buf->bptr + len;
799ac95763Schristos 	buf->ptr += diff;
809ac95763Schristos }
819ac95763Schristos 
820f0296d8Sperry static inline void
buf_putc(struct buffer * buf,wchar_t c)83*d40754b0Schristos buf_putc(struct buffer *buf, wchar_t c)
849ac95763Schristos {
859ac95763Schristos 	if (buf->ptr >= buf->eptr)
869ac95763Schristos 		buf_grow(buf, 1);
879ac95763Schristos 	*buf->ptr++ = c;
889ac95763Schristos }
899ac95763Schristos 
900f0296d8Sperry static inline void
buf_reset(struct buffer * buf)919ac95763Schristos buf_reset(struct buffer *buf)
929ac95763Schristos {
939ac95763Schristos 	buf->ptr = buf->bptr;
949ac95763Schristos }
959ac95763Schristos 
96*d40754b0Schristos static inline wchar_t
buf_unputc(struct buffer * buf)979ac95763Schristos buf_unputc(struct buffer *buf)
989ac95763Schristos {
999ac95763Schristos 	return buf->ptr > buf->bptr ? *--buf->ptr : '\0';
1009ac95763Schristos }
101