VideoCodec/Configuration/ConfigurationElement.cpp
#include "ConfigurationElement.h"
using namespace std;
namespace VideoCodec
{
ConfigurationElement::ConfigurationElement()
{
this->type = CONFIGURATION_EMPTY;
}
ConfigurationElement::~ConfigurationElement()
{
if (this->type == CONFIGURATION_PAIRS)
{
// Clean up each element.
for (configurationPairs::iterator i = this->pairs->begin(); i != this->pairs->end(); i++)
{
delete i->first;
delete i->second;
}
// Clean up the map.
delete this->pairs;
}
else if (this->type == CONFIGURATION_LIST)
{
// Clean up each element.
for (unsigned short int i = 0; i < this->list->size(); i++)
delete (*this->list)[i];
// Clean up the map.
delete this->list;
}
else if (this->type == CONFIGURATION_STRING)
delete this->stringValue;
else if (this->type == CONFIGURATION_INTEGER)
delete this->intValue;
else if (this->type == CONFIGURATION_FLOAT)
delete this->floatValue;
}
ConfigurationElement* ConfigurationElement::GetValue(string path)
{
if (path.empty())
return this;
int dotIndex = path.find('.', 0);
dotIndex = (dotIndex == -1 ? path.length() : dotIndex);
// Decompose the path.
switch (this->type)
{
case CONFIGURATION_PAIRS:
{
string next = (path.substr(0, dotIndex));
if (this->pairs->find(&next) != this->pairs->end())
return this->pairs->find(&next)->second->GetValue((dotIndex == (int)path.length()) ? std::string("") : path.substr(dotIndex + 1));
else
return NULL;
}
case CONFIGURATION_LIST:
{
int listIndex = -1;
sscanf(path.substr(0, dotIndex).c_str(), "%d", &listIndex);
if (listIndex == -1 || listIndex >= (int)this->list->size())
return NULL;
return this->list->at(listIndex);
}
default:
return NULL;
}
}
int ConfigurationElement::GetType()
{
return this->type;
}
void ConfigurationElement::SetPairs(configurationPairs* pairs)
{
this->pairs = pairs;
this->type = CONFIGURATION_PAIRS;
}
void ConfigurationElement::SetList(vector<ConfigurationElement*>* list)
{
this->list = list;
this->type = CONFIGURATION_LIST;
}
void ConfigurationElement::SetString(std::string* value)
{
this->stringValue = value;
this->type = CONFIGURATION_STRING;
}
void ConfigurationElement::SetInteger(int* value)
{
this->intValue = value;
this->type = CONFIGURATION_INTEGER;
}
void ConfigurationElement::SetFloat(float* value)
{
this->floatValue = value;
this->type = CONFIGURATION_FLOAT;
}
configurationPairs* ConfigurationElement::GetPairs()
{
return this->pairs;
}
vector<ConfigurationElement*>* ConfigurationElement::GetList()
{
return this->list;
}
std::string* ConfigurationElement::GetString()
{
return this->stringValue;
}
int* ConfigurationElement::GetInteger()
{
return this->intValue;
}
float* ConfigurationElement::GetFloat()
{
return this->floatValue;
}
}