zxd
2023-09-14 d34256830982fb9ea822c1e9b874c3b7fa0a614d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#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;
}