📄 bufferpipe.h
字号:
/*
* 版权所有 hdqqq mail: xuxuhappy@sina.com.cn
* 可以随便使用和分发下面的代码,但请保留这段文字.
*/
// BufferPipe.h: interface for the CBufferPipe class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_BUFFERPIPE_H__63AAC00E_3C15_4202_8811_23106E3B5640__INCLUDED_)
#define AFX_BUFFERPIPE_H__63AAC00E_3C15_4202_8811_23106E3B5640__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
template <typename data_type,int buf_size>
class buf_holder
{
public:
typedef data_type* pointer;
typedef const data_type* const_pointer;
typedef data_type& reference;
typedef const data_type& const_reference;
typedef data_type value_type;
private:
data_type* m_pBuf;
data_type* m_pDataPosition;
int m_nBufSize;
int m_nDataCount;
public:
operator LPBYTE() {
return (BYTE*)m_pBuf;
}
int GetBufSize() {
return m_nBufSize;
}
int GetDataCount() {
return m_nDataCount;
}
int SetDataCount(int count) {
m_nDataCount = count;
m_pDataPosition = m_pBuf + count;
return m_nDataCount;
}
int GetFreeDataCount() {
return m_nBufSize - m_nDataCount;
}
int WriteBuf(const void* buf,int bufsize) {
int ret = 0;
if ((m_nBufSize - m_nDataCount) >= bufsize) {
memcpy(m_pDataPosition,buf,bufsize);
m_pDataPosition += bufsize;
m_nDataCount += bufsize;
ret = bufsize;
}else {
int copycount = m_nBufSize - m_nDataCount;
if (copycount > 0) {
memcpy(m_pDataPosition,buf,copycount);
m_pDataPosition += copycount;
m_nDataCount += copycount;
ret = copycount;
}
}
return ret;
}
int ReadBuf(void* buf,int readcount) {
int ret = 0;
if (m_nDataCount >= readcount) {
memcpy(buf,m_pBuf,readcount);
ret = readcount;
}
return ret;
}
void Clear() {
m_pDataPosition = m_pBuf;
m_nDataCount = 0;
memset(m_pBuf,0,sizeof(data_type)*m_nBufSize);
}
void Resize(int size)
{
m_nBufSize = size;
if (m_pBuf) {
delete [] m_pBuf;
}
m_pBuf = new data_type[size];
m_pDataPosition = m_pBuf;
Clear();
}
public:
buf_holder() : m_nDataCount(0)
{
m_nBufSize = buf_size;
m_pBuf = new data_type[buf_size];
m_pDataPosition = m_pBuf;
Clear();
}
~buf_holder() {
if (m_pBuf) {
delete [] m_pBuf;
}
}
};
template <typename filter,typename buffer_type, typename next_pipe>
class BufferPipe
{
private:
filter m_Filter;
next_pipe* m_NextPipe;
buffer_type m_Buf;
public:
UINT Read(void* lpBuf,UINT nCount) {
int ret = 0;
BYTE* ptemp = (BYTE*)lpBuf;
if (m_Buf.GetDataCount() > nCount) {
//memcpy(lpBuf, m_Buf.operator LPBYTE(), nCount);
ret = m_Buf.ReadBuf(lpBuf, nCount);
}else {
ret = m_Buf.ReadBuf(lpBuf, m_Buf.GetDataCount());
}
return ret;
}
void Write(const void* lpBuf,UINT nCount) {
BYTE* ptemp = (BYTE*)lpBuf;
int writecount = m_Buf.WriteBuf(lpBuf, nCount);
while (writecount < nCount) {
int filter_out = 0;
BYTE* pout = m_Filter.Process(m_Buf.operator LPBYTE(), m_Buf.GetDataCount(),filter_out);
if (pout && filter_out > 0) {
m_NextPipe->Write(pout, filter_out);
delete [] pout;
}
m_Buf.Clear();
writecount += m_Buf.WriteBuf(ptemp + writecount, nCount - writecount);
}
}
void Close() {
if (m_Buf.GetDataCount() > 0) {
int filter_out = 0;
BYTE* pout = m_Filter.ProcessLast(m_Buf.operator LPBYTE(), m_Buf.GetDataCount(),filter_out);
if (pout && filter_out > 0) {
m_NextPipe->Write(pout, filter_out);
delete [] pout;
}
m_Buf.Clear();
}
}
void SetNextPipe(next_pipe* np) {
m_NextPipe = np;
}
public:
BufferPipe() : m_NextPipe(NULL) {}
BufferPipe(next_pipe* np) : m_NextPipe(np) {}
~BufferPipe() {}
};
#endif // !defined(AFX_BUFFERPIPE_H__63AAC00E_3C15_4202_8811_23106E3B5640__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -