VirtualBox

source: kBuild/branches/FREEBSD/src/kmk/buf.c@ 163

Last change on this file since 163 was 24, checked in by bird, 23 years ago

Import of RELENG_4_7_0_RELEASE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1/*
2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3 * Copyright (c) 1988, 1989 by Adam de Boor
4 * Copyright (c) 1989 by Berkeley Softworks
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#ifndef lint
40#if 0
41static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93";
42#else
43static const char rcsid[] =
44 "$FreeBSD: src/usr.bin/make/buf.c,v 1.11 1999/09/11 13:08:01 hoek Exp $";
45#endif
46#endif /* not lint */
47
48/*-
49 * buf.c --
50 * Functions for automatically-expanded buffers.
51 */
52
53#include "sprite.h"
54#include "make.h"
55#include "buf.h"
56
57#ifndef max
58#define max(a,b) ((a) > (b) ? (a) : (b))
59#endif
60
61/*
62 * BufExpand --
63 * Expand the given buffer to hold the given number of additional
64 * bytes.
65 * Makes sure there's room for an extra NULL byte at the end of the
66 * buffer in case it holds a string.
67 */
68#define BufExpand(bp,nb) \
69 if (bp->left < (nb)+1) {\
70 int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
71 Byte *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
72 \
73 (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
74 (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
75 (bp)->buffer = newBuf;\
76 (bp)->size = newSize;\
77 (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
78 }
79
80#define BUF_DEF_SIZE 256 /* Default buffer size */
81#define BUF_ADD_INC 256 /* Expansion increment when Adding */
82#define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */
83
84/*-
85 *-----------------------------------------------------------------------
86 * Buf_OvAddByte --
87 * Add a single byte to the buffer. left is zero or negative.
88 *
89 * Results:
90 * None.
91 *
92 * Side Effects:
93 * The buffer may be expanded.
94 *
95 *-----------------------------------------------------------------------
96 */
97void
98Buf_OvAddByte (bp, byte)
99 register Buffer bp;
100 int byte;
101{
102 int nbytes = 1;
103 bp->left = 0;
104 BufExpand (bp, nbytes);
105
106 *bp->inPtr++ = byte;
107 bp->left--;
108
109 /*
110 * Null-terminate
111 */
112 *bp->inPtr = 0;
113}
114
115
116/*-
117 *-----------------------------------------------------------------------
118 * Buf_AddBytes --
119 * Add a number of bytes to the buffer.
120 *
121 * Results:
122 * None.
123 *
124 * Side Effects:
125 * Guess what?
126 *
127 *-----------------------------------------------------------------------
128 */
129void
130Buf_AddBytes (bp, numBytes, bytesPtr)
131 register Buffer bp;
132 int numBytes;
133 const Byte *bytesPtr;
134{
135
136 BufExpand (bp, numBytes);
137
138 memcpy (bp->inPtr, bytesPtr, numBytes);
139 bp->inPtr += numBytes;
140 bp->left -= numBytes;
141
142 /*
143 * Null-terminate
144 */
145 *bp->inPtr = 0;
146}
147
148
149/*-
150 *-----------------------------------------------------------------------
151 * Buf_UngetByte --
152 * Place the byte back at the beginning of the buffer.
153 *
154 * Results:
155 * SUCCESS if the byte was added ok. FAILURE if not.
156 *
157 * Side Effects:
158 * The byte is stuffed in the buffer and outPtr is decremented.
159 *
160 *-----------------------------------------------------------------------
161 */
162void
163Buf_UngetByte (bp, byte)
164 register Buffer bp;
165 int byte;
166{
167
168 if (bp->outPtr != bp->buffer) {
169 bp->outPtr--;
170 *bp->outPtr = byte;
171 } else if (bp->outPtr == bp->inPtr) {
172 *bp->inPtr = byte;
173 bp->inPtr++;
174 bp->left--;
175 *bp->inPtr = 0;
176 } else {
177 /*
178 * Yech. have to expand the buffer to stuff this thing in.
179 * We use a different expansion constant because people don't
180 * usually push back many bytes when they're doing it a byte at
181 * a time...
182 */
183 int numBytes = bp->inPtr - bp->outPtr;
184 Byte *newBuf;
185
186 newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
187 memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
188 bp->outPtr = newBuf + BUF_UNGET_INC;
189 bp->inPtr = bp->outPtr + numBytes;
190 free ((char *)bp->buffer);
191 bp->buffer = newBuf;
192 bp->size += BUF_UNGET_INC;
193 bp->left = bp->size - (bp->inPtr - bp->buffer);
194 bp->outPtr -= 1;
195 *bp->outPtr = byte;
196 }
197}
198
199
200/*-
201 *-----------------------------------------------------------------------
202 * Buf_UngetBytes --
203 * Push back a series of bytes at the beginning of the buffer.
204 *
205 * Results:
206 * None.
207 *
208 * Side Effects:
209 * outPtr is decremented and the bytes copied into the buffer.
210 *
211 *-----------------------------------------------------------------------
212 */
213void
214Buf_UngetBytes (bp, numBytes, bytesPtr)
215 register Buffer bp;
216 int numBytes;
217 Byte *bytesPtr;
218{
219
220 if (bp->outPtr - bp->buffer >= numBytes) {
221 bp->outPtr -= numBytes;
222 memcpy (bp->outPtr, bytesPtr, numBytes);
223 } else if (bp->outPtr == bp->inPtr) {
224 Buf_AddBytes (bp, numBytes, bytesPtr);
225 } else {
226 int curNumBytes = bp->inPtr - bp->outPtr;
227 Byte *newBuf;
228 int newBytes = max(numBytes,BUF_UNGET_INC);
229
230 newBuf = (Byte *)emalloc (bp->size + newBytes);
231 memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
232 bp->outPtr = newBuf + newBytes;
233 bp->inPtr = bp->outPtr + curNumBytes;
234 free ((char *)bp->buffer);
235 bp->buffer = newBuf;
236 bp->size += newBytes;
237 bp->left = bp->size - (bp->inPtr - bp->buffer);
238 bp->outPtr -= numBytes;
239 memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
240 }
241}
242
243
244/*-
245 *-----------------------------------------------------------------------
246 * Buf_GetByte --
247 * Return the next byte from the buffer. Actually returns an integer.
248 *
249 * Results:
250 * Returns BUF_ERROR if there's no byte in the buffer, or the byte
251 * itself if there is one.
252 *
253 * Side Effects:
254 * outPtr is incremented and both outPtr and inPtr will be reset if
255 * the buffer is emptied.
256 *
257 *-----------------------------------------------------------------------
258 */
259int
260Buf_GetByte (bp)
261 register Buffer bp;
262{
263 int res;
264
265 if (bp->inPtr == bp->outPtr) {
266 return (BUF_ERROR);
267 } else {
268 res = (int) *bp->outPtr;
269 bp->outPtr += 1;
270 if (bp->outPtr == bp->inPtr) {
271 bp->outPtr = bp->inPtr = bp->buffer;
272 bp->left = bp->size;
273 *bp->inPtr = 0;
274 }
275 return (res);
276 }
277}
278
279
280/*-
281 *-----------------------------------------------------------------------
282 * Buf_GetBytes --
283 * Extract a number of bytes from the buffer.
284 *
285 * Results:
286 * The number of bytes gotten.
287 *
288 * Side Effects:
289 * The passed array is overwritten.
290 *
291 *-----------------------------------------------------------------------
292 */
293int
294Buf_GetBytes (bp, numBytes, bytesPtr)
295 register Buffer bp;
296 int numBytes;
297 Byte *bytesPtr;
298{
299
300 if (bp->inPtr - bp->outPtr < numBytes) {
301 numBytes = bp->inPtr - bp->outPtr;
302 }
303 memcpy (bytesPtr, bp->outPtr, numBytes);
304 bp->outPtr += numBytes;
305
306 if (bp->outPtr == bp->inPtr) {
307 bp->outPtr = bp->inPtr = bp->buffer;
308 bp->left = bp->size;
309 *bp->inPtr = 0;
310 }
311 return (numBytes);
312}
313
314
315/*-
316 *-----------------------------------------------------------------------
317 * Buf_GetAll --
318 * Get all the available data at once.
319 *
320 * Results:
321 * A pointer to the data and the number of bytes available.
322 *
323 * Side Effects:
324 * None.
325 *
326 *-----------------------------------------------------------------------
327 */
328Byte *
329Buf_GetAll (bp, numBytesPtr)
330 register Buffer bp;
331 int *numBytesPtr;
332{
333
334 if (numBytesPtr != (int *)NULL) {
335 *numBytesPtr = bp->inPtr - bp->outPtr;
336 }
337
338 return (bp->outPtr);
339}
340
341
342/*-
343 *-----------------------------------------------------------------------
344 * Buf_Discard --
345 * Throw away bytes in a buffer.
346 *
347 * Results:
348 * None.
349 *
350 * Side Effects:
351 * The bytes are discarded.
352 *
353 *-----------------------------------------------------------------------
354 */
355void
356Buf_Discard (bp, numBytes)
357 register Buffer bp;
358 int numBytes;
359{
360
361 if (bp->inPtr - bp->outPtr <= numBytes) {
362 bp->inPtr = bp->outPtr = bp->buffer;
363 bp->left = bp->size;
364 *bp->inPtr = 0;
365 } else {
366 bp->outPtr += numBytes;
367 }
368}
369
370
371/*-
372 *-----------------------------------------------------------------------
373 * Buf_Size --
374 * Returns the number of bytes in the given buffer. Doesn't include
375 * the null-terminating byte.
376 *
377 * Results:
378 * The number of bytes.
379 *
380 * Side Effects:
381 * None.
382 *
383 *-----------------------------------------------------------------------
384 */
385int
386Buf_Size (buf)
387 Buffer buf;
388{
389 return (buf->inPtr - buf->outPtr);
390}
391
392
393/*-
394 *-----------------------------------------------------------------------
395 * Buf_Init --
396 * Initialize a buffer. If no initial size is given, a reasonable
397 * default is used.
398 *
399 * Results:
400 * A buffer to be given to other functions in this library.
401 *
402 * Side Effects:
403 * The buffer is created, the space allocated and pointers
404 * initialized.
405 *
406 *-----------------------------------------------------------------------
407 */
408Buffer
409Buf_Init (size)
410 int size; /* Initial size for the buffer */
411{
412 Buffer bp; /* New Buffer */
413
414 bp = (Buffer)emalloc(sizeof(*bp));
415
416 if (size <= 0) {
417 size = BUF_DEF_SIZE;
418 }
419 bp->left = bp->size = size;
420 bp->buffer = (Byte *)emalloc(size);
421 bp->inPtr = bp->outPtr = bp->buffer;
422 *bp->inPtr = 0;
423
424 return (bp);
425}
426
427
428/*-
429 *-----------------------------------------------------------------------
430 * Buf_Destroy --
431 * Nuke a buffer and all its resources.
432 *
433 * Results:
434 * None.
435 *
436 * Side Effects:
437 * The buffer is freed.
438 *
439 *-----------------------------------------------------------------------
440 */
441void
442Buf_Destroy (buf, freeData)
443 Buffer buf; /* Buffer to destroy */
444 Boolean freeData; /* TRUE if the data should be destroyed as well */
445{
446
447 if (freeData) {
448 free ((char *)buf->buffer);
449 }
450 free ((char *)buf);
451}
452
453
454/*-
455 *-----------------------------------------------------------------------
456 * Buf_ReplaceLastByte --
457 * Replace the last byte in a buffer.
458 *
459 * Results:
460 * None.
461 *
462 * Side Effects:
463 * If the buffer was empty intially, then a new byte will be added.
464 * Otherwise, the last byte is overwritten.
465 *
466 *-----------------------------------------------------------------------
467 */
468void
469Buf_ReplaceLastByte (buf, byte)
470 Buffer buf; /* buffer to augment */
471 int byte; /* byte to be written */
472{
473 if (buf->inPtr == buf->outPtr)
474 Buf_AddByte(buf, byte);
475 else
476 *(buf->inPtr - 1) = byte;
477}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette