#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 (WritePReadP) 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) { 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()