#include "stdafx.h"
|
#include "MyCircularBuf.h"
|
//CMyCircularBuf
|
CMyCircularBuf::CMyCircularBuf()
|
{
|
pBuffer=NULL;
|
nDataSize=0;
|
bIsEmpty=true;
|
bIsFull=false;
|
}
|
CMyCircularBuf::CMyCircularBuf(int nCapicity)
|
{
|
Alloc(nCapicity);
|
}
|
CMyCircularBuf::~CMyCircularBuf()
|
{
|
if (pBuffer!=NULL) {delete [] pBuffer;pBuffer=NULL;}
|
}
|
int CMyCircularBuf::Alloc(int nCapicity)
|
{
|
if (pBuffer!=NULL) {delete [] pBuffer;pBuffer=NULL;}
|
pBuffer=new char[nCapicity];
|
nmCapacity=nCapicity;
|
nDataSize=0;
|
ReadP=0;WriteP=0;
|
bIsEmpty=true;
|
bIsFull=false;
|
return nCapicity;
|
}
|
int CMyCircularBuf::GetCapicity()
|
{
|
return nmCapacity;
|
}
|
int CMyCircularBuf::GetNextWriteP(int nOffset)
|
{
|
int nextip=WriteP+nOffset;
|
if (nextip>=nmCapacity) {nextip-=nmCapacity;}
|
return nextip;
|
}
|
int CMyCircularBuf::GetNextReadP(int nOffset)
|
{
|
int nextop=ReadP+nOffset;
|
if (nextop>=nmCapacity) {nextop-=nmCapacity;}
|
return nextop;
|
}
|
int CMyCircularBuf::GetTailRoomSize()
|
{
|
int nTailLeftSize;
|
if (bIsFull) {nTailLeftSize=0; return nTailLeftSize;}
|
if (WriteP<ReadP) {nTailLeftSize=ReadP-WriteP;}
|
else {nTailLeftSize=nmCapacity-WriteP;}
|
return nTailLeftSize;
|
}
|
int CMyCircularBuf::GetTailDataSize()
|
{
|
int nTailLeftDataSize;
|
if (bIsEmpty) return 0;
|
if (WriteP>ReadP) nTailLeftDataSize=WriteP-ReadP;
|
else {nTailLeftDataSize=nmCapacity-ReadP;}
|
return nTailLeftDataSize;
|
}
|
int CMyCircularBuf::GetLeftRoomSize()
|
{
|
int nLeftSize=nmCapacity-nDataSize;
|
return nLeftSize;
|
}
|
int CMyCircularBuf::GetDataSize()
|
{
|
return nDataSize;
|
}
|
char * CMyCircularBuf::GetWriteBuffer(int * LeftRoomSize)
|
{
|
int nTailLeftRoomSize=GetTailRoomSize();
|
*LeftRoomSize=nTailLeftRoomSize;
|
return pBuffer+WriteP;
|
}
|
int CMyCircularBuf::ReleaseWriteBuffer(int AddSize)
|
{
|
WriteP=GetNextWriteP(AddSize);
|
nDataSize+=AddSize;
|
bIsEmpty=false;
|
if (nDataSize>=nmCapacity) bIsFull=true;
|
|
return AddSize;
|
}
|
char * CMyCircularBuf::GetReadBuffer(int * DataSize)
|
{
|
int nTailLeftDataSize=GetTailDataSize();
|
*DataSize=nTailLeftDataSize;
|
return pBuffer+ReadP;
|
}
|
int CMyCircularBuf::ReleaseReadBuffer(int DelSize)
|
{
|
DelData(DelSize);
|
return DelSize;
|
}
|
|
int CMyCircularBuf::PushIn(void * pData,int Size)
|
{
|
if (GetLeftRoomSize()<Size) {return -1;}
|
int nTailLeftSize=GetTailRoomSize();
|
if (nTailLeftSize>=Size)
|
{
|
memcpy_s(pBuffer+WriteP,nTailLeftSize,pData,Size);
|
WriteP=GetNextWriteP(Size);
|
}else
|
{
|
memcpy_s(pBuffer+WriteP,nTailLeftSize,pData,nTailLeftSize);
|
memcpy_s(pBuffer,Size-nTailLeftSize,(char *)pData+nTailLeftSize,Size-nTailLeftSize);
|
WriteP=GetNextWriteP(Size);
|
}
|
bIsEmpty=false;
|
nDataSize+=Size;
|
if (nDataSize==nmCapacity) {bIsFull=true;}
|
return Size;
|
}
|
int CMyCircularBuf::CopyData(void * pData,int Size)
|
{
|
if (GetDataSize()<Size) {return -1;}
|
int nTailLeftSize=GetTailDataSize(); //»·Ðλº³åβ²¿Ê£ÓàÊý¾ÝÁ¿
|
if (nTailLeftSize<Size) ////Ê£Óà¿Õ¼äС£¬½«Êý¾ÝתÒƵ½Ò»Æð
|
{
|
memcpy_s(pData,Size,pBuffer+ReadP,nTailLeftSize);
|
memcpy_s((char *)pData+nTailLeftSize,Size-nTailLeftSize,pBuffer,Size-nTailLeftSize);
|
}else
|
{
|
memcpy_s(pData,Size,pBuffer+ReadP,Size);
|
}
|
return Size;
|
}
|
int CMyCircularBuf::DelData(int Size)
|
{
|
if (GetDataSize()<Size) {return -1;}
|
bIsFull=false;
|
ReadP=GetNextReadP(Size);
|
nDataSize-=Size;
|
if (nDataSize==0) {bIsEmpty=true;}
|
return Size;
|
}
|
|
int CMyCircularBuf::PopOut(void * pData,int Size)
|
{
|
CopyData(pData,Size);
|
DelData(Size);
|
return Size;
|
}
|