Simplify code by using IF_DRAIN.
[dragonfly.git] / usr.bin / make / buf.c
1 /*-
2  * Copyright (c) 2005 Max Okumoto
3  * Copyright (c) 1988, 1989, 1990, 1993
4  *      The Regents of the University of California.  All rights reserved.
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.
39  *
40  * @(#)buf.c    8.1 (Berkeley) 6/6/93
41  * $FreeBSD: src/usr.bin/make/buf.c,v 1.32 2005/02/07 11:27:47 harti Exp $
42  * $DragonFly: src/usr.bin/make/buf.c,v 1.38 2005/05/05 09:06:23 okumoto Exp $
43  */
44
45 /*
46  * buf.c
47  *      Functions for automatically-expanded buffers.
48  */
49
50 #include <string.h>
51 #include <stdlib.h>
52
53 #include "buf.h"
54 #include "util.h"
55
56 /**
57  * Returns the number of bytes in the buffer.  Doesn't include the
58  * null-terminating byte.
59  *
60  * @return The number of bytes in Buffer object.
61  */
62 inline size_t
63 Buf_Size(const Buffer *buf)
64 {
65
66         return (buf->end - buf->buf);
67 }
68
69 /**
70  * Returns a reference to the data contained in the buffer.
71  *  
72  * @note Adding data to the Buffer object may invalidate the reference.
73  */
74 inline char *
75 Buf_Data(const Buffer *bp)
76 {
77
78         return (bp->buf);
79 }
80
81 /**
82  * Expand the buffer to hold the number of additional bytes, plus
83  * space to store a terminating NULL byte.
84  */
85 static inline void
86 BufExpand(Buffer *bp, size_t nb)
87 {
88         size_t  len = Buf_Size(bp);
89         if (bp->size < len + nb + 1) {
90                 int size = bp->size + MAX(nb + 1, BUF_ADD_INC);
91
92                 bp->buf         = erealloc(bp->buf, size);
93                 bp->size        = size;
94                 bp->end         = bp->buf + len;
95         }
96 }
97
98 /**
99  * Add a single byte to the buffer.
100  */
101 inline void
102 Buf_AddByte(Buffer *bp, Byte byte)
103 {
104
105         BufExpand(bp, 1);
106
107         *bp->end = byte;
108         bp->end++;
109         *bp->end = '\0';
110 }
111
112 /**
113  * Add bytes to the buffer.
114  */
115 void
116 Buf_AddBytes(Buffer *bp, size_t len, const Byte *bytes)
117 {
118         BufExpand(bp, len);
119
120         memcpy(bp->end, bytes, len);
121         bp->end += len;
122         *bp->end = '\0';
123 }
124
125 /**
126  * Get a reference to the internal buffer.
127  *
128  * @param len   Pointer to where we return the number of bytes in
129  *              the internal buffer.
130  *
131  * @return A pointer to the data.
132  */
133 Byte *
134 Buf_GetAll(Buffer *bp, size_t *len)
135 {
136
137         if (len != NULL)
138                 *len = Buf_Size(bp);
139
140         return (bp->buf);
141 }
142
143 /**
144  * Get the contents of a buffer and destroy the buffer. If the buffer
145  * is NULL, return NULL.
146  *
147  * Returns:
148  *      the pointer to the data.
149  */
150 char *
151 Buf_Peel(Buffer *bp)
152 {
153         char *ret;
154
155         if (bp == NULL)
156                 return (NULL);
157         ret = bp->buf;
158         free(bp);
159         return (ret);
160 }
161
162 /**
163  * Initialize a buffer. If no initial size is given, a reasonable
164  * default is used.
165  *
166  * @return A buffer object to be given to other functions in this library.
167  *
168  * Side Effects:
169  *      Space is allocated for the Buffer object and a internal buffer.
170  */
171 Buffer *
172 Buf_Init(size_t size)
173 {
174         Buffer *bp;     /* New Buffer */
175
176         if (size <= 0)
177                 size = BUF_DEF_SIZE;
178
179         bp = emalloc(sizeof(*bp));
180         bp->size        = size;
181         bp->buf         = emalloc(size);
182         bp->end         = bp->buf;
183         *bp->end        = '\0';
184
185         return (bp);
186 }
187
188 /**
189  * Destroy a buffer, and optionally free its data, too.
190  *
191  * Side Effects:
192  *      Space for the Buffer object and possibly the internal buffer
193  *      is de-allocated.
194  */
195 void
196 Buf_Destroy(Buffer *buf, Boolean freeData)
197 {
198
199         if (freeData)
200                 free(buf->buf);
201         free(buf);
202 }
203
204 /**
205  * Replace the last byte in a buffer.  If the buffer was empty
206  * intially, then a new byte will be added.
207  */
208 void
209 Buf_ReplaceLastByte(Buffer *bp, Byte byte)
210 {
211
212         if (bp->end == bp->buf) {
213                 Buf_AddByte(bp, byte);
214         } else {
215                 *(bp->end - 1) = byte;
216         }
217 }
218
219 /**
220  * Append characters in str to Buffer object
221  */
222 void
223 Buf_Append(Buffer *bp, const char str[])
224 {
225
226         Buf_AddBytes(bp, strlen(str), str);
227 }
228
229 /**
230  * Append characters in buf to Buffer object
231  */
232 void
233 Buf_AppendBuf(Buffer *bp, const Buffer *buf)
234 {
235
236         Buf_AddBytes(bp, Buf_Size(buf), buf->buf);
237 }
238
239 /**
240  * Append characters between str and end to Buffer object.
241  */
242 void
243 Buf_AppendRange(Buffer *bp, const char str[], const char *end)
244 {
245
246         Buf_AddBytes(bp, end - str, str);
247 }
248
249 /**
250  * Convert newlines in buffer to spaces.  The trailing newline is
251  * removed.
252  */
253 void
254 Buf_StripNewlines(Buffer *bp)
255 {
256         char *ptr = bp->end;
257
258         /*
259          * If there is anything in the buffer, remove the last
260          * newline character.
261          */
262         if (ptr != bp->buf) {
263                 if (*(ptr - 1) == '\n') {
264                         /* shorten buffer */
265                         *(ptr - 1) = '\0';
266                         --bp->end;
267                 }
268                 --ptr;
269         }
270
271         /* Convert newline characters to a space characters.  */
272         while (ptr != bp->buf) {
273                 if (*ptr == '\n') {
274                         *ptr = ' ';
275                 }
276                 --ptr;
277         }
278 }
279
280 /**
281  * Clear the contents of the buffer.
282  */
283 void
284 Buf_Clear(Buffer *bp)
285 {
286
287         bp->end = bp->buf;
288         *bp->end = '\0';
289 }
290