重写String类,也有些区别,供参考

头文件如下:

#pragma once
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
using namespace std;

typedef int   BOOL;
#define FALSE 0
#define TRUE  1


typedef unsigned char BYTE;
typedef unsigned int UINT;

class MBuffer
{
public:
	MBuffer();
	MBuffer(const char * other);
	MBuffer(const MBuffer&  other);
	MBuffer(const string&  other);
	virtual ~MBuffer();
private:
	BYTE*	 m_pBuffer;
	UINT	 m_nBufferSize;

	BYTE*	 m_pData;
	UINT	 m_nDataSize;

public:
	virtual void FreeBuffer();
	virtual BOOL AppendData(unsigned char* pData, UINT nDataSize);
	virtual BOOL append( char* str,int nSize /*= 0*/ );
	virtual BOOL append(const MBuffer &other);
	virtual BOOL append(unsigned int value);
	virtual void ClearData();
	const char* GetData() ;
	char* c_str()  const;
	unsigned long   length() const;
	virtual void clear();
	MBuffer& operator=(const char * other);//赋值操作符重载
	MBuffer& operator=(const MBuffer& other);//赋值操作符重载
	MBuffer& operator=(const string& other);//赋值操作符重载
	BOOL operator==(const MBuffer& other);//赋值操作符重载
	char* substr( int nStart,int iSize );
	int findlastofindex(char *cStr);
	int findlastofindex(char cChr);
	bool subbuffer(MBuffer& outBuffer,int istart,int ilen);
private:
	virtual BOOL AllocateBuffer(UINT nBufferSize);
	virtual BOOL ExtendBuffer(UINT nBufferSize);
	virtual BOOL FillData(unsigned char* pData, UINT nDataSize);

};

  CPP文件:

#include "MBuffer.h"

MBuffer::MBuffer()
{
	m_pBuffer = NULL;
	m_nBufferSize = 0;

	m_pData = NULL;
	m_nDataSize = 0;
}

MBuffer::MBuffer( const char * other )
 {
	 m_pBuffer = NULL;
	 m_nBufferSize = 0;

	 m_pData = NULL;
	 m_nDataSize = 0;
	 if (other != NULL)
	 {
		 AppendData((unsigned char*)other,strlen(other));
	 }
}

MBuffer::MBuffer( const MBuffer& other )
{
	m_pBuffer = NULL;
	m_nBufferSize = 0;

	m_pData = NULL;
	m_nDataSize = 0;
	AppendData(other.m_pData,other.m_nDataSize);
}

MBuffer::MBuffer( const string& other )
{
	FreeBuffer();
	AppendData((unsigned char*)other.c_str(),other.length());
}

MBuffer::~MBuffer()
{
	FreeBuffer();
}

void MBuffer::FreeBuffer()
{
	if (m_pBuffer == NULL)
		return;

	delete[] m_pBuffer;

	m_pBuffer = NULL;
	m_nBufferSize = 0;

	m_pData = NULL;
	m_nDataSize = 0;
}

BOOL MBuffer::AllocateBuffer( UINT nBufferSize )
{
	if ( nBufferSize == 0)
		return FALSE;

	FreeBuffer();

	m_pBuffer = new BYTE[nBufferSize + 1];
	if ( m_pBuffer == NULL)
		return FALSE;
	m_nBufferSize = nBufferSize;
	memset(m_pBuffer,0,nBufferSize + 1);
	m_pData = m_pBuffer;

	return true;
}

BOOL MBuffer::ExtendBuffer( UINT nBufferSize )
{
	if (nBufferSize <= m_nBufferSize)
		return TRUE;

	return AllocateBuffer(nBufferSize);
}

BOOL MBuffer::FillData( unsigned char* pData, UINT nDataSize )
{
	if (pData == NULL || nDataSize == 0)
		return FALSE;

	if (m_nBufferSize < nDataSize)
		AllocateBuffer(nDataSize);

	memcpy(m_pBuffer, pData, nDataSize);
	m_nDataSize = nDataSize;

	return TRUE;
}

BOOL MBuffer::AppendData( unsigned char* pData, UINT nDataSize )
{
	if (nDataSize == 0)
		return FALSE;

	if (!m_pBuffer && !m_nBufferSize)
		AllocateBuffer(nDataSize);

	if ( ( m_nBufferSize - ( m_pData - m_pBuffer)) < ( m_nDataSize + nDataSize))
	{
		MBuffer bufferTemp;
		if (m_nDataSize)
		{
			if ( bufferTemp.FillData(m_pData, m_nDataSize) == FALSE)
				return FALSE;
		}

		unsigned char* pBuffer = NULL;
		pBuffer = new BYTE[m_nDataSize + nDataSize + 1];
		if ( pBuffer == NULL)
			return FALSE;

		if ( m_pBuffer != NULL)
			{
				delete [] m_pBuffer;
				m_pBuffer = NULL;
			}

		m_pBuffer = pBuffer;
		m_pData = pBuffer;

		m_nBufferSize = m_nDataSize + nDataSize;

		if (bufferTemp.m_nDataSize)
			memcpy( m_pData, bufferTemp.m_pData, bufferTemp.m_nDataSize);
		m_pData[m_nDataSize + nDataSize] = '\0';
		m_nDataSize = bufferTemp.m_nDataSize;
	}
	memcpy( m_pData + m_nDataSize, pData, nDataSize);
	m_nDataSize += nDataSize;

	return TRUE;
}

void MBuffer::ClearData()
{
	m_nDataSize = 0;
	m_pData = m_pBuffer;
}

const char* MBuffer::GetData()
{
	return  (char*)m_pData;
}

char* MBuffer::c_str() const
{
	if (m_pData == NULL || m_nDataSize<=0)
	{
		return NULL;
	}
	return  (char*)m_pData;
}

unsigned long MBuffer::length() const
{
	return m_nDataSize;
}

MBuffer& MBuffer::operator=( const char * other )
{
	FreeBuffer();
	if (other)
	{
		AppendData((unsigned char*)other,strlen(other));
	}
	return *this;
}

MBuffer& MBuffer::operator=( const MBuffer& other )
{
	FreeBuffer();
	AppendData(other.m_pData,other.m_nDataSize);
	return *this;
}

MBuffer& MBuffer::operator=( const string& other )
{
	FreeBuffer();
	AppendData((unsigned char*)other.c_str(),other.length());
	return *this;
}

void MBuffer::clear()
{
	FreeBuffer();
}

char* MBuffer::substr( int nStart,int iSize )
{
	if (nStart > m_nDataSize || iSize < 0 || iSize > m_nDataSize - nStart)
	{
		return NULL;
	}
	char *temp = (char*)malloc(iSize + 1);
	memcpy(temp,m_pData + nStart,iSize);
	temp[iSize] = '\0';
	return temp;
}

int MBuffer::findlastofindex( char cChr )
{
	if (m_pData == NULL || m_nDataSize < 1)
	{
		return -1;
	}
	for (int i = m_nDataSize-1 ;i >=0 ; i--)
	{
		if (m_pData[i] == cChr)
		{
			return i;
		}
	}
	return -1;
}

int MBuffer::findlastofindex( char *cStr )
{
	if (m_pData == NULL || cStr == NULL)
	{
		return -1;
	}

	int i, s1_len = strlen(cStr), end = m_nDataSize - s1_len;
	char* last_index = NULL;
	for (i = end - 1; i >= 0; i--)
		if (!strncmp((char*)(m_pData + i), cStr, s1_len))
			return i;
	return -1;
}

BOOL MBuffer::append( char* str,int nSize /*= 0*/ )
{
	if (nSize == 0)
	{
		nSize = strlen(str);
	}
	return AppendData((unsigned char*)str,nSize);

}

BOOL MBuffer::append( unsigned int value )
{
	char cBuf[10]={0};
	sprintf(cBuf,"%d",value);
	return AppendData((unsigned char*)cBuf,strlen(cBuf));

}

BOOL MBuffer::append(const MBuffer &other )
{
	return AppendData((unsigned char*)other.c_str(),other.length());
}

BOOL MBuffer::operator==( const MBuffer& other )
{
	BOOL res = TRUE;
	if (m_nDataSize != other.m_nDataSize)
	{
		return FALSE;
	}
	if (m_nDataSize<=0)
	{
		return FALSE;
	}
	for (int i =0;i<m_nDataSize;i++)
	{
		if (m_pData[i]!= other.m_pData[i])
		{
			res = FALSE;
			break;
		}
	}
	return res;
}

bool MBuffer::subbuffer( MBuffer& outBuffer,int istart,int ilen )
{
	if (m_nDataSize < istart + ilen || ilen <=0 || istart <0)
	{
		return false;
	}

	char *sbfer = new char[ilen+1];
	memset(sbfer,0,ilen+1);
	memcpy(sbfer,m_pData + istart + 1,ilen -1);
	outBuffer = sbfer;
	delete sbfer;
	return true;
}

  如果有bug还请大家帮忙指正,提出大家的宝贵意见,不断完善~谢谢~

posted @ 2015-12-10 12:09  fireflyover  阅读(904)  评论(0编辑  收藏  举报