/** ****************************************************************************** * @file : MyQueue.c * @brief : MyQueue program body ****************************************************************************** */ #include "../inc/MyQueue.h" #include "string.h" int initQueue(stMyQueue * theQueue, void * pBuf, int nSize) { theQueue->buf1=pBuf; theQueue->Caps=nSize; theQueue->wp=0; theQueue->rp=0; // theQueue->RecvBytes=0; //theQueue->state=0; theQueue->bFull=0; theQueue->bEmpty=1; return 0; } int EmptyQueue(stMyQueue * theQueue) { theQueue->wp=0; theQueue->rp=0; //theQueue->state=0; theQueue->bFull=0; theQueue->bEmpty=1; return 0; } int GetContinueEmptyRoom(stMyQueue * theQueue) { int rp=theQueue->rp; int wp=theQueue->wp; if (wp < rp) { return rp - wp; }else if (wp > rp) { return theQueue->Caps - wp; }else if (IsEmpty(theQueue)) { return theQueue->Caps - wp; }else if (IsFull(theQueue)) { return 0; } return 0; } int GetContinueData(stMyQueue * theQueue) { int rp=theQueue->rp; int wp=theQueue->wp; if (rp < wp) { return wp - rp; }else if (rp > wp) { return theQueue->Caps - rp; }else if (IsFull(theQueue)) { return theQueue->Caps - rp; }else if (IsEmpty(theQueue)) { return 0; } return 0; } int AddSpace(stMyQueue * theQueue, int nSize) { if (nSize==0) return nSize; int wp=theQueue->wp+=nSize; if (wp>=theQueue->Caps) wp-=theQueue->Caps; theQueue->bEmpty=0; if (wp == theQueue->rp) theQueue->bFull=1; theQueue->wp = wp; // theQueue->RecvBytes+=nSize; return nSize; } int DelData(stMyQueue * theQueue, int nSize) { if (nSize==0) return nSize; int rp=theQueue->rp+nSize; if (rp>=theQueue->Caps) rp-=theQueue->Caps; theQueue->bFull=0; theQueue->rp = rp; if (rp == theQueue->wp) { theQueue->wp=0; theQueue->rp=0; // theQueue->state=0; theQueue->bEmpty=1; } return nSize; } int CopyData(stMyQueue * theQueue, void * pData, int nSize) { int len1=GetContinueData(theQueue); if (nSize <= len1) { memcpy(pData,theQueue->buf1+theQueue->rp,nSize); return nSize; } else { memcpy(pData,theQueue->buf1+theQueue->rp,len1); memcpy((char *)pData+len1,theQueue->buf1,nSize-len1); return nSize; } } int PushOne(stMyQueue * theQueue, unsigned char ch) { theQueue->bEmpty=0; if (!IsFull(theQueue)) { theQueue->buf1[theQueue->wp]=ch; AddSpace(theQueue,1); return 0; } return -1; } int PopOne(stMyQueue * theQueue) { theQueue->bFull=0; unsigned char ch=theQueue->buf1[theQueue->rp]; if (!IsEmpty(theQueue)) { DelData(theQueue,1); } return ch; } int PushIn(stMyQueue * theQueue, void * pData, int nSize) { int len1=GetContinueEmptyRoom(theQueue); if (nSize <= len1) { memcpy(GetWriteBuffer(theQueue),pData,nSize); AddSpace(theQueue, nSize); return nSize; } memcpy(GetWriteBuffer(theQueue),pData,len1); AddSpace(theQueue,len1); int len2=GetContinueEmptyRoom(theQueue); if (nSize-len1 <= len2) { memcpy(GetWriteBuffer(theQueue),(char *)pData+len1,nSize-len1); AddSpace(theQueue, nSize-len1); return nSize; } if (len2>0) { memcpy(GetWriteBuffer(theQueue),(char *)pData+len1,len2); AddSpace(theQueue, len2); len1+=len2; } return len1; } int PopOut(stMyQueue * theQueue, void * pData, int nSize) { // theQueue->rp+=nSize; // if (theQueue->rp >= theQueue ->Caps) theQueue->rp-=theQueue->Caps; // theQueue->DataSize-=nSize; CopyData(theQueue,pData,nSize); DelData(theQueue,nSize); return nSize; }