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