- UID
- 1029342
- 性别
- 男
|
return size;
}
int MyBuf::read(int size, char* buf)
{
assert(size > 0);
if (size > m_size) return -1;
int n = getBufferEnd() - m_head;
if (n > = size){
memcpy(buf, m_head, size);
m_head += size;
}else{
memcpy(buf, m_head, n);
memcpy(buf + n, m_buf, size - n);
m_head = m_buf + size - n;
}
m_size -= size;
if (m_size == 0) m_head = m_buf;
return size;
}
int main()
{
MyBuf buf(12);
char tmp[100];
buf.write( "abchello ", (int)sizeof( "abchello ") - 1);
buf.read(tmp, 3);
tmp[3] = 0;
cout < < tmp < < endl;
buf.write( " world! ", (int)sizeof( " world! ") - 1);
buf.read(tmp, 12);
tmp[12] = 0;
cout < < tmp < < endl;
return 0;
}
#define BUFFER_SIZE (unsigned long )10000000
unsigned char BUFFER[BUFFER_SIZE] = {0};
unsigned char *pBufferFront = (unsigned char *)BUFFER;
unsigned char *pBuffereEnd = (unsigned char *)BUFFER +BUFFER_SIZE -1;
unsigned char *pBufferCur = pBufferFront;
unsigned char Buffer_Put(unsigned long put_size,unsigned char *put_pointer)
{
unsigned long current_size;
unsigned long count;
if(put_size > (pBufferCur - pBufferFront))
return FALSE;
for(count = 0;count <put_size;count++)
*pBufferCur-- = *put_pointer++;
}
unsigned char Buffer_Get(unsigned long get_size,unsigned char *get_pointer)
{
unsigned long count;
unsigned char *move_pointer = pBuffereEnd;
if(get_size> BUFFER_SIZE- pBufferCur)
return FALSE;
for(count = 0;count <get_size;count++)
*get_pointer++ = *move_pointer--;
for(count = 0,pBufferCur = pBuffereEnd;count < move_pointer - pBufferCur;count++)
*pBufferCur-- = *move_pointer--;
return TRUE;
} |
|