?? buffer.h
字號:
#define BASE_BUFFER_SIZE 1024
#define BASE_BUFFER_INC 512
class CAutoBuffer
{
protected:
char *buffer; // Yeah, this is our buffer
int nLength; // Current length of data
int nSize; // Current size of buffer
int nIncrement; // By how much we should increment
// when buffer is too small
/*
The IncreaseSize(..) function re-allocates memory space for the
buffer and copies its current contents into the new memory space.
*/
__forceinline void IncreaseSize(int nIncrement_)
{
char *newbuf;
newbuf = new char [nSize + nIncrement_];
memcpy(newbuf, buffer, nSize);
delete [] buffer;
buffer = newbuf;
nSize += nIncrement_;
}
__forceinline void IncreaseSize()
{
IncreaseSize(nIncrement);
}
public:
CAutoBuffer(int nIncrement_) : buffer(NULL),
nLength(0),
nIncrement(nIncrement_)
{
buffer = new char [BASE_BUFFER_SIZE]; // Allocate memory
nSize = BASE_BUFFER_SIZE;
}
~CAutoBuffer()
{
if (buffer)
delete [] buffer; // De-allocate memory
}
__forceinline int GetLength(){return nLength;}
/*
I assume that memory can be allocated, and that the new keyword
never returns NULL. I didn't embed error-detection in order to
enhance performance. If there is not enough memory to be allocated,
something is terribly wrong.
Furthermore, no internal memory checks are performed inside the
functions.
*/
/*
AddToBuffer(..) adds data from a source buffer to the class' buffer.
src: the source buffer
len: the source buffer's size
*/
__forceinline void AddToBuffer(const char *src, int len)
{
if (len > 0)
{
if (len + nLength > nSize)
IncreaseSize(BASE_BUFFER_INC > len ? BASE_BUFFER_INC : len);
memcpy(buffer + nLength, src, len);
nLength += len;
}
}
/*
AddToBufferBeg(..) adds data from a source buffer to the beginning
of the class' buffer.
*/
__forceinline void AddToBufferBeg(const char *src, int len)
{
if (len + nLength > nSize)
IncreaseSize(len);
memmove(buffer + len, buffer, nLength);
memcpy(buffer, src, len);
nLength += len;
}
/*
GetBuffer(..) copies data from the class' buffer to a destination
buffer. dst is a pointer to the destination buffer. len is a pointer
to an integer that specifies the size of dst. The value to which len
points is changes by the function, to reflect the number of bytes
transferred from the class' buffer to the destination buffer.
*/
__forceinline void GetBuffer(char *dst, int start, int len)
{
if (nLength > (start+len))
{ // Supplied buffer is not large enough.
GetFromBuffer(dst,start, len);
}
else if (nLength > start)
{
memcpy(dst, buffer+start, nLength);
len = nLength-start;
//nLength = 0; // Clear buffer;
}
}
__forceinline void GetFromBuffer(char *dst,int start, int len)
{
if ((len+start) > nLength)GetBuffer(dst, start,len);
else memcpy(dst, buffer+start, len);
}
__forceinline char *GetBuffer()
{
return buffer;
}
__forceinline bool IsEmpty()
{
return (nLength == 0);
}
__forceinline void ReSet(){
if (buffer)
delete [] buffer;
buffer = new char [BASE_BUFFER_SIZE]; // Allocate memory
nSize = BASE_BUFFER_SIZE;
nLength = 0;
}
};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -