Add regression test infrastructure.
[dragonfly.git] / usr.bin / make / buf.c
1 /*-
2  * Copyright (c) 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.11 1999/09/11 13:08:01 hoek Exp $
42  * $DragonFly: src/usr.bin/make/buf.c,v 1.29 2005/01/27 10:25:19 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 "sprite.h"
55 #include "util.h"
56
57 #ifndef MAX
58 #define MAX(a,b)  ((a) > (b) ? (a) : (b))
59 #endif
60
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  */
67 inline size_t
68 Buf_Size(const Buffer *buf)
69 {
70         return (buf->end - buf->buf);
71 }
72
73 /**
74  * Expand the buffer to hold the number of additional bytes, plus
75  * space to store a terminating NULL byte.
76  */
77 static inline void
78 BufExpand(Buffer *bp, size_t nb)
79 {
80         size_t  len = Buf_Size(bp);
81         if (bp->size < len + nb + 1) {
82                 int size = bp->size + MAX(nb + 1, BUF_ADD_INC);
83
84                 bp->size        = size;
85                 bp->buf         = erealloc(bp->buf, size);
86                 bp->end         = bp->buf + len;
87         }
88 }
89
90 /**
91  * Add a single byte to the buffer.
92  */
93 inline void
94 Buf_AddByte(Buffer *bp, Byte byte)
95 {
96         BufExpand(bp, 1);
97
98         *bp->end = byte;
99         bp->end++;
100         *bp->end = '\0';
101 }
102
103 /**
104  * Add bytes to the buffer.
105  */
106 void
107 Buf_AddBytes(Buffer *bp, size_t len, const Byte *bytes)
108 {
109         BufExpand(bp, len);
110
111         memcpy(bp->end, bytes, len);
112         bp->end += len;
113         *bp->end = '\0';
114 }
115
116 /**
117  * Get a reference to the internal buffer.
118  *
119  * @param len   Pointer to where we return the number of bytes in
120  *              the internal buffer.
121  *
122  * @return A pointer to the data.
123  */
124 Byte *
125 Buf_GetAll(Buffer *bp, size_t *len)
126 {
127         if (len != NULL)
128                 *len = Buf_Size(bp);
129
130         return (bp->buf);
131 }
132
133 /**
134  * Initialize a buffer. If no initial size is given, a reasonable
135  * default is used.
136  *
137  * @return A buffer object to be given to other functions in this library.
138  *
139  * Side Effects:
140  *      Space is allocated for the Buffer object and a internal buffer.
141  */
142 Buffer *
143 Buf_Init(size_t size)
144 {
145         Buffer *bp;     /* New Buffer */
146
147         if (size <= 0)
148                 size = BUF_DEF_SIZE;
149
150         bp = emalloc(sizeof(*bp));
151         bp->size        = size;
152         bp->buf         = emalloc(size);
153         bp->end         = bp->buf;
154         *bp->end        = '\0';
155
156         return (bp);
157 }
158
159 /**
160  * Destroy a buffer, and optionally free its data, too.
161  *
162  * Side Effects:
163  *      Space for the Buffer object and possibly the internal buffer
164  *      is de-allocated.
165  */
166 void
167 Buf_Destroy(Buffer *buf, Boolean freeData)
168 {
169         if (freeData)
170                 free(buf->buf);
171         free(buf);
172 }
173
174 /**
175  * Replace the last byte in a buffer.  If the buffer was empty
176  * intially, then a new byte will be added.
177  */
178 void
179 Buf_ReplaceLastByte(Buffer *bp, Byte byte)
180 {
181         if (bp->end == bp->buf) {
182                 Buf_AddByte(bp, byte);
183         } else {
184                 *(bp->end - 1) = byte;
185         }
186 }
187
188 /**
189  * Clear the contents of the buffer.
190  */
191 void
192 Buf_Clear(Buffer *bp)
193 {
194         bp->end = bp->buf;
195         *bp->end = '\0';
196 }
197
198 /**
199  * Append characters in str to Buffer object
200  */
201 void
202 Buf_Append(Buffer *bp, const char str[])
203 {
204         Buf_AddBytes(bp, strlen(str), str);
205 }
206
207 /**
208  * Append characters between str and end to Buffer object.
209  */
210 void
211 Buf_AppendRange(Buffer *bp, const char str[], const char *end)
212 {
213         Buf_AddBytes(bp, end - str, str);
214 }
215
216 /**
217  * Convert newlines in buffer to spaces.  The trailing newline is
218  * removed.
219  */
220 void
221 Buf_StripNewlines(Buffer *bp)
222 {
223         char *ptr = bp->end;
224
225         /*
226          * If there is anything in the buffer, remove the last
227          * newline character.
228          */
229         if (ptr != bp->buf) {
230                 if (*(ptr - 1) == '\n') {
231                         /* shorten buffer */
232                         *(ptr - 1) = '\0';
233                         --bp->end;
234                 }
235                 --ptr;
236         }
237
238         /* Convert newline characters to a space characters.  */
239         while (ptr != bp->buf) {
240                 if (*ptr == '\n') {
241                         *ptr = ' ';
242                 }
243                 --ptr;
244         }
245 }