提交 | 用户 | age
|
4b03ae
|
1 |
#include "stdafx.h"
|
Q |
2 |
#include "MyCircularBuf.h"
|
|
3 |
//CMyCircularBuf
|
|
4 |
CMyCircularBuf::CMyCircularBuf()
|
|
5 |
{
|
|
6 |
pBuffer=NULL;
|
|
7 |
nDataSize=0;
|
|
8 |
bIsEmpty=true;
|
|
9 |
bIsFull=false;
|
|
10 |
}
|
|
11 |
CMyCircularBuf::CMyCircularBuf(int nCapicity)
|
|
12 |
{
|
|
13 |
Alloc(nCapicity);
|
|
14 |
}
|
|
15 |
CMyCircularBuf::~CMyCircularBuf()
|
|
16 |
{
|
|
17 |
if (pBuffer!=NULL) {delete [] pBuffer;pBuffer=NULL;}
|
|
18 |
}
|
|
19 |
int CMyCircularBuf::Alloc(int nCapicity)
|
|
20 |
{
|
|
21 |
if (pBuffer!=NULL) {delete [] pBuffer;pBuffer=NULL;}
|
|
22 |
pBuffer=new char[nCapicity];
|
|
23 |
nmCapacity=nCapicity;
|
|
24 |
nDataSize=0;
|
|
25 |
ReadP=0;WriteP=0;
|
|
26 |
bIsEmpty=true;
|
|
27 |
bIsFull=false;
|
|
28 |
return nCapicity;
|
|
29 |
}
|
|
30 |
int CMyCircularBuf::GetCapicity()
|
|
31 |
{
|
|
32 |
return nmCapacity;
|
|
33 |
}
|
|
34 |
int CMyCircularBuf::GetNextWriteP(int nOffset)
|
|
35 |
{
|
|
36 |
int nextip=WriteP+nOffset;
|
|
37 |
if (nextip>=nmCapacity) {nextip-=nmCapacity;}
|
|
38 |
return nextip;
|
|
39 |
}
|
|
40 |
int CMyCircularBuf::GetNextReadP(int nOffset)
|
|
41 |
{
|
|
42 |
int nextop=ReadP+nOffset;
|
|
43 |
if (nextop>=nmCapacity) {nextop-=nmCapacity;}
|
|
44 |
return nextop;
|
|
45 |
}
|
|
46 |
int CMyCircularBuf::GetTailRoomSize()
|
|
47 |
{
|
|
48 |
int nTailLeftSize;
|
|
49 |
if (bIsFull) {nTailLeftSize=0; return nTailLeftSize;}
|
|
50 |
if (WriteP<ReadP) {nTailLeftSize=ReadP-WriteP;}
|
|
51 |
else {nTailLeftSize=nmCapacity-WriteP;}
|
|
52 |
return nTailLeftSize;
|
|
53 |
}
|
|
54 |
int CMyCircularBuf::GetTailDataSize()
|
|
55 |
{
|
|
56 |
int nTailLeftDataSize;
|
|
57 |
if (bIsEmpty) return 0;
|
|
58 |
if (WriteP>ReadP) nTailLeftDataSize=WriteP-ReadP;
|
|
59 |
else {nTailLeftDataSize=nmCapacity-ReadP;}
|
|
60 |
return nTailLeftDataSize;
|
|
61 |
}
|
|
62 |
int CMyCircularBuf::GetLeftRoomSize()
|
|
63 |
{
|
|
64 |
int nLeftSize=nmCapacity-nDataSize;
|
|
65 |
return nLeftSize;
|
|
66 |
}
|
|
67 |
int CMyCircularBuf::GetDataSize()
|
|
68 |
{
|
|
69 |
return nDataSize;
|
|
70 |
}
|
|
71 |
char * CMyCircularBuf::GetWriteBuffer(int * LeftRoomSize)
|
|
72 |
{
|
|
73 |
int nTailLeftRoomSize=GetTailRoomSize();
|
|
74 |
*LeftRoomSize=nTailLeftRoomSize;
|
|
75 |
return pBuffer+WriteP;
|
|
76 |
}
|
|
77 |
int CMyCircularBuf::ReleaseWriteBuffer(int AddSize)
|
|
78 |
{
|
|
79 |
WriteP=GetNextWriteP(AddSize);
|
|
80 |
nDataSize+=AddSize;
|
|
81 |
bIsEmpty=false;
|
|
82 |
if (nDataSize>=nmCapacity) bIsFull=true;
|
|
83 |
|
|
84 |
return AddSize;
|
|
85 |
}
|
|
86 |
char * CMyCircularBuf::GetReadBuffer(int * DataSize)
|
|
87 |
{
|
|
88 |
int nTailLeftDataSize=GetTailDataSize();
|
|
89 |
*DataSize=nTailLeftDataSize;
|
|
90 |
return pBuffer+ReadP;
|
|
91 |
}
|
|
92 |
int CMyCircularBuf::ReleaseReadBuffer(int DelSize)
|
|
93 |
{
|
|
94 |
DelData(DelSize);
|
|
95 |
return DelSize;
|
|
96 |
}
|
|
97 |
|
|
98 |
int CMyCircularBuf::PushIn(void * pData,int Size)
|
|
99 |
{
|
|
100 |
if (GetLeftRoomSize()<Size) {return -1;}
|
|
101 |
int nTailLeftSize=GetTailRoomSize();
|
|
102 |
if (nTailLeftSize>=Size)
|
|
103 |
{
|
|
104 |
memcpy_s(pBuffer+WriteP,nTailLeftSize,pData,Size);
|
|
105 |
WriteP=GetNextWriteP(Size);
|
|
106 |
}else
|
|
107 |
{
|
|
108 |
memcpy_s(pBuffer+WriteP,nTailLeftSize,pData,nTailLeftSize);
|
|
109 |
memcpy_s(pBuffer,Size-nTailLeftSize,(char *)pData+nTailLeftSize,Size-nTailLeftSize);
|
|
110 |
WriteP=GetNextWriteP(Size);
|
|
111 |
}
|
|
112 |
bIsEmpty=false;
|
|
113 |
nDataSize+=Size;
|
|
114 |
if (nDataSize==nmCapacity) {bIsFull=true;}
|
|
115 |
return Size;
|
|
116 |
}
|
|
117 |
int CMyCircularBuf::CopyData(void * pData,int Size)
|
|
118 |
{
|
|
119 |
if (GetDataSize()<Size) {return -1;}
|
|
120 |
int nTailLeftSize=GetTailDataSize(); //环形缓冲尾部剩余数据量
|
|
121 |
if (nTailLeftSize<Size) ////剩余空间小,将数据转移到一起
|
|
122 |
{
|
|
123 |
memcpy_s(pData,Size,pBuffer+ReadP,nTailLeftSize);
|
|
124 |
memcpy_s((char *)pData+nTailLeftSize,Size-nTailLeftSize,pBuffer,Size-nTailLeftSize);
|
|
125 |
}else
|
|
126 |
{
|
|
127 |
memcpy_s(pData,Size,pBuffer+ReadP,Size);
|
|
128 |
}
|
|
129 |
return Size;
|
|
130 |
}
|
|
131 |
int CMyCircularBuf::DelData(int Size)
|
|
132 |
{
|
|
133 |
if (GetDataSize()<Size) {return -1;}
|
|
134 |
bIsFull=false;
|
|
135 |
ReadP=GetNextReadP(Size);
|
|
136 |
nDataSize-=Size;
|
|
137 |
if (nDataSize==0) {bIsEmpty=true;}
|
|
138 |
return Size;
|
|
139 |
}
|
|
140 |
|
|
141 |
int CMyCircularBuf::PopOut(void * pData,int Size)
|
|
142 |
{
|
|
143 |
CopyData(pData,Size);
|
|
144 |
DelData(Size);
|
|
145 |
return Size;
|
|
146 |
} |