memstream.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:6k
源码类别:
P2P编程
开发平台:
Visual C++
- // memstream.cpp: implementation of the Cmemstream class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testBT.h"
- #include "memstream.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- memstream::memstream()
- {
- ;
- }
- memstream::~memstream()
- {
- ;
- }
- memstream::operator char* const() const
- {
- ASSERT(m_lpBuffer && GetLength() > 0);
- return ( char* const)m_lpBuffer;
- }
- void memstream::clear()
- {
- delete Detach();
- }
- void memstream::write(const char* pBuf, long length)
- {
- if (length <= 0)
- return;
- Write(pBuf, length);
- }
- long memstream::size() const
- {
- return GetLength();
- }
- void memstream::operator = (const memstream& other)
- {
- clear();
- if (other.size() <= 0)
- return;
- write(other, other.GetLength());
- }
- bool memstream::operator ==(memstream& other)
- {
- if (GetLength () != other.GetLength())
- return false;
- for (int i=0; i<other.GetLength(); i++)
- {
- if ( (*(m_lpBuffer+i)) != other[i] )
- return false;
- }
- return true;
- }
- memstream& memstream::operator +=(string& str)
- {
- write(str.data(), str.length());
- return *this;
- }
- memstream& memstream::operator +=(const char* pstr)
- {
- write(pstr, strlen(pstr));
- return *this;
- }
- void memstream::TrimLeft(long length)
- {
- //*
- assert(size() > 0);
- assert(length <= size() && length >= 0);
- if (size() == length)
- {
- clear();
- return;
- }
- long len = size() - length;
- char* pnewBuf = (char*)Detach();
- auto_ptr<char> pp(pnewBuf);
- write(pnewBuf + length, len);
- }
- /*
- memstream::memstream()
- {
- m_pBuf = new strstream();
- }
- memstream::~memstream()
- {
- delete m_pBuf;
- }
- void memstream::clear()
- {
- delete m_pBuf;
- m_pBuf = new strstream();
- }
- void memstream::write(const char* pBuf, long length)
- {
- if (length <= 0)
- return;
- // m_pBuf->seekp(0, ios::end);
- m_pBuf->write(pBuf, length);
- assert(m_pBuf->good());
- }
- /*
- int memstream::read(char* pBuf, long length)
- {
- assert(length > 0);
- m_pBuf->seekg(0, ios::beg);
- m_pBuf->read(pBuf, length);
- assert(m_pBuf->good());
- return length;
- }
- memstream::operator char* const() const
- {
- if (size() <= 0)
- assert(false);
- char* pRet = m_pBuf->str();
- m_pBuf->freeze(0);
- assert(m_pBuf->good());
- return pRet;
- }
- long memstream::size() const
- {
- m_pBuf->seekg(0, ios::end);
- long len = m_pBuf->tellg();
- m_pBuf->seekg(0, ios::beg);
- assert(m_pBuf->good());
- return len < 0 ? 0 : len;
- }
- //*
- void memstream::operator = (const memstream& other)
- {
- long len = other.size();
- char* pszBuf = new char[len];
- other.m_pBuf->read(pszBuf, len);
- assert(other.m_pBuf->good());
- delete m_pBuf;
- m_pBuf = new strstream();
- m_pBuf->write(pszBuf, len);
- delete pszBuf;
- assert(m_pBuf->good());
- }
- bool memstream::operator ==(memstream& other)
- {
- if (size() != other.size())
- return false;
- if (size() <= 0)
- return true;
- char* pBuf = new char[size()];
- char* pBuf2 = new char[size()];
- auto_ptr<char> a1(pBuf);
- auto_ptr<char> a2(pBuf2);
- m_pBuf->seekg(0, ios::beg);
- other.m_pBuf->seekg(0, ios::beg);
- m_pBuf->read(pBuf, size());
- other.m_pBuf->read(pBuf2, size());
- for (int i=0; i<size(); i++)
- {
- if ( (*(pBuf+i)) != (*(pBuf2+i)))
- return false;
- }
- return true;
- }
- memstream& memstream::operator +=(string& str)
- {
- write(str.data(), str.length());
- return *this;
- }
- memstream& memstream::operator +=(const char* pstr)
- {
- write(pstr, strlen(pstr));
- return *this;
- }
- void memstream::TrimLeft(long length)
- {
- //*
- assert(size() > 0);
- assert(length <= size() && length >= 0);
- if (size() == length)
- {
- delete m_pBuf;
- m_pBuf = new strstream();
- assert(m_pBuf->good());
- return;
- }
- long len = size() - length;
- char* pnewBuf = new char[len];
- auto_ptr<char> pp(pnewBuf);
- m_pBuf->seekg(length, ios::beg);
- m_pBuf->read(pnewBuf, len);
- assert(m_pBuf->good());
- delete m_pBuf;
- m_pBuf = new strstream();
- write(pnewBuf, len);
- assert(m_pBuf->good());
- }
- //*/
- /*
- memstream::memstream()
- {
- m_pBuf = 0;
- m_lLength = 0;
- }
- memstream::~memstream()
- {
- clear();
- }
- void memstream::clear()
- {
- if (m_lLength)
- {
- ASSERT(m_pBuf);
- delete m_pBuf;
- }
- m_lLength = 0;
- m_pBuf = 0;
- }
- void memstream::write(const char* pBuf, long length)
- {
- if (length == 0)
- return;
- if (!m_lLength)
- {
- ASSERT(!m_pBuf);
- m_pBuf = new char[1024];
- }
- else if ((m_lLength % 1024 + length ) > 1024)
- {
- long lnewsize = ((m_lLength + length) / 1024 + 2) * 1024;
- char* pnewBuf = new char[lnewsize];
- ASSERT(m_pBuf !=0 );
- memmove(pnewBuf, m_pBuf, m_lLength);
- delete m_pBuf;
- m_pBuf = pnewBuf;
- }
- memcpy(m_pBuf, pBuf, length);
- m_lLength+= length;
- /*
- char* pnewBuf = new char[m_lLength+length];
- if (m_lLength)
- {
- assert(m_pBuf !=0 );
- memcpy(pnewBuf, m_pBuf, m_lLength);
- delete m_pBuf;
- }
- memcpy(pnewBuf+m_lLength, pBuf, length);
- m_lLength+= length;
- m_pBuf = pnewBuf;
- }
- void memstream::operator =(const memstream& other)
- {
- if (other.m_lLength == 0)
- {
- m_lLength = 0;
- m_pBuf = 0;
- }
- else
- {
- write(other, other.size());
- }
- }
- bool memstream::operator ==(memstream& other)
- {
- if (m_lLength != other.m_lLength)
- return false;
- for (int i=0; i<m_lLength; i++)
- {
- if ( (*(m_pBuf+i)) != (*(other.m_pBuf+i)))
- return false;
- }
- return true;
- }
- memstream& memstream::operator +=(string& str)
- {
- write(str.data(), str.length());
- return *this;
- }
- memstream& memstream::operator +=(const char* pstr)
- {
- write(pstr, strlen(pstr));
- return *this;
- }
- memstream::operator char* const() const
- {
- if (m_lLength <= 0)
- assert(false);
- return m_pBuf;
- }
- long memstream::size() const
- {
- return m_lLength;
- }
- void memstream::TrimLeft(long length)
- {
- assert(m_lLength > 0);
- assert(length <= m_lLength);
- if (m_lLength == length)
- {
- delete m_pBuf;
- m_lLength = 0;
- m_pBuf = 0;
- return;
- }
- char* pnewBuf = new char[m_lLength-length];
- memcpy(pnewBuf, m_pBuf+length, m_lLength-length);
- delete m_pBuf;
- m_pBuf = pnewBuf;
- m_lLength = m_lLength-length;
- }
- //*/