- Reduce white space diffs after harti commited my change to FreeBSD.
[dragonfly.git] / usr.bin / make / buf.c
CommitLineData
e03e8291 1/*-
d6377c7c 2 * Copyright (c) 2005 Max Okumoto
9a309bcc
MD
3 * Copyright (c) 1988, 1989, 1990, 1993
4 * The Regents of the University of California. All rights reserved.
984263bc
MD
5 * Copyright (c) 1988, 1989 by Adam de Boor
6 * Copyright (c) 1989 by Berkeley Softworks
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Adam de Boor.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
1de703da
MD
39 *
40 * @(#)buf.c 8.1 (Berkeley) 6/6/93
41 * $FreeBSD: src/usr.bin/make/buf.c,v 1.11 1999/09/11 13:08:01 hoek Exp $
fa8e78c0 42 * $DragonFly: src/usr.bin/make/buf.c,v 1.32 2005/02/07 20:09:45 okumoto Exp $
984263bc
MD
43 */
44
e03e8291
MO
45/*
46 * buf.c
984263bc
MD
47 * Functions for automatically-expanded buffers.
48 */
49
9863ce62
MO
50#include <string.h>
51#include <stdlib.h>
52
1e609a21 53#include "buf.h"
9863ce62
MO
54#include "sprite.h"
55#include "util.h"
984263bc 56
e03e8291
MO
57#ifndef MAX
58#define MAX(a,b) ((a) > (b) ? (a) : (b))
984263bc
MD
59#endif
60
e03e8291
MO
61/**
62 * Returns the number of bytes in the buffer. Doesn't include the
63 * null-terminating byte.
64 *
65 * @return The number of bytes in Buffer object.
66 */
67inline size_t
68Buf_Size(const Buffer *buf)
69{
d6377c7c 70
e03e8291
MO
71 return (buf->end - buf->buf);
72}
73
74/**
75 * Expand the buffer to hold the number of additional bytes, plus
76 * space to store a terminating NULL byte.
984263bc 77 */
f3d85b3c
MO
78static inline void
79BufExpand(Buffer *bp, size_t nb)
80{
e03e8291
MO
81 size_t len = Buf_Size(bp);
82 if (bp->size < len + nb + 1) {
83 int size = bp->size + MAX(nb + 1, BUF_ADD_INC);
84
e03e8291 85 bp->buf = erealloc(bp->buf, size);
09040c89 86 bp->size = size;
e03e8291 87 bp->end = bp->buf + len;
f3d85b3c
MO
88 }
89}
984263bc 90
e03e8291
MO
91/**
92 * Add a single byte to the buffer.
93 */
94inline void
57a22dcf
MO
95Buf_AddByte(Buffer *bp, Byte byte)
96{
d6377c7c 97
e03e8291
MO
98 BufExpand(bp, 1);
99
100 *bp->end = byte;
101 bp->end++;
102 *bp->end = '\0';
57a22dcf
MO
103}
104
e03e8291
MO
105/**
106 * Add bytes to the buffer.
984263bc
MD
107 */
108void
e03e8291 109Buf_AddBytes(Buffer *bp, size_t len, const Byte *bytes)
984263bc 110{
e03e8291 111 BufExpand(bp, len);
8294fbb3 112
e03e8291
MO
113 memcpy(bp->end, bytes, len);
114 bp->end += len;
115 *bp->end = '\0';
984263bc 116}
fbfaa208 117
e03e8291
MO
118/**
119 * Get a reference to the internal buffer.
62502733 120 *
e03e8291
MO
121 * @param len Pointer to where we return the number of bytes in
122 * the internal buffer.
8294fbb3 123 *
e03e8291 124 * @return A pointer to the data.
984263bc
MD
125 */
126Byte *
e03e8291 127Buf_GetAll(Buffer *bp, size_t *len)
984263bc 128{
d6377c7c 129
e03e8291
MO
130 if (len != NULL)
131 *len = Buf_Size(bp);
8294fbb3 132
e03e8291 133 return (bp->buf);
8294fbb3 134}
984263bc 135
e03e8291
MO
136/**
137 * Initialize a buffer. If no initial size is given, a reasonable
138 * default is used.
8294fbb3 139 *
e03e8291 140 * @return A buffer object to be given to other functions in this library.
8294fbb3
MO
141 *
142 * Side Effects:
e03e8291 143 * Space is allocated for the Buffer object and a internal buffer.
984263bc 144 */
6a3d9147 145Buffer *
4f762dd1 146Buf_Init(size_t size)
984263bc 147{
bf2bcf34 148 Buffer *bp; /* New Buffer */
984263bc 149
1e609a21
MO
150 if (size <= 0)
151 size = BUF_DEF_SIZE;
152
e03e8291
MO
153 bp = emalloc(sizeof(*bp));
154 bp->size = size;
155 bp->buf = emalloc(size);
156 bp->end = bp->buf;
157 *bp->end = '\0';
984263bc 158
1e609a21 159 return (bp);
984263bc 160}
fbfaa208 161
e03e8291
MO
162/**
163 * Destroy a buffer, and optionally free its data, too.
984263bc 164 *
984263bc 165 * Side Effects:
e03e8291
MO
166 * Space for the Buffer object and possibly the internal buffer
167 * is de-allocated.
984263bc
MD
168 */
169void
6a3d9147 170Buf_Destroy(Buffer *buf, Boolean freeData)
984263bc 171{
d6377c7c 172
1e609a21 173 if (freeData)
e03e8291 174 free(buf->buf);
1e609a21 175 free(buf);
984263bc 176}
fbfaa208 177
e03e8291
MO
178/**
179 * Replace the last byte in a buffer. If the buffer was empty
180 * intially, then a new byte will be added.
984263bc
MD
181 */
182void
e03e8291 183Buf_ReplaceLastByte(Buffer *bp, Byte byte)
984263bc 184{
d6377c7c 185
e03e8291
MO
186 if (bp->end == bp->buf) {
187 Buf_AddByte(bp, byte);
188 } else {
189 *(bp->end - 1) = byte;
190 }
984263bc 191}
0964826d 192
fcf95e9c
MO
193/**
194 * Append characters in str to Buffer object
195 */
196void
197Buf_Append(Buffer *bp, const char str[])
198{
199 Buf_AddBytes(bp, strlen(str), str);
200}
201
76b1c519
MO
202/**
203 * Append characters between str and end to Buffer object.
204 */
205void
206Buf_AppendRange(Buffer *bp, const char str[], const char *end)
207{
fa8e78c0 208
76b1c519
MO
209 Buf_AddBytes(bp, end - str, str);
210}
211
628ad7f9
MO
212/**
213 * Convert newlines in buffer to spaces. The trailing newline is
214 * removed.
215 */
216void
217Buf_StripNewlines(Buffer *bp)
218{
219 char *ptr = bp->end;
220
221 /*
222 * If there is anything in the buffer, remove the last
223 * newline character.
224 */
225 if (ptr != bp->buf) {
226 if (*(ptr - 1) == '\n') {
227 /* shorten buffer */
228 *(ptr - 1) = '\0';
229 --bp->end;
230 }
231 --ptr;
232 }
233
234 /* Convert newline characters to a space characters. */
235 while (ptr != bp->buf) {
236 if (*ptr == '\n') {
237 *ptr = ' ';
238 }
239 --ptr;
240 }
241}
09040c89
MO
242
243/**
244 * Clear the contents of the buffer.
245 */
246void
247Buf_Clear(Buffer *bp)
248{
249
250 bp->end = bp->buf;
251 *bp->end = '\0';
252}
253