buf.c revision 1.12 1 1.12 mycroft /* $NetBSD: buf.c,v 1.12 1999/09/15 04:16:31 mycroft Exp $ */
2 1.6 christos
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 1.1 cgd * Copyright (c) 1988, 1989 by Adam de Boor
6 1.1 cgd * Copyright (c) 1989 by Berkeley Softworks
7 1.1 cgd * All rights reserved.
8 1.1 cgd *
9 1.1 cgd * This code is derived from software contributed to Berkeley by
10 1.1 cgd * Adam de Boor.
11 1.1 cgd *
12 1.1 cgd * Redistribution and use in source and binary forms, with or without
13 1.1 cgd * modification, are permitted provided that the following conditions
14 1.1 cgd * are met:
15 1.1 cgd * 1. Redistributions of source code must retain the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer.
17 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 cgd * notice, this list of conditions and the following disclaimer in the
19 1.1 cgd * documentation and/or other materials provided with the distribution.
20 1.1 cgd * 3. All advertising materials mentioning features or use of this software
21 1.1 cgd * must display the following acknowledgement:
22 1.1 cgd * This product includes software developed by the University of
23 1.1 cgd * California, Berkeley and its contributors.
24 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.1 cgd * may be used to endorse or promote products derived from this software
26 1.1 cgd * without specific prior written permission.
27 1.1 cgd *
28 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 cgd * SUCH DAMAGE.
39 1.1 cgd */
40 1.1 cgd
41 1.11 lukem #ifdef MAKE_BOOTSTRAP
42 1.12 mycroft static char rcsid[] = "$NetBSD: buf.c,v 1.12 1999/09/15 04:16:31 mycroft Exp $";
43 1.11 lukem #else
44 1.10 christos #include <sys/cdefs.h>
45 1.1 cgd #ifndef lint
46 1.6 christos #if 0
47 1.8 christos static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93";
48 1.6 christos #else
49 1.12 mycroft __RCSID("$NetBSD: buf.c,v 1.12 1999/09/15 04:16:31 mycroft Exp $");
50 1.6 christos #endif
51 1.1 cgd #endif /* not lint */
52 1.11 lukem #endif
53 1.1 cgd
54 1.1 cgd /*-
55 1.1 cgd * buf.c --
56 1.1 cgd * Functions for automatically-expanded buffers.
57 1.1 cgd */
58 1.1 cgd
59 1.1 cgd #include "sprite.h"
60 1.4 cgd #include "make.h"
61 1.1 cgd #include "buf.h"
62 1.1 cgd
63 1.1 cgd #ifndef max
64 1.1 cgd #define max(a,b) ((a) > (b) ? (a) : (b))
65 1.1 cgd #endif
66 1.1 cgd
67 1.1 cgd /*
68 1.1 cgd * BufExpand --
69 1.1 cgd * Expand the given buffer to hold the given number of additional
70 1.1 cgd * bytes.
71 1.1 cgd * Makes sure there's room for an extra NULL byte at the end of the
72 1.1 cgd * buffer in case it holds a string.
73 1.1 cgd */
74 1.1 cgd #define BufExpand(bp,nb) \
75 1.12 mycroft while (bp->left < (nb)+1) {\
76 1.12 mycroft int newSize = (bp)->size * 2; \
77 1.7 jtc Byte *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
78 1.1 cgd \
79 1.1 cgd (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
80 1.1 cgd (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
81 1.1 cgd (bp)->buffer = newBuf;\
82 1.1 cgd (bp)->size = newSize;\
83 1.1 cgd (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
84 1.1 cgd }
85 1.1 cgd
86 1.1 cgd #define BUF_DEF_SIZE 256 /* Default buffer size */
87 1.1 cgd
88 1.1 cgd /*-
89 1.1 cgd *-----------------------------------------------------------------------
90 1.1 cgd * Buf_OvAddByte --
91 1.1 cgd * Add a single byte to the buffer. left is zero or negative.
92 1.1 cgd *
93 1.1 cgd * Results:
94 1.1 cgd * None.
95 1.1 cgd *
96 1.1 cgd * Side Effects:
97 1.1 cgd * The buffer may be expanded.
98 1.1 cgd *
99 1.1 cgd *-----------------------------------------------------------------------
100 1.1 cgd */
101 1.1 cgd void
102 1.1 cgd Buf_OvAddByte (bp, byte)
103 1.1 cgd register Buffer bp;
104 1.4 cgd int byte;
105 1.1 cgd {
106 1.5 jtc int nbytes = 1;
107 1.1 cgd bp->left = 0;
108 1.5 jtc BufExpand (bp, nbytes);
109 1.1 cgd
110 1.1 cgd *bp->inPtr++ = byte;
111 1.1 cgd bp->left--;
112 1.1 cgd
113 1.1 cgd /*
114 1.1 cgd * Null-terminate
115 1.1 cgd */
116 1.1 cgd *bp->inPtr = 0;
117 1.1 cgd }
118 1.1 cgd
119 1.1 cgd /*-
121 1.1 cgd *-----------------------------------------------------------------------
122 1.1 cgd * Buf_AddBytes --
123 1.1 cgd * Add a number of bytes to the buffer.
124 1.1 cgd *
125 1.1 cgd * Results:
126 1.1 cgd * None.
127 1.1 cgd *
128 1.1 cgd * Side Effects:
129 1.1 cgd * Guess what?
130 1.1 cgd *
131 1.1 cgd *-----------------------------------------------------------------------
132 1.1 cgd */
133 1.1 cgd void
134 1.1 cgd Buf_AddBytes (bp, numBytes, bytesPtr)
135 1.1 cgd register Buffer bp;
136 1.8 christos int numBytes;
137 1.1 cgd const Byte *bytesPtr;
138 1.1 cgd {
139 1.1 cgd
140 1.1 cgd BufExpand (bp, numBytes);
141 1.4 cgd
142 1.1 cgd memcpy (bp->inPtr, bytesPtr, numBytes);
143 1.1 cgd bp->inPtr += numBytes;
144 1.1 cgd bp->left -= numBytes;
145 1.1 cgd
146 1.1 cgd /*
147 1.1 cgd * Null-terminate
148 1.1 cgd */
149 1.1 cgd *bp->inPtr = 0;
150 1.1 cgd }
151 1.1 cgd
152 1.1 cgd /*-
154 1.1 cgd *-----------------------------------------------------------------------
155 1.1 cgd * Buf_GetAll --
156 1.1 cgd * Get all the available data at once.
157 1.1 cgd *
158 1.1 cgd * Results:
159 1.1 cgd * A pointer to the data and the number of bytes available.
160 1.1 cgd *
161 1.1 cgd * Side Effects:
162 1.1 cgd * None.
163 1.1 cgd *
164 1.1 cgd *-----------------------------------------------------------------------
165 1.1 cgd */
166 1.1 cgd Byte *
167 1.1 cgd Buf_GetAll (bp, numBytesPtr)
168 1.1 cgd register Buffer bp;
169 1.1 cgd int *numBytesPtr;
170 1.1 cgd {
171 1.1 cgd
172 1.1 cgd if (numBytesPtr != (int *)NULL) {
173 1.8 christos *numBytesPtr = bp->inPtr - bp->outPtr;
174 1.1 cgd }
175 1.1 cgd
176 1.1 cgd return (bp->outPtr);
177 1.1 cgd }
178 1.1 cgd
179 1.1 cgd /*-
181 1.1 cgd *-----------------------------------------------------------------------
182 1.1 cgd * Buf_Discard --
183 1.1 cgd * Throw away bytes in a buffer.
184 1.1 cgd *
185 1.1 cgd * Results:
186 1.8 christos * None.
187 1.1 cgd *
188 1.1 cgd * Side Effects:
189 1.1 cgd * The bytes are discarded.
190 1.1 cgd *
191 1.1 cgd *-----------------------------------------------------------------------
192 1.1 cgd */
193 1.1 cgd void
194 1.1 cgd Buf_Discard (bp, numBytes)
195 1.1 cgd register Buffer bp;
196 1.1 cgd int numBytes;
197 1.1 cgd {
198 1.1 cgd
199 1.1 cgd if (bp->inPtr - bp->outPtr <= numBytes) {
200 1.1 cgd bp->inPtr = bp->outPtr = bp->buffer;
201 1.1 cgd bp->left = bp->size;
202 1.1 cgd *bp->inPtr = 0;
203 1.1 cgd } else {
204 1.1 cgd bp->outPtr += numBytes;
205 1.1 cgd }
206 1.1 cgd }
207 1.1 cgd
208 1.1 cgd /*-
210 1.1 cgd *-----------------------------------------------------------------------
211 1.1 cgd * Buf_Size --
212 1.1 cgd * Returns the number of bytes in the given buffer. Doesn't include
213 1.1 cgd * the null-terminating byte.
214 1.1 cgd *
215 1.1 cgd * Results:
216 1.1 cgd * The number of bytes.
217 1.1 cgd *
218 1.1 cgd * Side Effects:
219 1.1 cgd * None.
220 1.1 cgd *
221 1.1 cgd *-----------------------------------------------------------------------
222 1.1 cgd */
223 1.1 cgd int
224 1.1 cgd Buf_Size (buf)
225 1.1 cgd Buffer buf;
226 1.1 cgd {
227 1.1 cgd return (buf->inPtr - buf->outPtr);
228 1.1 cgd }
229 1.1 cgd
230 1.1 cgd /*-
232 1.1 cgd *-----------------------------------------------------------------------
233 1.1 cgd * Buf_Init --
234 1.1 cgd * Initialize a buffer. If no initial size is given, a reasonable
235 1.1 cgd * default is used.
236 1.1 cgd *
237 1.1 cgd * Results:
238 1.1 cgd * A buffer to be given to other functions in this library.
239 1.1 cgd *
240 1.1 cgd * Side Effects:
241 1.1 cgd * The buffer is created, the space allocated and pointers
242 1.1 cgd * initialized.
243 1.1 cgd *
244 1.1 cgd *-----------------------------------------------------------------------
245 1.1 cgd */
246 1.1 cgd Buffer
247 1.1 cgd Buf_Init (size)
248 1.1 cgd int size; /* Initial size for the buffer */
249 1.1 cgd {
250 1.1 cgd Buffer bp; /* New Buffer */
251 1.1 cgd
252 1.1 cgd bp = (Buffer)emalloc(sizeof(*bp));
253 1.1 cgd
254 1.1 cgd if (size <= 0) {
255 1.1 cgd size = BUF_DEF_SIZE;
256 1.1 cgd }
257 1.1 cgd bp->left = bp->size = size;
258 1.1 cgd bp->buffer = (Byte *)emalloc(size);
259 1.1 cgd bp->inPtr = bp->outPtr = bp->buffer;
260 1.1 cgd *bp->inPtr = 0;
261 1.1 cgd
262 1.1 cgd return (bp);
263 1.1 cgd }
264 1.1 cgd
265 1.1 cgd /*-
267 1.1 cgd *-----------------------------------------------------------------------
268 1.1 cgd * Buf_Destroy --
269 1.1 cgd * Nuke a buffer and all its resources.
270 1.1 cgd *
271 1.1 cgd * Results:
272 1.1 cgd * None.
273 1.1 cgd *
274 1.1 cgd * Side Effects:
275 1.1 cgd * The buffer is freed.
276 1.1 cgd *
277 1.1 cgd *-----------------------------------------------------------------------
278 1.8 christos */
279 1.1 cgd void
280 1.1 cgd Buf_Destroy (buf, freeData)
281 1.1 cgd Buffer buf; /* Buffer to destroy */
282 1.1 cgd Boolean freeData; /* TRUE if the data should be destroyed as well */
283 1.8 christos {
284 1.8 christos
285 1.8 christos if (freeData) {
286 1.8 christos free ((char *)buf->buffer);
287 1.8 christos }
288 1.8 christos free ((char *)buf);
289 1.8 christos }
290 1.8 christos
291 1.8 christos /*-
293 1.8 christos *-----------------------------------------------------------------------
294 1.8 christos * Buf_ReplaceLastByte --
295 1.8 christos * Replace the last byte in a buffer.
296 1.8 christos *
297 1.8 christos * Results:
298 1.8 christos * None.
299 1.8 christos *
300 1.8 christos * Side Effects:
301 1.8 christos * If the buffer was empty intially, then a new byte will be added.
302 1.9 christos * Otherwise, the last byte is overwritten.
303 1.8 christos *
304 1.8 christos *-----------------------------------------------------------------------
305 1.8 christos */
306 1.8 christos void
307 1.8 christos Buf_ReplaceLastByte (buf, byte)
308 1.1 cgd Buffer buf; /* buffer to augment */
309 int byte; /* byte to be written */
310 {
311 if (buf->inPtr == buf->outPtr)
312 Buf_AddByte(buf, byte);
313 else
314 *(buf->inPtr - 1) = byte;
315 }
316