/************************************************************************
 IniFile
 created:  Willem de jonge, willem@argcargv.com, 03/2003
 modified: Bob Kuehne, rpk@blue-newt.com, 09/2005
************************************************************************/

#ifndef INIFILE_H
#define INIFILE_H

#include <vector>
#include <string>

struct IniLine
{
	IniLine(const std::string& key, const std::string& val, const std::string& line)
		: m_key(key), m_val(val), m_line(line) {}

	std::string m_key;   // key name if any
	std::string m_val;   // value part (trimmed), valid when m_key not empty
	std::string m_line;  // original line from file including comments
};

class IniSection
{
public:
	IniSection(const std::string& name) : m_name(name) {}

	const std::string& name() const { return m_name; }
	void addLine(const std::string& line);
	bool getValStr(const std::string& key, std::string& val) const;

private:
	std::string extractKey(const std::string& line);
	std::string extractVal(const std::string& line);

	std::string m_name;
	typedef std::vector<IniLine> Lines_t;
	Lines_t m_lines;
};

class IniFile
{
public:
	IniFile() {}

	bool read(const std::string& fileName);

	void get(const std::string& section, const std::string& key, bool& val, bool def);
	void get(const std::string& section, const std::string& key, float& val, float def);
	void get(const std::string& section, const std::string& key, std::string& val, const std::string& def);

private:
	bool getValStr(const std::string& section, const std::string& key, std::string& val);

	typedef std::vector<IniSection> Sections_t;
	Sections_t m_sections;
};

#endif // INIFILE_H
