📄 gblob.cpp
字号:
/* Copyright (C) 2006, Mike Gashler This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. see http://www.gnu.org/copyleft/lesser.html*/#include "GBlob.h"#include "GBits.h"GBlob::GBlob(int nOutBufferSize, bool bOkToResizeOutBuffer){ if(nOutBufferSize > 0) m_pOutBuffer = new unsigned char[nOutBufferSize]; else m_pOutBuffer = NULL; m_nOutBufferSize = nOutBufferSize; m_nOutBufferPos = 0; m_pInBuffer = m_pOutBuffer; m_nInBufferSize = m_nOutBufferSize; m_nInBufferPos = 0; m_bDeleteInBuffer = false; m_bOkToResizeOutBuffer = bOkToResizeOutBuffer;}GBlob::~GBlob(){ delete(m_pOutBuffer); if(m_bDeleteInBuffer) delete(m_pInBuffer);}void GBlob::SetInBuffer(unsigned char* pInBuffer, int nSize, bool bDeleteBuffer){ GAssert(pInBuffer, "pInBuffer can't be NULL"); if(m_bDeleteInBuffer) { GAssert(pInBuffer != m_pInBuffer, "You gave me ownership of the same buffer twice"); delete(m_pInBuffer); } m_pInBuffer = pInBuffer; m_nInBufferSize = nSize; m_nInBufferPos = 0; m_bDeleteInBuffer = bDeleteBuffer;}void GBlob::ResizeOutBuffer(int nRequiredSize){ if(m_bOkToResizeOutBuffer) { bool bSameBuffer = (m_pInBuffer == m_pOutBuffer); int nNewSize = MAX(3 * m_nOutBufferSize, MAX(1024, nRequiredSize)); unsigned char* pNewOutBuffer = new unsigned char[nNewSize]; memcpy(pNewOutBuffer, m_pOutBuffer, m_nOutBufferPos); delete[] m_pOutBuffer; m_pOutBuffer = pNewOutBuffer; m_nOutBufferSize = nNewSize; if(bSameBuffer) { m_pInBuffer = m_pOutBuffer; m_nInBufferSize = m_nOutBufferSize; } } else ThrowError(L"GBlob out buffer too small to hold blob");}void GBlob::Push(const unsigned char* pData, int nSize){ if(m_nOutBufferSize - m_nOutBufferPos < nSize) ResizeOutBuffer(m_nOutBufferPos + nSize); memcpy(&m_pOutBuffer[m_nOutBufferPos], pData, nSize); m_nOutBufferPos += nSize;}void GBlob::Push(const wchar_t wc){#ifdef WIN32 GAssert(sizeof(wchar_t) == 2, "wchar_t wrong size"); wchar_t tmp = GBits::N16ToLittleEndian((unsigned short)wc); Push((const unsigned char*)&tmp, sizeof(wchar_t));#else // WIN32 GAssert(sizeof(wchar_t) == 4, "wchar_t wrong size"); wchar_t tmp = GBits::N32ToLittleEndian(wc); Push((const unsigned char*)&tmp, sizeof(wchar_t));#endif // !WIN32}void GBlob::Push(const char c){ Push((const unsigned char*)&c, sizeof(char));}void GBlob::Push(const int n){ int i = GBits::N32ToLittleEndian(n); Push((const unsigned char*)&i, sizeof(int));}void GBlob::Push(const unsigned int n){ unsigned int i = GBits::N32ToLittleEndian(n); Push((const unsigned char*)&i, sizeof(unsigned int));}void GBlob::Push(const unsigned char uc){ Push((const unsigned char*)&uc, sizeof(unsigned char));}void GBlob::Push(const float f){ float f2 = GBits::R32ToLittleEndian(f); Push((const unsigned char*)&f2, sizeof(float));}void GBlob::Push(const double d){ double d2 = GBits::R64ToLittleEndian(d); Push((const unsigned char*)&d2, sizeof(double));}void GBlob::Push(const char* szString){ int nLen = strlen(szString); Push(nLen); Push((const unsigned char*)szString, nLen);}void GBlob::Pop(unsigned char* pData, int nSize){ if(m_nInBufferSize - m_nInBufferPos < nSize) ThrowError(L"GBlob incoming blob is too small to contain the expected data"); memcpy(pData, &m_pInBuffer[m_nInBufferPos], nSize); m_nInBufferPos += nSize;}void GBlob::Pop(wchar_t* pwc){ Pop((unsigned char*)pwc, sizeof(wchar_t));#ifdef WIN32 GAssert(sizeof(wchar_t) == 2, "wchar_t wrong size"); *pwc = GBits::LittleEndianToN16((unsigned short)*pwc);#else // WIN32 GAssert(sizeof(wchar_t) == 4, "wchar_t wrong size"); *pwc = GBits::LittleEndianToN32(*pwc);#endif // !WIN32}void GBlob::Pop(char* pc){ Pop((unsigned char*)pc, sizeof(char));}void GBlob::Pop(int* pn){ Pop((unsigned char*)pn, sizeof(int)); *pn = GBits::LittleEndianToN32(*pn);}void GBlob::Pop(unsigned int* pui){ Pop((unsigned char*)pui, sizeof(unsigned int)); *pui = GBits::LittleEndianToN32(*pui);}void GBlob::Pop(unsigned char* puc){ Pop(puc, sizeof(unsigned char));}void GBlob::Pop(float* pf){ Pop((unsigned char*)pf, sizeof(float)); *pf = GBits::LittleEndianToR32(*pf);}void GBlob::Pop(double* pd){ Pop((unsigned char*)pd, sizeof(double)); *pd = GBits::LittleEndianToR64(*pd);}void GBlob::Pop(char** pszString){ int nLen; Pop(&nLen); delete(*pszString); char* pString = new char[nLen + 1]; *pszString = pString; Pop((unsigned char*)pString, nLen); pString[nLen] = '\0';}void GBlob::Peek(int nIndex, unsigned char* pData, int nSize){ if(nSize < 0 || nIndex < 0 || nIndex + nSize > m_nInBufferSize) ThrowError(L"GBlob peek out of range"); memcpy(pData, &m_pInBuffer[nIndex], nSize);}void GBlob::Poke(int nIndex, const unsigned char* pData, int nSize){ if(nSize < 0 || nIndex < 0 || nIndex + nSize > m_nOutBufferPos) ThrowError(L"GBlob poke out of range"); memcpy(&m_pOutBuffer[nIndex], pData, nSize);}void GBlob::Poke(int nIndex, const int n){ int tmp = GBits::N32ToLittleEndian(n); Poke(nIndex, (const unsigned char*)&tmp, sizeof(int));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -