/* -*- C++ -*- */
#ifndef __anonymous_Config_Header_File__
#define __anonymous_Config_Header_File__

#include "types.h"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <map>
#include "exceptions.h"

namespace fft
{
   /**
    * Class providing read access to config file.
    */
   class Config
   {
      public:
		
	 /**
	  * An item in config file
	  */
	 struct Item
	 {
	       /**
		* Name of an item
		*/
	       std::string name;
			
	       /**
		* Value of an item
		*/
	       std::string value;
		
	       /**
		* Constructor of item
		*/
	       Item(std::string aName, std::string aValue):
		  name(aName),value(aValue){}
			
	       /**
		* Converts item into antoher type
		* @note To get string interpretation of value, use value 
		* attribute directly instead of calling this function.
		*/
	       template <class T>
	       void to(T& ret) const  {
		  std::istringstream iss(value);
		  iss >> ret;
	       }

	       //template <>
	       void to(String& ret) const { ret = value; }


	       bool toBool() const
	       {
		  bool ret = false;
		  if (value == "yes" || value == "1" || value == "true")
		  {
		     ret = true;
		  }
		  else if (value == "no" || value == "0" || value == "false")
		  {
		     ret = false;
		  }
		  else
		  {
		     std::cerr << "No such bool value" << value  << std::endl;
		  }
		  return ret;
	       }



			
		
	 };
		
	 /**
	  * Config constructor. Reads configuration file.
	  * @param fileName Path to configuration file
	  */
	 Config(const String& fileName);
		
	 /**
	  * Access operator to items
	  */
	 const Item& operator[](std::string name) const;

	 /**
	  * Test if named item is in config file
	  */
	 bool hasKey(std::string name) const;
		
      private:
	 /**
	  * Filename of config file
	  */
	 String fileName;
	 /**
	  * Storage of items
	  */
	 std::map<std::string, Item> data;
	
	 /**
	  * Removes beginning and ending spaces of string.
	  */
	 static std::string trim(std::string str);
   };
}



#endif //__anonymous_Config_Header_File__
