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