/******************************************************************************************
> [KAZOE]                                                                                 <
> Initial Sofware by [Yannick Verdie, Florent Lafarge], Copyright (C) Inria (GEOMETRICA)  <
> All Rights Reserved, 2012, [Version 1.0].                                               <
******************************************************************************************/
#include "ParameterFile.h"


ParameterFile::ParameterFile(const char* n)
{
	 //First open Parameter.ini to get all the parameter
	 name = string(n);

	cout<<"Loading parameters from '" << name<<endl;

	ifstream fic(n,ios::in);
	
	if (!fic.is_open()) printf("error during opening\n");

	string 	lineread ;
	vector<string> tokens;

	map<string,string> listParameters;

	while(getline( fic, lineread ))
	{
		tokens.clear();
		Tokenize(lineread, tokens, "=");

		if (tokens.size() >= 2)
		{
			tokens[0] = delSpaces(tokens[0]);
			tokens[1] = delSpaces(tokens[1]);

			listParameters.insert(pair<string,string>(tokens[0],tokens[1]));
		}
	}

	//now overwrite with user parameters
	map<string,string>::iterator it;
	
	#define INT( name , val )													\
	if ((it = listParameters.find(#name)) != listParameters.end())				\
	{ 																			\
			name =sToT<int>((*it).second);      			                   }\
	else																		\
	{		name = val;														    }


	#define FLOAT( name , val )													\
	if ((it = listParameters.find(#name)) != listParameters.end())				\
	{ 																			\
			name =sToT<float>((*it).second);			                       }\
	else																		\
	{		name = val;														    }


	#define STRING( name , val )												\
	if ((it = listParameters.find(#name)) != listParameters.end())				\
	{ 																			\
			name =(*it).second;			                                       }\
	else																		\
	{		name = val;														    }

	#include "parameter.ini"

	#undef FLOAT
	#undef INT
	#undef STRING

	 fic.close();


}