core-legacy/models/period_list.cc
Tobias Reisinger be84c0e4c2 fix: cleanup
2019-09-08 23:42:48 +02:00

70 lines
1.5 KiB
C++

#include <cstdint>
#include <cstdlib>
#include "period_list.h"
period_list::period_list()
{
this->periods = (period**)malloc(0);
this->length = 0;
}
period_list::period_list(const uint16_t* periods_blob)
{
this->length = periods_blob[0];
this->periods = (period**)malloc(sizeof(period*) * this->length);
for(int i = 0; i < length; i++)
{
auto new_period = new period(periods_blob[(i * 2) + 1], periods_blob[(i * 2) + 2]);
periods[i] = new_period;
}
}
period_list::period_list(period **periods, uint16_t length)
{
this->periods = periods;
this->length = length;
}
period_list::~period_list()
{
for(int i = 0; i < length; i++)
{
delete this->periods[i];
}
free(this->periods);
}
void
period_list::add_period(uint16_t start, uint16_t end)
{
this->length++;
this->periods = (period**)realloc(this->periods, sizeof(period*) * this->length);
this->periods[this->length - 1] = new period(start, end);
}
Json::Value
period_list::to_json()
{
Json::Value result(Json::arrayValue);
for(int i = 0; i < this->length; i++)
{
result.append(this->periods[i]->to_json());
}
return result;
}
uint16_t*
period_list::to_db_blob()
{
auto result = (uint16_t*)malloc(sizeof(uint16_t) * ((this->length * 2) + 1));
result[0] = this->length;
for(int i = 0; i < this->length; i++)
{
result[(i * 2) + 1] = this->periods[i]->start;
result[(i * 2) + 2] = this->periods[i]->end;
}
return result;
}