Style: fix indentation, protect macro with do { } while (0).
Checked with: diff on object file.
This commit is contained in:
parent
161ad64e63
commit
002c565593
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=138337
@ -47,9 +47,9 @@ __FBSDID("$FreeBSD$");
|
||||
* Functions for automatically-expanded buffers.
|
||||
*/
|
||||
|
||||
#include "sprite.h"
|
||||
#include "make.h"
|
||||
#include "buf.h"
|
||||
#include "sprite.h"
|
||||
#include "make.h"
|
||||
#include "buf.h"
|
||||
|
||||
#ifndef max
|
||||
#define max(a,b) ((a) > (b) ? (a) : (b))
|
||||
@ -62,17 +62,18 @@ __FBSDID("$FreeBSD$");
|
||||
* Makes sure there's room for an extra NULL byte at the end of the
|
||||
* buffer in case it holds a string.
|
||||
*/
|
||||
#define BufExpand(bp,nb) \
|
||||
if (bp->left < (nb)+1) {\
|
||||
int newSize = (bp)->size + max((nb) + 1, BUF_ADD_INC); \
|
||||
Byte *newBuf = erealloc((bp)->buffer, newSize); \
|
||||
\
|
||||
(bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
|
||||
(bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
|
||||
(bp)->buffer = newBuf;\
|
||||
(bp)->size = newSize;\
|
||||
(bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
|
||||
}
|
||||
#define BufExpand(bp, nb) do { \
|
||||
if (bp->left < (nb) + 1) { \
|
||||
int newSize = (bp)->size + max((nb) + 1, BUF_ADD_INC); \
|
||||
Byte *newBuf = erealloc((bp)->buffer, newSize); \
|
||||
\
|
||||
(bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
|
||||
(bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer); \
|
||||
(bp)->buffer = newBuf; \
|
||||
(bp)->size = newSize; \
|
||||
(bp)->left = newSize - ((bp)->inPtr - (bp)->buffer); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define BUF_DEF_SIZE 256 /* Default buffer size */
|
||||
#define BUF_ADD_INC 256 /* Expansion increment when Adding */
|
||||
@ -95,16 +96,16 @@ void
|
||||
Buf_OvAddByte(Buffer bp, int byte)
|
||||
{
|
||||
|
||||
bp->left = 0;
|
||||
BufExpand(bp, 1);
|
||||
bp->left = 0;
|
||||
BufExpand(bp, 1);
|
||||
|
||||
*bp->inPtr++ = byte;
|
||||
bp->left--;
|
||||
*bp->inPtr++ = byte;
|
||||
bp->left--;
|
||||
|
||||
/*
|
||||
* Null-terminate
|
||||
*/
|
||||
*bp->inPtr = 0;
|
||||
/*
|
||||
* Null-terminate
|
||||
*/
|
||||
*bp->inPtr = 0;
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -124,16 +125,16 @@ void
|
||||
Buf_AddBytes(Buffer bp, int numBytes, const Byte *bytesPtr)
|
||||
{
|
||||
|
||||
BufExpand(bp, numBytes);
|
||||
BufExpand(bp, numBytes);
|
||||
|
||||
memcpy(bp->inPtr, bytesPtr, numBytes);
|
||||
bp->inPtr += numBytes;
|
||||
bp->left -= numBytes;
|
||||
memcpy(bp->inPtr, bytesPtr, numBytes);
|
||||
bp->inPtr += numBytes;
|
||||
bp->left -= numBytes;
|
||||
|
||||
/*
|
||||
* Null-terminate
|
||||
*/
|
||||
*bp->inPtr = 0;
|
||||
/*
|
||||
* Null-terminate
|
||||
*/
|
||||
*bp->inPtr = 0;
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -153,35 +154,37 @@ void
|
||||
Buf_UngetByte(Buffer bp, int byte)
|
||||
{
|
||||
|
||||
if (bp->outPtr != bp->buffer) {
|
||||
bp->outPtr--;
|
||||
*bp->outPtr = byte;
|
||||
} else if (bp->outPtr == bp->inPtr) {
|
||||
*bp->inPtr = byte;
|
||||
bp->inPtr++;
|
||||
bp->left--;
|
||||
*bp->inPtr = 0;
|
||||
} else {
|
||||
/*
|
||||
* Yech. have to expand the buffer to stuff this thing in.
|
||||
* We use a different expansion constant because people don't
|
||||
* usually push back many bytes when they're doing it a byte at
|
||||
* a time...
|
||||
*/
|
||||
int numBytes = bp->inPtr - bp->outPtr;
|
||||
Byte *newBuf;
|
||||
if (bp->outPtr != bp->buffer) {
|
||||
bp->outPtr--;
|
||||
*bp->outPtr = byte;
|
||||
|
||||
newBuf = emalloc(bp->size + BUF_UNGET_INC);
|
||||
memcpy(newBuf + BUF_UNGET_INC, bp->outPtr, numBytes + 1);
|
||||
bp->outPtr = newBuf + BUF_UNGET_INC;
|
||||
bp->inPtr = bp->outPtr + numBytes;
|
||||
free(bp->buffer);
|
||||
bp->buffer = newBuf;
|
||||
bp->size += BUF_UNGET_INC;
|
||||
bp->left = bp->size - (bp->inPtr - bp->buffer);
|
||||
bp->outPtr -= 1;
|
||||
*bp->outPtr = byte;
|
||||
}
|
||||
} else if (bp->outPtr == bp->inPtr) {
|
||||
*bp->inPtr = byte;
|
||||
bp->inPtr++;
|
||||
bp->left--;
|
||||
*bp->inPtr = 0;
|
||||
|
||||
} else {
|
||||
/*
|
||||
* Yech. have to expand the buffer to stuff this thing in.
|
||||
* We use a different expansion constant because people don't
|
||||
* usually push back many bytes when they're doing it a byte at
|
||||
* a time...
|
||||
*/
|
||||
int numBytes = bp->inPtr - bp->outPtr;
|
||||
Byte *newBuf;
|
||||
|
||||
newBuf = emalloc(bp->size + BUF_UNGET_INC);
|
||||
memcpy(newBuf + BUF_UNGET_INC, bp->outPtr, numBytes + 1);
|
||||
bp->outPtr = newBuf + BUF_UNGET_INC;
|
||||
bp->inPtr = bp->outPtr + numBytes;
|
||||
free(bp->buffer);
|
||||
bp->buffer = newBuf;
|
||||
bp->size += BUF_UNGET_INC;
|
||||
bp->left = bp->size - (bp->inPtr - bp->buffer);
|
||||
bp->outPtr -= 1;
|
||||
*bp->outPtr = byte;
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -201,27 +204,27 @@ void
|
||||
Buf_UngetBytes(Buffer bp, int numBytes, Byte *bytesPtr)
|
||||
{
|
||||
|
||||
if (bp->outPtr - bp->buffer >= numBytes) {
|
||||
bp->outPtr -= numBytes;
|
||||
memcpy(bp->outPtr, bytesPtr, numBytes);
|
||||
} else if (bp->outPtr == bp->inPtr) {
|
||||
Buf_AddBytes(bp, numBytes, bytesPtr);
|
||||
} else {
|
||||
int curNumBytes = bp->inPtr - bp->outPtr;
|
||||
Byte *newBuf;
|
||||
int newBytes = max(numBytes,BUF_UNGET_INC);
|
||||
if (bp->outPtr - bp->buffer >= numBytes) {
|
||||
bp->outPtr -= numBytes;
|
||||
memcpy(bp->outPtr, bytesPtr, numBytes);
|
||||
} else if (bp->outPtr == bp->inPtr) {
|
||||
Buf_AddBytes(bp, numBytes, bytesPtr);
|
||||
} else {
|
||||
int curNumBytes = bp->inPtr - bp->outPtr;
|
||||
Byte *newBuf;
|
||||
int newBytes = max(numBytes, BUF_UNGET_INC);
|
||||
|
||||
newBuf = emalloc(bp->size + newBytes);
|
||||
memcpy(newBuf + newBytes, bp->outPtr, curNumBytes + 1);
|
||||
bp->outPtr = newBuf + newBytes;
|
||||
bp->inPtr = bp->outPtr + curNumBytes;
|
||||
free(bp->buffer);
|
||||
bp->buffer = newBuf;
|
||||
bp->size += newBytes;
|
||||
bp->left = bp->size - (bp->inPtr - bp->buffer);
|
||||
bp->outPtr -= numBytes;
|
||||
memcpy(bp->outPtr, bytesPtr, numBytes);
|
||||
}
|
||||
newBuf = emalloc(bp->size + newBytes);
|
||||
memcpy(newBuf + newBytes, bp->outPtr, curNumBytes + 1);
|
||||
bp->outPtr = newBuf + newBytes;
|
||||
bp->inPtr = bp->outPtr + curNumBytes;
|
||||
free(bp->buffer);
|
||||
bp->buffer = newBuf;
|
||||
bp->size += newBytes;
|
||||
bp->left = bp->size - (bp->inPtr - bp->buffer);
|
||||
bp->outPtr -= numBytes;
|
||||
memcpy(bp->outPtr, bytesPtr, numBytes);
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -242,20 +245,19 @@ Buf_UngetBytes(Buffer bp, int numBytes, Byte *bytesPtr)
|
||||
int
|
||||
Buf_GetByte(Buffer bp)
|
||||
{
|
||||
int res;
|
||||
int res;
|
||||
|
||||
if (bp->inPtr == bp->outPtr)
|
||||
return (BUF_ERROR);
|
||||
|
||||
if (bp->inPtr == bp->outPtr) {
|
||||
return (BUF_ERROR);
|
||||
} else {
|
||||
res = (int)*bp->outPtr;
|
||||
bp->outPtr += 1;
|
||||
if (bp->outPtr == bp->inPtr) {
|
||||
bp->outPtr = bp->inPtr = bp->buffer;
|
||||
bp->left = bp->size;
|
||||
*bp->inPtr = 0;
|
||||
bp->outPtr = bp->inPtr = bp->buffer;
|
||||
bp->left = bp->size;
|
||||
*bp->inPtr = 0;
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -275,18 +277,18 @@ int
|
||||
Buf_GetBytes(Buffer bp, int numBytes, Byte *bytesPtr)
|
||||
{
|
||||
|
||||
if (bp->inPtr - bp->outPtr < numBytes) {
|
||||
numBytes = bp->inPtr - bp->outPtr;
|
||||
}
|
||||
memcpy(bytesPtr, bp->outPtr, numBytes);
|
||||
bp->outPtr += numBytes;
|
||||
if (bp->inPtr - bp->outPtr < numBytes)
|
||||
numBytes = bp->inPtr - bp->outPtr;
|
||||
|
||||
if (bp->outPtr == bp->inPtr) {
|
||||
bp->outPtr = bp->inPtr = bp->buffer;
|
||||
bp->left = bp->size;
|
||||
*bp->inPtr = 0;
|
||||
}
|
||||
return (numBytes);
|
||||
memcpy(bytesPtr, bp->outPtr, numBytes);
|
||||
bp->outPtr += numBytes;
|
||||
|
||||
if (bp->outPtr == bp->inPtr) {
|
||||
bp->outPtr = bp->inPtr = bp->buffer;
|
||||
bp->left = bp->size;
|
||||
*bp->inPtr = 0;
|
||||
}
|
||||
return (numBytes);
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -306,11 +308,10 @@ Byte *
|
||||
Buf_GetAll(Buffer bp, int *numBytesPtr)
|
||||
{
|
||||
|
||||
if (numBytesPtr != NULL) {
|
||||
*numBytesPtr = bp->inPtr - bp->outPtr;
|
||||
}
|
||||
if (numBytesPtr != NULL)
|
||||
*numBytesPtr = bp->inPtr - bp->outPtr;
|
||||
|
||||
return (bp->outPtr);
|
||||
return (bp->outPtr);
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -330,13 +331,12 @@ void
|
||||
Buf_Discard(Buffer bp, int numBytes)
|
||||
{
|
||||
|
||||
if (bp->inPtr - bp->outPtr <= numBytes) {
|
||||
bp->inPtr = bp->outPtr = bp->buffer;
|
||||
bp->left = bp->size;
|
||||
*bp->inPtr = 0;
|
||||
} else {
|
||||
bp->outPtr += numBytes;
|
||||
}
|
||||
if (bp->inPtr - bp->outPtr <= numBytes) {
|
||||
bp->inPtr = bp->outPtr = bp->buffer;
|
||||
bp->left = bp->size;
|
||||
*bp->inPtr = 0;
|
||||
} else
|
||||
bp->outPtr += numBytes;
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -357,7 +357,7 @@ int
|
||||
Buf_Size(Buffer buf)
|
||||
{
|
||||
|
||||
return (buf->inPtr - buf->outPtr);
|
||||
return (buf->inPtr - buf->outPtr);
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -378,19 +378,19 @@ Buf_Size(Buffer buf)
|
||||
Buffer
|
||||
Buf_Init(int size)
|
||||
{
|
||||
Buffer bp; /* New Buffer */
|
||||
Buffer bp; /* New Buffer */
|
||||
|
||||
bp = emalloc(sizeof(*bp));
|
||||
bp = emalloc(sizeof(*bp));
|
||||
|
||||
if (size <= 0) {
|
||||
size = BUF_DEF_SIZE;
|
||||
}
|
||||
bp->left = bp->size = size;
|
||||
bp->buffer = emalloc(size);
|
||||
bp->inPtr = bp->outPtr = bp->buffer;
|
||||
*bp->inPtr = 0;
|
||||
if (size <= 0)
|
||||
size = BUF_DEF_SIZE;
|
||||
|
||||
return (bp);
|
||||
bp->left = bp->size = size;
|
||||
bp->buffer = emalloc(size);
|
||||
bp->inPtr = bp->outPtr = bp->buffer;
|
||||
*bp->inPtr = 0;
|
||||
|
||||
return (bp);
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -410,10 +410,9 @@ void
|
||||
Buf_Destroy(Buffer buf, Boolean freeData)
|
||||
{
|
||||
|
||||
if (freeData) {
|
||||
free(buf->buffer);
|
||||
}
|
||||
free(buf);
|
||||
if (freeData)
|
||||
free(buf->buffer);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/*-
|
||||
@ -433,8 +432,8 @@ Buf_Destroy(Buffer buf, Boolean freeData)
|
||||
void
|
||||
Buf_ReplaceLastByte(Buffer buf, int byte)
|
||||
{
|
||||
if (buf->inPtr == buf->outPtr)
|
||||
Buf_AddByte(buf, byte);
|
||||
else
|
||||
*(buf->inPtr - 1) = byte;
|
||||
if (buf->inPtr == buf->outPtr)
|
||||
Buf_AddByte(buf, byte);
|
||||
else
|
||||
*(buf->inPtr - 1) = byte;
|
||||
}
|
||||
|
@ -48,16 +48,16 @@
|
||||
#ifndef _BUF_H
|
||||
#define _BUF_H
|
||||
|
||||
#include "sprite.h"
|
||||
#include "sprite.h"
|
||||
|
||||
typedef char Byte;
|
||||
|
||||
typedef struct Buffer {
|
||||
int size; /* Current size of the buffer */
|
||||
int left; /* Space left (== size - (inPtr - buffer)) */
|
||||
Byte *buffer; /* The buffer itself */
|
||||
Byte *inPtr; /* Place to write to */
|
||||
Byte *outPtr; /* Place to read from */
|
||||
int size; /* Current size of the buffer */
|
||||
int left; /* Space left (== size - (inPtr - buffer)) */
|
||||
Byte *buffer; /* The buffer itself */
|
||||
Byte *inPtr; /* Place to write to */
|
||||
Byte *outPtr; /* Place to read from */
|
||||
} *Buffer;
|
||||
|
||||
/* Buf_AddByte adds a single byte to a buffer. */
|
||||
|
Loading…
Reference in New Issue
Block a user