120 lines
2.9 KiB
C++
120 lines
2.9 KiB
C++
#include <fstream>
|
|
#include "PropertyLoader.h"
|
|
#include <algorithm>
|
|
#include <stdio.h>
|
|
#include <iterator>
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
PropertyLoader* PropertyLoader::mInstance = NULL;
|
|
|
|
void PropertyLoader::Create() {
|
|
if (mInstance == NULL)
|
|
mInstance = new PropertyLoader();
|
|
|
|
}
|
|
|
|
void PropertyLoader::Destroy() {
|
|
if (mInstance != NULL)
|
|
delete mInstance;
|
|
mInstance = NULL;
|
|
}
|
|
|
|
PropertyLoader* PropertyLoader::Instance() {
|
|
return mInstance;
|
|
}
|
|
|
|
void PropertyLoader::Update() {
|
|
for (std::string propertyFile : mPropertyFiles)
|
|
LoadProperties(propertyFile.c_str());
|
|
}
|
|
|
|
void PropertyLoader::Clear()
|
|
{
|
|
mProperties.clear();
|
|
mPropertyFiles.clear();
|
|
}
|
|
|
|
void PropertyLoader::AddPropertyFile(std::string file)
|
|
{
|
|
mPropertyFiles.push_back(file);
|
|
Update();
|
|
}
|
|
|
|
//************************************
|
|
// Get property value based on its identifier
|
|
//************************************
|
|
std::string PropertyLoader::GetProperty(const std::string& id) {
|
|
auto res = mProperties.find(id);
|
|
if (res == mProperties.end()) return "";
|
|
return res->second;
|
|
}
|
|
|
|
unsigned PropertyLoader::GetIntProperty(const std::string& id)
|
|
{
|
|
return std::stoi(GetProperty(id));
|
|
}
|
|
|
|
float PropertyLoader::GetFloatProperty(const std::string& id)
|
|
{
|
|
return std::stof(GetProperty(id));
|
|
}
|
|
|
|
bool PropertyLoader::GetBoolProperty(const std::string& id)
|
|
{
|
|
std::string prop = GetProperty(id);
|
|
std::string upperProp;
|
|
std::transform(prop.begin(), prop.end(), std::back_inserter(upperProp), toupper);
|
|
return prop == "1" || upperProp == "TRUE";
|
|
}
|
|
|
|
std::vector<std::string> PropertyLoader::GetListProperty(const std::string &id)
|
|
{
|
|
bool found = true;
|
|
size_t index = 0;
|
|
std::vector<std::string> res = std::vector<std::string>();
|
|
while (found)
|
|
{
|
|
std::string toSearch = id + std::to_string(index);
|
|
std::string prop = GetProperty(toSearch);
|
|
found = prop != "";
|
|
if (found) res.push_back(prop);
|
|
index++;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
PropertyLoader::PropertyLoader() {
|
|
LoadDefaults();
|
|
Update();
|
|
}
|
|
|
|
PropertyLoader::~PropertyLoader() {
|
|
mProperties.clear();
|
|
}
|
|
|
|
void PropertyLoader::LoadProperties(const char* fileName) {
|
|
std::ifstream properties(fileName);
|
|
std::string property;
|
|
while (getline(properties, property)) {
|
|
std::size_t pos = property.find(" ");
|
|
if (pos < property.npos) {
|
|
std::string left = property.substr(0, pos);
|
|
std::string right = property.substr(pos + 1);
|
|
// Remove newline characters
|
|
right.erase(std::remove(right.begin(), right.end(), '\r'), right.end());
|
|
right.erase(std::remove(right.begin(), right.end(), '\n'), right.end());
|
|
std::pair<std::map<std::string, std::string>::iterator, bool> loc = mProperties.insert(std::pair<std::string, std::string>(left, right));
|
|
if (!loc.second)
|
|
loc.first->second = right;
|
|
}
|
|
}
|
|
properties.close();
|
|
}
|
|
|
|
void PropertyLoader::LoadDefaults() {
|
|
mPropertyFiles.clear();
|
|
mPropertyFiles.push_back(std::string("shaders/shader_properties.txt"));
|
|
mPropertyFiles.push_back(std::string("properties.txt"));
|
|
}
|