core-legacy/models/period_list.cc

71 lines
1.5 KiB
C++
Raw Normal View History

2019-07-19 09:41:39 +00:00
#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;
}
}
2019-07-20 12:51:45 +00:00
period_list::period_list(period **periods, uint16_t length)
2019-07-19 09:41:39 +00:00
{
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*
2020-04-18 19:29:52 +00:00
period_list::to_blob()
2019-07-19 09:41:39 +00:00
{
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;
}