Files
2025-03-02 16:15:54 -07:00

87 lines
1.4 KiB
C++

#include "data_store.h"
#include <xtl.h>
#include <stdint.h>
data_store::data_store()
{
mSize = 0;
mMaxSize = DATA_SIZE_INCREMENT;
mValueContainer = (unsigned char*)malloc(mMaxSize);
if (mValueContainer == NULL)
{
mMaxSize = 0;
return;
}
}
data_store::~data_store()
{
free(mValueContainer);
}
void data_store::add(unsigned char value)
{
if (mSize == mMaxSize)
{
unsigned char* reallocedValueContainers = (unsigned char*)realloc(mValueContainer, (mMaxSize + DATA_SIZE_INCREMENT));
if (reallocedValueContainers == NULL)
{
return;
}
mMaxSize += DATA_SIZE_INCREMENT;
mValueContainer = reallocedValueContainers;
}
mValueContainer[mSize] = value;
mSize++;
}
void data_store::add_range(unsigned char* buffer, uint32_t offset, uint32_t length)
{
for (uint32_t i = 0; i < length; i++)
{
add(buffer[offset + i]);
}
}
void data_store::remove(uint32_t index)
{
for (uint32_t j = index; j < mSize - 1; j++)
{
mValueContainer[j] = mValueContainer[j + 1];
}
mSize--;
}
void data_store::remove_range(uint32_t index, uint32_t count)
{
for (uint32_t j = index; j < mSize - count; j++)
{
mValueContainer[j] = mValueContainer[j + count];
}
mSize -= count;
}
unsigned char data_store::get(uint32_t index)
{
return mValueContainer[index];
}
unsigned char* data_store::get_all()
{
return mValueContainer;
}
uint32_t data_store::count()
{
return mSize;
}
void data_store::clear()
{
mSize = 0;
}