Changeset 51 for trunk/src/kmk/buf.c
- Timestamp:
- Apr 7, 2003, 3:30:32 AM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/buf.c
r35 r51 18 18 * 3. All advertising materials mentioning features or use of this software 19 19 * must display the following acknowledgement: 20 * 21 * 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 22 * 4. Neither the name of the University nor the names of its contributors 23 23 * may be used to endorse or promote products derived from this software … … 39 39 #ifndef lint 40 40 #if 0 41 static char sccsid[] = "@(#)buf.c 41 static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93"; 42 42 #else 43 43 static const char rcsid[] = 44 44 "$FreeBSD: src/usr.bin/make/buf.c,v 1.11 1999/09/11 13:08:01 hoek Exp $"; 45 45 #endif 46 #define KLIBFILEDEF rcsid 46 47 #endif /* not lint */ 47 48 48 49 /*- 49 50 * buf.c -- 50 * 51 * Functions for automatically-expanded buffers. 51 52 */ 52 53 … … 61 62 /* 62 63 * BufExpand -- 63 * 64 * 65 * 66 * 64 * Expand the given buffer to hold the given number of additional 65 * bytes. 66 * Makes sure there's room for an extra NULL byte at the end of the 67 * buffer in case it holds a string. 67 68 */ 68 69 #define BufExpand(bp,nb) \ 69 70 71 72 73 74 75 76 77 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 */70 if (bp->left < (nb)+1) {\ 71 int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \ 72 Byte *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \ 73 \ 74 (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \ 75 (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\ 76 (bp)->buffer = newBuf;\ 77 (bp)->size = newSize;\ 78 (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\ 79 } 80 81 #define BUF_DEF_SIZE 256 /* Default buffer size */ 82 #define BUF_ADD_INC 256 /* Expansion increment when Adding */ 83 #define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */ 83 84 84 85 /*- 85 86 *----------------------------------------------------------------------- 86 87 * Buf_OvAddByte -- 87 * 88 * 89 * Results: 90 * 91 * 92 * Side Effects: 93 * 88 * Add a single byte to the buffer. left is zero or negative. 89 * 90 * Results: 91 * None. 92 * 93 * Side Effects: 94 * The buffer may be expanded. 94 95 * 95 96 *----------------------------------------------------------------------- … … 117 118 *----------------------------------------------------------------------- 118 119 * Buf_AddBytes -- 119 * 120 * 121 * Results: 122 * 123 * 124 * Side Effects: 125 * 120 * Add a number of bytes to the buffer. 121 * 122 * Results: 123 * None. 124 * 125 * Side Effects: 126 * Guess what? 126 127 * 127 128 *----------------------------------------------------------------------- … … 130 131 Buf_AddBytes (bp, numBytes, bytesPtr) 131 132 register Buffer bp; 132 int 133 int numBytes; 133 134 const Byte *bytesPtr; 134 135 { … … 150 151 *----------------------------------------------------------------------- 151 152 * Buf_UngetByte -- 152 * 153 * 154 * Results: 155 * 156 * 157 * Side Effects: 158 * 153 * Place the byte back at the beginning of the buffer. 154 * 155 * Results: 156 * SUCCESS if the byte was added ok. FAILURE if not. 157 * 158 * Side Effects: 159 * The byte is stuffed in the buffer and outPtr is decremented. 159 160 * 160 161 *----------------------------------------------------------------------- … … 167 168 168 169 if (bp->outPtr != bp->buffer) { 169 170 170 bp->outPtr--; 171 *bp->outPtr = byte; 171 172 } else if (bp->outPtr == bp->inPtr) { 172 173 174 175 173 *bp->inPtr = byte; 174 bp->inPtr++; 175 bp->left--; 176 *bp->inPtr = 0; 176 177 } else { 177 178 179 180 181 182 183 intnumBytes = bp->inPtr - bp->outPtr;184 Byte*newBuf;185 186 187 188 189 190 191 192 193 194 195 178 /* 179 * Yech. have to expand the buffer to stuff this thing in. 180 * We use a different expansion constant because people don't 181 * usually push back many bytes when they're doing it a byte at 182 * a time... 183 */ 184 int numBytes = bp->inPtr - bp->outPtr; 185 Byte *newBuf; 186 187 newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC); 188 memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1); 189 bp->outPtr = newBuf + BUF_UNGET_INC; 190 bp->inPtr = bp->outPtr + numBytes; 191 efree ((char *)bp->buffer); 192 bp->buffer = newBuf; 193 bp->size += BUF_UNGET_INC; 194 bp->left = bp->size - (bp->inPtr - bp->buffer); 195 bp->outPtr -= 1; 196 *bp->outPtr = byte; 196 197 } 197 198 } … … 201 202 *----------------------------------------------------------------------- 202 203 * Buf_UngetBytes -- 203 * 204 * 205 * Results: 206 * 207 * 208 * Side Effects: 209 * 204 * Push back a series of bytes at the beginning of the buffer. 205 * 206 * Results: 207 * None. 208 * 209 * Side Effects: 210 * outPtr is decremented and the bytes copied into the buffer. 210 211 * 211 212 *----------------------------------------------------------------------- … … 214 215 Buf_UngetBytes (bp, numBytes, bytesPtr) 215 216 register Buffer bp; 216 int 217 int numBytes; 217 218 Byte *bytesPtr; 218 219 { 219 220 220 221 if (bp->outPtr - bp->buffer >= numBytes) { 221 222 222 bp->outPtr -= numBytes; 223 memcpy (bp->outPtr, bytesPtr, numBytes); 223 224 } else if (bp->outPtr == bp->inPtr) { 224 225 Buf_AddBytes (bp, numBytes, bytesPtr); 225 226 } else { 226 intcurNumBytes = bp->inPtr - bp->outPtr;227 Byte*newBuf;228 intnewBytes = max(numBytes,BUF_UNGET_INC);229 230 231 232 233 234 235 236 237 238 239 227 int curNumBytes = bp->inPtr - bp->outPtr; 228 Byte *newBuf; 229 int newBytes = max(numBytes,BUF_UNGET_INC); 230 231 newBuf = (Byte *)emalloc (bp->size + newBytes); 232 memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1); 233 bp->outPtr = newBuf + newBytes; 234 bp->inPtr = bp->outPtr + curNumBytes; 235 efree ((char *)bp->buffer); 236 bp->buffer = newBuf; 237 bp->size += newBytes; 238 bp->left = bp->size - (bp->inPtr - bp->buffer); 239 bp->outPtr -= numBytes; 240 memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes); 240 241 } 241 242 } … … 245 246 *----------------------------------------------------------------------- 246 247 * Buf_GetByte -- 247 * 248 * 249 * Results: 250 * 251 * 252 * 253 * Side Effects: 254 * 255 * 248 * Return the next byte from the buffer. Actually returns an integer. 249 * 250 * Results: 251 * Returns BUF_ERROR if there's no byte in the buffer, or the byte 252 * itself if there is one. 253 * 254 * Side Effects: 255 * outPtr is incremented and both outPtr and inPtr will be reset if 256 * the buffer is emptied. 256 257 * 257 258 *----------------------------------------------------------------------- … … 261 262 register Buffer bp; 262 263 { 263 int 264 int res; 264 265 265 266 if (bp->inPtr == bp->outPtr) { 266 267 return (BUF_ERROR); 267 268 } else { 268 269 270 271 272 273 274 275 269 res = (int) *bp->outPtr; 270 bp->outPtr += 1; 271 if (bp->outPtr == bp->inPtr) { 272 bp->outPtr = bp->inPtr = bp->buffer; 273 bp->left = bp->size; 274 *bp->inPtr = 0; 275 } 276 return (res); 276 277 } 277 278 } … … 281 282 *----------------------------------------------------------------------- 282 283 * Buf_GetBytes -- 283 * 284 * 285 * Results: 286 * 287 * 288 * Side Effects: 289 * 284 * Extract a number of bytes from the buffer. 285 * 286 * Results: 287 * The number of bytes gotten. 288 * 289 * Side Effects: 290 * The passed array is overwritten. 290 291 * 291 292 *----------------------------------------------------------------------- … … 294 295 Buf_GetBytes (bp, numBytes, bytesPtr) 295 296 register Buffer bp; 296 int 297 int numBytes; 297 298 Byte *bytesPtr; 298 299 { 299 300 300 301 if (bp->inPtr - bp->outPtr < numBytes) { 301 302 numBytes = bp->inPtr - bp->outPtr; 302 303 } 303 304 memcpy (bytesPtr, bp->outPtr, numBytes); … … 305 306 306 307 if (bp->outPtr == bp->inPtr) { 307 308 309 308 bp->outPtr = bp->inPtr = bp->buffer; 309 bp->left = bp->size; 310 *bp->inPtr = 0; 310 311 } 311 312 return (numBytes); … … 316 317 *----------------------------------------------------------------------- 317 318 * Buf_GetAll -- 318 * 319 * 320 * Results: 321 * 322 * 323 * Side Effects: 324 * 319 * Get all the available data at once. 320 * 321 * Results: 322 * A pointer to the data and the number of bytes available. 323 * 324 * Side Effects: 325 * None. 325 326 * 326 327 *----------------------------------------------------------------------- … … 329 330 Buf_GetAll (bp, numBytesPtr) 330 331 register Buffer bp; 331 int 332 int *numBytesPtr; 332 333 { 333 334 334 335 if (numBytesPtr != (int *)NULL) { 335 336 *numBytesPtr = bp->inPtr - bp->outPtr; 336 337 } 337 338 … … 343 344 *----------------------------------------------------------------------- 344 345 * Buf_Discard -- 345 * 346 * 347 * Results: 348 * 349 * 350 * Side Effects: 351 * 346 * Throw away bytes in a buffer. 347 * 348 * Results: 349 * None. 350 * 351 * Side Effects: 352 * The bytes are discarded. 352 353 * 353 354 *----------------------------------------------------------------------- … … 356 357 Buf_Discard (bp, numBytes) 357 358 register Buffer bp; 358 int 359 int numBytes; 359 360 { 360 361 361 362 if (bp->inPtr - bp->outPtr <= numBytes) { 362 363 364 363 bp->inPtr = bp->outPtr = bp->buffer; 364 bp->left = bp->size; 365 *bp->inPtr = 0; 365 366 } else { 366 367 bp->outPtr += numBytes; 367 368 } 368 369 } … … 372 373 *----------------------------------------------------------------------- 373 374 * Buf_Size -- 374 * 375 * 376 * 377 * Results: 378 * 379 * 380 * Side Effects: 381 * 375 * Returns the number of bytes in the given buffer. Doesn't include 376 * the null-terminating byte. 377 * 378 * Results: 379 * The number of bytes. 380 * 381 * Side Effects: 382 * None. 382 383 * 383 384 *----------------------------------------------------------------------- … … 394 395 *----------------------------------------------------------------------- 395 396 * Buf_Init -- 396 * 397 * 398 * 399 * Results: 400 * 401 * 402 * Side Effects: 403 * 404 * 397 * Initialize a buffer. If no initial size is given, a reasonable 398 * default is used. 399 * 400 * Results: 401 * A buffer to be given to other functions in this library. 402 * 403 * Side Effects: 404 * The buffer is created, the space allocated and pointers 405 * initialized. 405 406 * 406 407 *----------------------------------------------------------------------- … … 408 409 Buffer 409 410 Buf_Init (size) 410 int size;/* Initial size for the buffer */411 { 412 Buffer bp; 411 int size; /* Initial size for the buffer */ 412 { 413 Buffer bp; /* New Buffer */ 413 414 414 415 bp = (Buffer)emalloc(sizeof(*bp)); 415 416 416 417 if (size <= 0) { 417 418 size = BUF_DEF_SIZE; 418 419 } 419 420 bp->left = bp->size = size; … … 429 430 *----------------------------------------------------------------------- 430 431 * Buf_Destroy -- 431 * 432 * 433 * Results: 434 * 435 * 436 * Side Effects: 437 * 432 * Nuke a buffer and all its resources. 433 * 434 * Results: 435 * None. 436 * 437 * Side Effects: 438 * The buffer is freed. 438 439 * 439 440 *----------------------------------------------------------------------- … … 441 442 void 442 443 Buf_Destroy (buf, freeData) 443 Buffer buf; 444 Boolean freeData; 444 Buffer buf; /* Buffer to destroy */ 445 Boolean freeData; /* TRUE if the data should be destroyed as well */ 445 446 { 446 447 447 448 if (freeData) { 448 449 efree ((char *)buf->buffer); 449 450 } 450 451 efree ((char *)buf); … … 468 469 void 469 470 Buf_ReplaceLastByte (buf, byte) 470 Buffer buf; 471 int byte; 471 Buffer buf; /* buffer to augment */ 472 int byte; /* byte to be written */ 472 473 { 473 474 if (buf->inPtr == buf->outPtr)
Note:
See TracChangeset
for help on using the changeset viewer.