TombEngine/TR5Main/Specific/IO/ChunkId.cpp
Raildex a3433e16cb Deactivated Pool memory
@TODO: Fix Pool
Changed pointers for ChunkIds to smart pointers
2020-08-11 19:18:36 +02:00

54 lines
1.3 KiB
C++

#include "framework.h"
#include "ChunkId.h"
using std::string;
ChunkId::ChunkId(char* bytes, int length) {
if (length == 0) {
m_chunkBytes = NULL;
m_length = 0;
} else {
m_chunkBytes = (byte*)malloc(length);
memcpy(m_chunkBytes, bytes, length);
m_length = length;
}
}
ChunkId::~ChunkId() {
if (m_chunkBytes != NULL)
delete m_chunkBytes;
}
std::unique_ptr<ChunkId> ChunkId::FromString(const char* str) {
return std::make_unique<ChunkId>((char*)str, strlen(str));
}
std::unique_ptr<ChunkId> ChunkId::FromString(string* str) {
return std::make_unique<ChunkId>( (char*)str->c_str(), str->length());
}
std::unique_ptr<ChunkId> ChunkId::FromStream(BaseStream* stream) {
int idLength = LEB128::ReadInt32(stream);
char* buffer = (char*)malloc(idLength);
stream->Read(buffer, idLength);
std::unique_ptr<ChunkId> chunk = std::make_unique<ChunkId>(buffer, idLength);
free(buffer);
return chunk;
}
void ChunkId::ToStream(BaseStream* stream) {
LEB128::Write(stream, m_length);
stream->WriteBytes(m_chunkBytes, m_length);
}
byte* ChunkId::GetBytes() {
return m_chunkBytes;
}
int ChunkId::GetLength() {
return m_length;
}
bool ChunkId::EqualsTo(ChunkId* other) {
if (m_length != other->GetLength())
return false;
return (strncmp((const char*)m_chunkBytes, (const char*)other->GetBytes(), m_length) == 0);
}