core/CDataPack.cpp
changeset 484 d254e257cac2
child 488 daf1bd692ef3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/CDataPack.cpp	Tue Feb 13 16:11:04 2007 +0000
     1.3 @@ -0,0 +1,176 @@
     1.4 +#include <malloc.h>
     1.5 +#include <string.h>
     1.6 +#include "CDataPack.h"
     1.7 +
     1.8 +#define DATAPACK_INITIAL_SIZE		512
     1.9 +
    1.10 +CDataPack::CDataPack()
    1.11 +{
    1.12 +	m_pBase = (char *)malloc(DATAPACK_INITIAL_SIZE);
    1.13 +	m_capacity = DATAPACK_INITIAL_SIZE;
    1.14 +	Initialize();
    1.15 +}
    1.16 +
    1.17 +CDataPack::~CDataPack()
    1.18 +{
    1.19 +	free(m_pBase);
    1.20 +}
    1.21 +
    1.22 +void CDataPack::Initialize()
    1.23 +{
    1.24 +	m_curptr = m_pBase;
    1.25 +	m_size = 0;
    1.26 +}
    1.27 +
    1.28 +void CDataPack::CheckSize(size_t typesize)
    1.29 +{
    1.30 +	if (m_curptr - m_pBase + typesize <= m_capacity)
    1.31 +	{
    1.32 +		return;
    1.33 +	}
    1.34 +
    1.35 +	size_t pos = m_curptr - m_pBase;
    1.36 +	do
    1.37 +	{
    1.38 +		m_capacity *= 2;
    1.39 +		m_pBase = (char *)realloc(m_pBase, m_capacity);
    1.40 +		m_curptr = m_pBase + pos;
    1.41 +	} while (m_curptr - m_pBase + typesize > m_capacity);
    1.42 +}
    1.43 +
    1.44 +void CDataPack::ResetSize()
    1.45 +{
    1.46 +	m_size = 0;
    1.47 +}
    1.48 +
    1.49 +size_t CDataPack::CreateMemory(size_t size, void **addr)
    1.50 +{
    1.51 +	size_t pos = m_curptr - m_pBase;
    1.52 +
    1.53 +	m_pBase = (char *)realloc(m_pBase, size);
    1.54 +	m_curptr = m_pBase + pos;
    1.55 +	m_capacity = size;
    1.56 +
    1.57 +	if (addr)
    1.58 +	{
    1.59 +		*addr = m_pBase;
    1.60 +	}
    1.61 +
    1.62 +	return pos;
    1.63 +}
    1.64 +
    1.65 +void CDataPack::PackCell(cell_t cell)
    1.66 +{
    1.67 +	CheckSize(sizeof(cell_t));
    1.68 +
    1.69 +	*(cell_t *)m_curptr = cell;
    1.70 +	m_curptr += sizeof(cell_t);
    1.71 +	m_size += sizeof(cell_t);
    1.72 +}
    1.73 +
    1.74 +void CDataPack::PackFloat(float val)
    1.75 +{
    1.76 +	CheckSize(sizeof(float));
    1.77 +
    1.78 +	*(float *)m_curptr = val;
    1.79 +	m_curptr += sizeof(float);
    1.80 +	m_size += sizeof(float);
    1.81 +}
    1.82 +
    1.83 +void CDataPack::PackString(const char *string)
    1.84 +{
    1.85 +	size_t len = strlen(string);
    1.86 +	size_t maxsize = sizeof(size_t) + len + 1;
    1.87 +	CheckSize(maxsize);
    1.88 +
    1.89 +	// Pack the string length first for buffer overrun checking.
    1.90 +	*(size_t *)m_curptr = len;
    1.91 +	m_curptr += sizeof(size_t);
    1.92 +
    1.93 +	// Now pack the string.
    1.94 +	memcpy(m_curptr, string, len);
    1.95 +	m_curptr[len] = '\0';
    1.96 +	m_curptr += len + 1;
    1.97 +
    1.98 +	m_size += maxsize;
    1.99 +}
   1.100 +
   1.101 +void CDataPack::Reset() const
   1.102 +{
   1.103 +	m_curptr = m_pBase;
   1.104 +}
   1.105 +
   1.106 +size_t CDataPack::GetPosition() const
   1.107 +{
   1.108 +	return static_cast<size_t>(m_curptr - m_pBase);
   1.109 +}
   1.110 +
   1.111 +bool CDataPack::SetPosition(size_t pos) const
   1.112 +{
   1.113 +	if (pos > m_size-1)
   1.114 +	{
   1.115 +		return false;
   1.116 +	}
   1.117 +	m_curptr = m_pBase + pos;
   1.118 +
   1.119 +	return true;
   1.120 +}
   1.121 +
   1.122 +cell_t CDataPack::ReadCell() const
   1.123 +{
   1.124 +	if (!IsReadable(sizeof(cell_t)))
   1.125 +	{
   1.126 +		return 0;
   1.127 +	}
   1.128 +	cell_t val = *reinterpret_cast<cell_t *>(m_curptr);
   1.129 +	m_curptr += sizeof(cell_t);
   1.130 +	return val;
   1.131 +}
   1.132 +
   1.133 +float CDataPack::ReadFloat() const
   1.134 +{
   1.135 +	if (!IsReadable(sizeof(float)))
   1.136 +	{
   1.137 +		return 0;
   1.138 +	}
   1.139 +	float val = *reinterpret_cast<float *>(m_curptr);
   1.140 +	m_curptr += sizeof(float);
   1.141 +	return val;
   1.142 +}
   1.143 +
   1.144 +bool CDataPack::IsReadable(size_t bytes) const
   1.145 +{
   1.146 +	return (bytes + (m_curptr - m_pBase) > m_size) ? false : true;
   1.147 +}
   1.148 +
   1.149 +const char *CDataPack::ReadString(size_t *len) const
   1.150 +{
   1.151 +	if (!IsReadable(sizeof(size_t)))
   1.152 +	{
   1.153 +		return 0;
   1.154 +	}
   1.155 +
   1.156 +	size_t real_len = *(size_t *)m_curptr;
   1.157 +
   1.158 +	m_curptr += sizeof(size_t);
   1.159 +	char *str = (char *)m_curptr;
   1.160 +
   1.161 +	if ((strlen(str) != real_len) || !(IsReadable(real_len+1)))
   1.162 +	{
   1.163 +		return NULL;
   1.164 +	}
   1.165 +
   1.166 +	if (len)
   1.167 +	{
   1.168 +		*len = real_len;
   1.169 +	}
   1.170 +
   1.171 +	m_curptr += real_len + 1;
   1.172 +
   1.173 +	return str;
   1.174 +}
   1.175 +
   1.176 +void *CDataPack::GetMemory() const
   1.177 +{
   1.178 +	return m_curptr;
   1.179 +}