5 #define DATAPACK_INITIAL_SIZE 512
9 m_pBase = (char *)malloc(DATAPACK_INITIAL_SIZE);
10 m_capacity = DATAPACK_INITIAL_SIZE;
14 CDataPack::~CDataPack()
19 void CDataPack::Initialize()
25 void CDataPack::CheckSize(size_t typesize)
27 if (m_curptr - m_pBase + typesize <= m_capacity)
32 size_t pos = m_curptr - m_pBase;
36 m_pBase = (char *)realloc(m_pBase, m_capacity);
37 m_curptr = m_pBase + pos;
38 } while (m_curptr - m_pBase + typesize > m_capacity);
41 void CDataPack::ResetSize()
46 size_t CDataPack::CreateMemory(size_t size, void **addr)
48 size_t pos = m_curptr - m_pBase;
50 m_pBase = (char *)realloc(m_pBase, size);
51 m_curptr = m_pBase + pos;
62 void CDataPack::PackCell(cell_t cell)
64 CheckSize(sizeof(cell_t));
66 *(cell_t *)m_curptr = cell;
67 m_curptr += sizeof(cell_t);
68 m_size += sizeof(cell_t);
71 void CDataPack::PackFloat(float val)
73 CheckSize(sizeof(float));
75 *(float *)m_curptr = val;
76 m_curptr += sizeof(float);
77 m_size += sizeof(float);
80 void CDataPack::PackString(const char *string)
82 size_t len = strlen(string);
83 size_t maxsize = sizeof(size_t) + len + 1;
86 // Pack the string length first for buffer overrun checking.
87 *(size_t *)m_curptr = len;
88 m_curptr += sizeof(size_t);
90 // Now pack the string.
91 memcpy(m_curptr, string, len);
98 void CDataPack::Reset() const
103 size_t CDataPack::GetPosition() const
105 return static_cast<size_t>(m_curptr - m_pBase);
108 bool CDataPack::SetPosition(size_t pos) const
114 m_curptr = m_pBase + pos;
119 cell_t CDataPack::ReadCell() const
121 if (!IsReadable(sizeof(cell_t)))
125 cell_t val = *reinterpret_cast<cell_t *>(m_curptr);
126 m_curptr += sizeof(cell_t);
130 float CDataPack::ReadFloat() const
132 if (!IsReadable(sizeof(float)))
136 float val = *reinterpret_cast<float *>(m_curptr);
137 m_curptr += sizeof(float);
141 bool CDataPack::IsReadable(size_t bytes) const
143 return (bytes + (m_curptr - m_pBase) > m_size) ? false : true;
146 const char *CDataPack::ReadString(size_t *len) const
148 if (!IsReadable(sizeof(size_t)))
153 size_t real_len = *(size_t *)m_curptr;
155 m_curptr += sizeof(size_t);
156 char *str = (char *)m_curptr;
158 if ((strlen(str) != real_len) || !(IsReadable(real_len+1)))
168 m_curptr += real_len + 1;
173 void *CDataPack::GetMemory() const