2023-08-13 21:15:30 +02:00

49 lines
712 B
C++

#pragma once
class Buffer
{
public:
unsigned char* buffer = nullptr;
int taken = 0;
int size = 0;
Buffer(int size)
{
this->size = size;
this->buffer = new unsigned char[size];
}
Buffer() {
size = 0;
buffer = nullptr;
}
~Buffer()
{
if (buffer) delete[] buffer;
}
void resize(int new_size)
{
unsigned char* new_buffer = (unsigned char*)realloc(buffer, new_size);
if (!new_buffer)
{
printf_s("Error resizing buffer\n");
return;
}
buffer = new_buffer;
size = new_size;
}
void add(void* data, int data_size)
{
if (taken + data_size > size)
{
resize(size + data_size);
}
memcpy_s(buffer + taken, size - taken, data, data_size);
taken += data_size;
}
};