#include "Config.h"

#include <sstream>
#include "INI_Section.h"

#include "MemoryManager/debugNew.h"

using namespace std;

Config* Config::instance = NULL;

Config::Config(void){
}

Config::~Config(void){
}

Config& Config::getInstance(){
	if (instance == NULL){
		instance = new Config();
	}
	return *instance;
}

void Config::destroy(){
	delete instance;
	instance = NULL;
}

float Config::getVariable(const string& section, const string& variable){
	CINI_Section* fileSection;
	if (fichier.LookupSection(section, fileSection)){
		string result;
		if(fileSection->LookupKey(variable, result)){
			istringstream converter(result);
			float ret;
			if(!(converter >> ret)){
				cerr<<"erreur au chargement de la variable "<<section << ":"<< variable << " : impossible de convertir en float" << endl;
				return 0.0f;
			}
			return ret;
		}else{
			cerr<<"erreur au chargement de la variable "<<section << ":"<< variable << " : impossible de trouver la variable" << endl;
			return 0.0f;
		}
	}else{
		cerr<<"erreur au chargement de la variable "<<section << ":"<< variable << " : impossible de trouver la section" << endl;
		return 0.0f;
	}

}

std::string Config::getValue(const string& section, const string& variable){
	CINI_Section* fileSection;
	if (fichier.LookupSection(section, fileSection)){
		string result;
		if(fileSection->LookupKey(variable, result)){
			return result;
		}else{
			cerr<<"erreur au chargement de la variable "<<section << ":"<< variable << " : impossible de trouver la variable" << endl;
			return "";
		}
	}else{
		cerr<<"erreur au chargement de la variable "<<section << ":"<< variable << " : impossible de trouver la section" << endl;
		return "";
	}
}

bool Config::load(const string path){
	return fichier.Load(path);	
}


