// =============================================================================
// Zboard Config Utility
// Copyright (C) 2007  Matt Razza

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
// -------------------------------------------------------------------------------
// This file manages settings files
// ===============================================================================

// Load header file
#include "zboard-settings.h"

// Constructor
settingMan::settingMan(std::string file)
{
	sFile = file; //Sets the file
}

// Destructor
settingMan::~settingMan(void)
{
}

// ========================================================================
// General Functions
// ========================================================================

// Returns the value of a given setting
std::string settingMan::getSetting(std::string setting)
{
	std::ifstream fileStream(sFile.c_str()); //Open the file
	std::string sTemp = "", sValue = "ZINVALIDZ"; //Temp var

	// Let's find the setting
	while (fileStream >> sTemp)
	{
		if (splitName(sTemp) == setting) //Get the name of the setting
		{
			sValue = splitValue(sTemp); //Get the value
			break;
		}
	}

	fileStream.close(); //Close the file

	return sValue;
}

// Returns the name of a setting by it's ID
std::string settingMan::getSettingNameByID(unsigned int id)
{
	std::ifstream fileStream(sFile.c_str()); //Open the file
	std::string sTemp = "", sValue = "ZINVALIDZ"; //Temp var
	unsigned int iLoop = 0;

	while (iLoop < id && std::getline(fileStream, sTemp))
	{
		if (getSubStr(sTemp, 0, 1) != "#" && getSubStr(sTemp, 0, 1) != "")
		{
			iLoop++;
			if (iLoop == id)
				sValue = splitName(sTemp);
		}
	}

	fileStream.close(); //Close the file

	return sValue;
}

// ========================================================================
// Private Functions
// ========================================================================

// Gets the name of a given setting
std::string settingMan::splitName(std::string sFull)
{
	int posOfSign;
	posOfSign = getPosOf(sFull, "="); //Get pos of =

	return getSubStr(sFull, 0, posOfSign);
}

// Gets the value of a given setting
std::string settingMan::splitValue(std::string sFull)
{
	int posOfSign;
	posOfSign = getPosOf(sFull, "="); //Get pos of =

	return getSubStr(sFull, posOfSign + 1, int(sFull.length()) - (posOfSign + 1));
}

// Gets text from start to end of string base
std::string settingMan::getSubStr(std::string base, int start, int len)
{
	std::string sRet(base, start, len);
	return sRet;
}

// Gets the first occorence of "what" in "base"
int settingMan::getPosOf(std::string base, std::string what)
{
	for (unsigned int x = 0; x < base.length(); x++)
	{
		if (getSubStr(base, x, int(what.length())) == what)
		{
			return x;
		}
	}

	return 0;
}
