buf.c revision 1.10 1 1.10 christos /* $NetBSD: buf.c,v 1.10 1997/07/01 21:17:08 christos 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.10 christos #include <sys/cdefs.h>
42 1.1 cgd #ifndef lint
43 1.6 christos #if 0
44 1.8 christos static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93";
45 1.6 christos #else
46 1.10 christos __RCSID("$NetBSD: buf.c,v 1.10 1997/07/01 21:17:08 christos Exp $");
47 1.6 christos #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd /*-
51 1.1 cgd * buf.c --
52 1.1 cgd * Functions for automatically-expanded buffers.
53 1.1 cgd */
54 1.1 cgd
55 1.1 cgd #include "sprite.h"
56 1.4 cgd #include "make.h"
57 1.1 cgd #include "buf.h"
58 1.1 cgd
59 1.1 cgd #ifndef max
60 1.1 cgd #define max(a,b) ((a) > (b) ? (a) : (b))
61 1.1 cgd #endif
62 1.1 cgd
63 1.1 cgd /*
64 1.1 cgd * BufExpand --
65 1.1 cgd * Expand the given buffer to hold the given number of additional
66 1.1 cgd * bytes.
67 1.1 cgd * Makes sure there's room for an extra NULL byte at the end of the
68 1.1 cgd * buffer in case it holds a string.
69 1.1 cgd */
70 1.1 cgd #define BufExpand(bp,nb) \
71 1.1 cgd if (bp->left < (nb)+1) {\
72 1.1 cgd int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
73 1.7 jtc Byte *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
74 1.1 cgd \
75 1.1 cgd (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
76 1.1 cgd (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
77 1.1 cgd (bp)->buffer = newBuf;\
78 1.1 cgd (bp)->size = newSize;\
79 1.1 cgd (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
80 1.1 cgd }
81 1.1 cgd
82 1.1 cgd #define BUF_DEF_SIZE 256 /* Default buffer size */
83 1.1 cgd #define BUF_ADD_INC 256 /* Expansion increment when Adding */
84 1.1 cgd #define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */
85 1.1 cgd
86 1.1 cgd /*-
87 1.1 cgd *-----------------------------------------------------------------------
88 1.1 cgd * Buf_OvAddByte --
89 1.1 cgd * Add a single byte to the buffer. left is zero or negative.
90 1.1 cgd *
91 1.1 cgd * Results:
92 1.1 cgd * None.
93 1.1 cgd *
94 1.1 cgd * Side Effects:
95 1.1 cgd * The buffer may be expanded.
96 1.1 cgd *
97 1.1 cgd *-----------------------------------------------------------------------
98 1.1 cgd */
99 1.1 cgd void
100 1.1 cgd Buf_OvAddByte (bp, byte)
101 1.1 cgd register Buffer bp;
102 1.4 cgd int byte;
103 1.1 cgd {
104 1.5 jtc int nbytes = 1;
105 1.1 cgd bp->left = 0;
106 1.5 jtc BufExpand (bp, nbytes);
107 1.1 cgd
108 1.1 cgd *bp->inPtr++ = byte;
109 1.1 cgd bp->left--;
110 1.1 cgd
111 1.1 cgd /*
112 1.1 cgd * Null-terminate
113 1.1 cgd */
114 1.1 cgd *bp->inPtr = 0;
115 1.1 cgd }
116 1.1 cgd
117 1.1 cgd /*-
119 1.1 cgd *-----------------------------------------------------------------------
120 1.1 cgd * Buf_AddBytes --
121 1.1 cgd * Add a number of bytes to the buffer.
122 1.1 cgd *
123 1.1 cgd * Results:
124 1.1 cgd * None.
125 1.1 cgd *
126 1.1 cgd * Side Effects:
127 1.1 cgd * Guess what?
128 1.1 cgd *
129 1.1 cgd *-----------------------------------------------------------------------
130 1.1 cgd */
131 1.1 cgd void
132 1.1 cgd Buf_AddBytes (bp, numBytes, bytesPtr)
133 1.1 cgd register Buffer bp;
134 1.8 christos int numBytes;
135 1.1 cgd const Byte *bytesPtr;
136 1.1 cgd {
137 1.1 cgd
138 1.1 cgd BufExpand (bp, numBytes);
139 1.4 cgd
140 1.1 cgd memcpy (bp->inPtr, bytesPtr, numBytes);
141 1.1 cgd bp->inPtr += numBytes;
142 1.1 cgd bp->left -= numBytes;
143 1.1 cgd
144 1.1 cgd /*
145 1.1 cgd * Null-terminate
146 1.1 cgd */
147 1.1 cgd *bp->inPtr = 0;
148 1.1 cgd }
149 1.1 cgd
150 1.1 cgd /*-
152 1.1 cgd *-----------------------------------------------------------------------
153 1.1 cgd * Buf_UngetByte --
154 1.1 cgd * Place the byte back at the beginning of the buffer.
155 1.1 cgd *
156 1.1 cgd * Results:
157 1.1 cgd * SUCCESS if the byte was added ok. FAILURE if not.
158 1.1 cgd *
159 1.1 cgd * Side Effects:
160 1.1 cgd * The byte is stuffed in the buffer and outPtr is decremented.
161 1.1 cgd *
162 1.1 cgd *-----------------------------------------------------------------------
163 1.1 cgd */
164 1.1 cgd void
165 1.4 cgd Buf_UngetByte (bp, byte)
166 1.1 cgd register Buffer bp;
167 1.1 cgd int byte;
168 1.1 cgd {
169 1.1 cgd
170 1.1 cgd if (bp->outPtr != bp->buffer) {
171 1.1 cgd bp->outPtr--;
172 1.1 cgd *bp->outPtr = byte;
173 1.1 cgd } else if (bp->outPtr == bp->inPtr) {
174 1.1 cgd *bp->inPtr = byte;
175 1.1 cgd bp->inPtr++;
176 1.1 cgd bp->left--;
177 1.1 cgd *bp->inPtr = 0;
178 1.1 cgd } else {
179 1.1 cgd /*
180 1.1 cgd * Yech. have to expand the buffer to stuff this thing in.
181 1.1 cgd * We use a different expansion constant because people don't
182 1.1 cgd * usually push back many bytes when they're doing it a byte at
183 1.1 cgd * a time...
184 1.1 cgd */
185 1.1 cgd int numBytes = bp->inPtr - bp->outPtr;
186 1.1 cgd Byte *newBuf;
187 1.4 cgd
188 1.1 cgd newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
189 1.1 cgd memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
190 1.1 cgd bp->outPtr = newBuf + BUF_UNGET_INC;
191 1.1 cgd bp->inPtr = bp->outPtr + numBytes;
192 1.1 cgd free ((char *)bp->buffer);
193 1.1 cgd bp->buffer = newBuf;
194 1.1 cgd bp->size += BUF_UNGET_INC;
195 1.1 cgd bp->left = bp->size - (bp->inPtr - bp->buffer);
196 1.1 cgd bp->outPtr -= 1;
197 1.1 cgd *bp->outPtr = byte;
198 1.1 cgd }
199 1.1 cgd }
200 1.1 cgd
201 1.1 cgd /*-
203 1.1 cgd *-----------------------------------------------------------------------
204 1.1 cgd * Buf_UngetBytes --
205 1.1 cgd * Push back a series of bytes at the beginning of the buffer.
206 1.1 cgd *
207 1.1 cgd * Results:
208 1.1 cgd * None.
209 1.1 cgd *
210 1.1 cgd * Side Effects:
211 1.1 cgd * outPtr is decremented and the bytes copied into the buffer.
212 1.1 cgd *
213 1.1 cgd *-----------------------------------------------------------------------
214 1.1 cgd */
215 1.1 cgd void
216 1.1 cgd Buf_UngetBytes (bp, numBytes, bytesPtr)
217 1.1 cgd register Buffer bp;
218 1.1 cgd int numBytes;
219 1.1 cgd Byte *bytesPtr;
220 1.1 cgd {
221 1.4 cgd
222 1.1 cgd if (bp->outPtr - bp->buffer >= numBytes) {
223 1.1 cgd bp->outPtr -= numBytes;
224 1.1 cgd memcpy (bp->outPtr, bytesPtr, numBytes);
225 1.1 cgd } else if (bp->outPtr == bp->inPtr) {
226 1.1 cgd Buf_AddBytes (bp, numBytes, bytesPtr);
227 1.1 cgd } else {
228 1.1 cgd int curNumBytes = bp->inPtr - bp->outPtr;
229 1.1 cgd Byte *newBuf;
230 1.4 cgd int newBytes = max(numBytes,BUF_UNGET_INC);
231 1.1 cgd
232 1.1 cgd newBuf = (Byte *)emalloc (bp->size + newBytes);
233 1.1 cgd memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
234 1.1 cgd bp->outPtr = newBuf + newBytes;
235 1.1 cgd bp->inPtr = bp->outPtr + curNumBytes;
236 1.1 cgd free ((char *)bp->buffer);
237 1.1 cgd bp->buffer = newBuf;
238 1.4 cgd bp->size += newBytes;
239 1.1 cgd bp->left = bp->size - (bp->inPtr - bp->buffer);
240 1.1 cgd bp->outPtr -= numBytes;
241 1.1 cgd memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
242 1.1 cgd }
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd /*-
247 1.1 cgd *-----------------------------------------------------------------------
248 1.1 cgd * Buf_GetByte --
249 1.1 cgd * Return the next byte from the buffer. Actually returns an integer.
250 1.1 cgd *
251 1.1 cgd * Results:
252 1.1 cgd * Returns BUF_ERROR if there's no byte in the buffer, or the byte
253 1.1 cgd * itself if there is one.
254 1.1 cgd *
255 1.1 cgd * Side Effects:
256 1.1 cgd * outPtr is incremented and both outPtr and inPtr will be reset if
257 1.1 cgd * the buffer is emptied.
258 1.1 cgd *
259 1.1 cgd *-----------------------------------------------------------------------
260 1.1 cgd */
261 1.1 cgd int
262 1.1 cgd Buf_GetByte (bp)
263 1.1 cgd register Buffer bp;
264 1.1 cgd {
265 1.1 cgd int res;
266 1.1 cgd
267 1.1 cgd if (bp->inPtr == bp->outPtr) {
268 1.1 cgd return (BUF_ERROR);
269 1.1 cgd } else {
270 1.1 cgd res = (int) *bp->outPtr;
271 1.1 cgd bp->outPtr += 1;
272 1.1 cgd if (bp->outPtr == bp->inPtr) {
273 1.1 cgd bp->outPtr = bp->inPtr = bp->buffer;
274 1.1 cgd bp->left = bp->size;
275 1.1 cgd *bp->inPtr = 0;
276 1.1 cgd }
277 1.1 cgd return (res);
278 1.1 cgd }
279 1.1 cgd }
280 1.1 cgd
281 1.1 cgd /*-
283 1.1 cgd *-----------------------------------------------------------------------
284 1.1 cgd * Buf_GetBytes --
285 1.1 cgd * Extract a number of bytes from the buffer.
286 1.1 cgd *
287 1.1 cgd * Results:
288 1.1 cgd * The number of bytes gotten.
289 1.1 cgd *
290 1.1 cgd * Side Effects:
291 1.1 cgd * The passed array is overwritten.
292 1.1 cgd *
293 1.1 cgd *-----------------------------------------------------------------------
294 1.1 cgd */
295 1.1 cgd int
296 1.8 christos Buf_GetBytes (bp, numBytes, bytesPtr)
297 1.1 cgd register Buffer bp;
298 1.1 cgd int numBytes;
299 1.1 cgd Byte *bytesPtr;
300 1.4 cgd {
301 1.1 cgd
302 1.1 cgd if (bp->inPtr - bp->outPtr < numBytes) {
303 1.1 cgd numBytes = bp->inPtr - bp->outPtr;
304 1.1 cgd }
305 1.1 cgd memcpy (bytesPtr, bp->outPtr, numBytes);
306 1.1 cgd bp->outPtr += numBytes;
307 1.1 cgd
308 1.1 cgd if (bp->outPtr == bp->inPtr) {
309 1.1 cgd bp->outPtr = bp->inPtr = bp->buffer;
310 1.1 cgd bp->left = bp->size;
311 1.1 cgd *bp->inPtr = 0;
312 1.1 cgd }
313 1.1 cgd return (numBytes);
314 1.1 cgd }
315 1.1 cgd
316 1.1 cgd /*-
318 1.1 cgd *-----------------------------------------------------------------------
319 1.1 cgd * Buf_GetAll --
320 1.1 cgd * Get all the available data at once.
321 1.1 cgd *
322 1.1 cgd * Results:
323 1.1 cgd * A pointer to the data and the number of bytes available.
324 1.1 cgd *
325 1.1 cgd * Side Effects:
326 1.1 cgd * None.
327 1.1 cgd *
328 1.1 cgd *-----------------------------------------------------------------------
329 1.1 cgd */
330 1.1 cgd Byte *
331 1.1 cgd Buf_GetAll (bp, numBytesPtr)
332 1.1 cgd register Buffer bp;
333 1.8 christos int *numBytesPtr;
334 1.1 cgd {
335 1.1 cgd
336 1.1 cgd if (numBytesPtr != (int *)NULL) {
337 1.1 cgd *numBytesPtr = bp->inPtr - bp->outPtr;
338 1.1 cgd }
339 1.1 cgd
340 1.1 cgd return (bp->outPtr);
341 1.1 cgd }
342 1.1 cgd
343 1.1 cgd /*-
345 1.1 cgd *-----------------------------------------------------------------------
346 1.8 christos * Buf_Discard --
347 1.1 cgd * Throw away bytes in a buffer.
348 1.1 cgd *
349 1.1 cgd * Results:
350 1.1 cgd * None.
351 1.1 cgd *
352 1.1 cgd * Side Effects:
353 1.1 cgd * The bytes are discarded.
354 1.1 cgd *
355 1.1 cgd *-----------------------------------------------------------------------
356 1.1 cgd */
357 1.1 cgd void
358 1.1 cgd Buf_Discard (bp, numBytes)
359 1.1 cgd register Buffer bp;
360 1.1 cgd int numBytes;
361 1.1 cgd {
362 1.1 cgd
363 1.1 cgd if (bp->inPtr - bp->outPtr <= numBytes) {
364 1.1 cgd bp->inPtr = bp->outPtr = bp->buffer;
365 1.1 cgd bp->left = bp->size;
366 1.1 cgd *bp->inPtr = 0;
367 1.1 cgd } else {
368 1.1 cgd bp->outPtr += numBytes;
369 1.1 cgd }
370 1.1 cgd }
371 1.1 cgd
372 1.1 cgd /*-
374 1.1 cgd *-----------------------------------------------------------------------
375 1.1 cgd * Buf_Size --
376 1.1 cgd * Returns the number of bytes in the given buffer. Doesn't include
377 1.1 cgd * the null-terminating byte.
378 1.1 cgd *
379 1.1 cgd * Results:
380 1.1 cgd * The number of bytes.
381 1.1 cgd *
382 1.1 cgd * Side Effects:
383 1.1 cgd * None.
384 1.1 cgd *
385 1.1 cgd *-----------------------------------------------------------------------
386 1.1 cgd */
387 1.1 cgd int
388 1.1 cgd Buf_Size (buf)
389 1.1 cgd Buffer buf;
390 1.1 cgd {
391 1.1 cgd return (buf->inPtr - buf->outPtr);
392 1.1 cgd }
393 1.1 cgd
394 1.1 cgd /*-
396 1.1 cgd *-----------------------------------------------------------------------
397 1.1 cgd * Buf_Init --
398 1.1 cgd * Initialize a buffer. If no initial size is given, a reasonable
399 1.1 cgd * default is used.
400 1.1 cgd *
401 1.1 cgd * Results:
402 1.1 cgd * A buffer to be given to other functions in this library.
403 1.1 cgd *
404 1.1 cgd * Side Effects:
405 1.1 cgd * The buffer is created, the space allocated and pointers
406 1.1 cgd * initialized.
407 1.1 cgd *
408 1.1 cgd *-----------------------------------------------------------------------
409 1.1 cgd */
410 1.1 cgd Buffer
411 1.1 cgd Buf_Init (size)
412 1.1 cgd int size; /* Initial size for the buffer */
413 1.1 cgd {
414 1.1 cgd Buffer bp; /* New Buffer */
415 1.1 cgd
416 1.1 cgd bp = (Buffer)emalloc(sizeof(*bp));
417 1.1 cgd
418 1.1 cgd if (size <= 0) {
419 1.1 cgd size = BUF_DEF_SIZE;
420 1.1 cgd }
421 1.1 cgd bp->left = bp->size = size;
422 1.1 cgd bp->buffer = (Byte *)emalloc(size);
423 1.1 cgd bp->inPtr = bp->outPtr = bp->buffer;
424 1.1 cgd *bp->inPtr = 0;
425 1.1 cgd
426 1.1 cgd return (bp);
427 1.1 cgd }
428 1.1 cgd
429 1.1 cgd /*-
431 1.1 cgd *-----------------------------------------------------------------------
432 1.1 cgd * Buf_Destroy --
433 1.1 cgd * Nuke a buffer and all its resources.
434 1.1 cgd *
435 1.1 cgd * Results:
436 1.1 cgd * None.
437 1.1 cgd *
438 1.8 christos * Side Effects:
439 1.1 cgd * The buffer is freed.
440 1.1 cgd *
441 1.1 cgd *-----------------------------------------------------------------------
442 1.1 cgd */
443 1.8 christos void
444 1.8 christos Buf_Destroy (buf, freeData)
445 1.8 christos Buffer buf; /* Buffer to destroy */
446 1.8 christos Boolean freeData; /* TRUE if the data should be destroyed as well */
447 1.8 christos {
448 1.8 christos
449 1.8 christos if (freeData) {
450 1.8 christos free ((char *)buf->buffer);
451 1.8 christos }
452 1.8 christos free ((char *)buf);
453 1.8 christos }
454 1.8 christos
455 1.8 christos /*-
457 1.8 christos *-----------------------------------------------------------------------
458 1.8 christos * Buf_ReplaceLastByte --
459 1.8 christos * Replace the last byte in a buffer.
460 1.8 christos *
461 1.8 christos * Results:
462 1.9 christos * None.
463 1.8 christos *
464 1.8 christos * Side Effects:
465 1.8 christos * If the buffer was empty intially, then a new byte will be added.
466 1.8 christos * Otherwise, the last byte is overwritten.
467 1.8 christos *
468 1.1 cgd *-----------------------------------------------------------------------
469 */
470 void
471 Buf_ReplaceLastByte (buf, byte)
472 Buffer buf; /* buffer to augment */
473 int byte; /* byte to be written */
474 {
475 if (buf->inPtr == buf->outPtr)
476 Buf_AddByte(buf, byte);
477 else
478 *(buf->inPtr - 1) = byte;
479 }
480