//环形缓冲区头文件
#ifndef CCycleBuffer_H
#define CCycleBuffer_H
class CCycleBuffer
{
public:
bool isFull();
bool isEmpty();
void empty();
int getLength();
CCycleBuffer(int size);
virtual~CCycleBuffer();
int write(char* buf,int count);
int read(char* buf,int count);
int getStart()
{
return m_nReadPos;
}
int getEnd()
{
return m_nWritePos;
}
private:
bool m_bEmpty, m_bFull;
char* m_pBuf;
int m_nBufSize;
int m_nReadPos;
int m_nWritePos;
int test;
};
#endif// CCycleBuffer_H
CCycleBuffer.c
//环形缓冲区源文件
//尽量用C运行时库代码,改变了原作者基于WindowsAPI的代码。
//修改了原作者的两处错误,1是read函数最后的else 少了一个leftcount
//第二个错误,是write函数中,m_nWritePos 变量,他重新定义了一个。这些错误编译不会出错。
//但是运行起来,要了亲命啊。
#include "CCycleBuffer.h"
#include
#include
#include
// 定义
CCycleBuffer::CCycleBuffer(int size)
{
m_nBufSize = size;
m_nReadPos =0;
m_nWritePos =0;
m_pBuf =newchar[m_nBufSize];
m_bEmpty =true;
m_bFull =false;
test =0;
}
CCycleBuffer::~CCycleBuffer()
{
delete[] m_pBuf;
}
/************************************************************************/
/* 向缓冲区写入数据,返回实际写入的字节数 */
/************************************************************************/
int CCycleBuffer::write(char* buf,int count)
{
if(count <=0)
return0;
m_bEmpty =false;
<