//#include <iostream.h>
#include <string>
#include <iterator>
#include <vector>
#include "util.h"

using namespace std;

//_-_-_-_-_-_-_-_-_-_-_-_-_-_
// Constructor.
//_-_-_-_-_-_-_-_-_-_-_-_-_-_

util::util(string Request)
{
	saoriReq = getSAORIREQ(Request);
	saoriVer = getSAORIVER(Request);
	saoriSecLV = getSAORISECLV(Request);
	saoriChar = getSAORICHAR(Request);
	saoriSender = getSAORISENDER(Request);
	saoriArgSize = getSAORIARG(Request);
}

//_-_-_-_-_-_-_-_-_-_-_-_-_-_
// The "IN" functions.
//_-_-_-_-_-_-_-_-_-_-_-_-_-_

string util::getSAORIREQ(string Request)
{
	string ret;
	endChar = Request.find("\r\n",0);
	if ( endChar != npos )
	{
		indexChar = Request.find_last_of(" ", endChar);
		ret = Request.substr( 0, indexChar );
	}
	else
	{
		ret = "EXECUTE";
	}
	return (ret);
}

string util::getSAORIVER(string Request)
{
	string ret;
	indexChar = Request.find(EXECUTE,0);
	if ( indexChar != npos )
	{
		endChar = Request.find("\r\n", indexChar);
		ret = EXECUTE;
		ret = Request.substr( indexChar + ret.size(), endChar - (indexChar + ret.size()) );
	}
	else
	{
		ret = "SAORI/1.0";
	}
	return (ret);
}

string util::getSAORISECLV(string Request)
{
	string ret;
	indexChar = Request.find(SECURITYLV,0);
	if ( indexChar != npos )
	{
		endChar = Request.find("\r\n", indexChar);
		ret = SECURITYLV;
		ret = Request.substr( indexChar + ret.size(), endChar - (indexChar + ret.size()) );
	}
	else
	{
		ret = "Local";
	}
	return (ret);
}

string util::getSAORICHAR(string Request)
{
	string ret;
	indexChar = Request.find(CHARSET,0);
	if ( indexChar != npos )
	{
		endChar = Request.find("\r\n", indexChar);
		ret = CHARSET;
		ret = Request.substr( indexChar + ret.size(), endChar - (indexChar + ret.size()) );
	}
	else
	{
		ret = "Shift_JIS";
	}
	return (ret);
}

string util::getSAORISENDER(string Request)
{
	string ret;
	indexChar = Request.find(SENDER,0);
	if ( indexChar != npos )
	{
		endChar = Request.find("\r\n", indexChar);
		ret = SENDER;
		ret = Request.substr( indexChar + ret.size(), endChar - (indexChar + ret.size()) );
	}
	else
	{
		ret = "saori_host";
	}
	return (ret);
}

int util::getSAORIARG(string Request)
{
	string ret;
	bool finish = false;
	for (int z = 0; finish != true; z++)
	{
		ret = ARGUMENT;
		ret = ret + util::IntToString(z);
		ret = ret + ": ";
		indexChar = Request.find(ret, 0);		
		if ( indexChar != npos )
		{
			endChar = Request.find("\r\n", indexChar);
			saoriArg.push_back (Request.substr( indexChar + ret.size(), endChar - (indexChar + ret.size()) ));
		}
		else
		{
			finish = true;
		}
	}
	if (saoriArg.size() > 0)
	{
		return (saoriArg.size());
	}
	else
	{
		return (0);
	}
}

//_-_-_-_-_-_-_-_-_-_-_-_-_-_
// The "OUT" functions.
//_-_-_-_-_-_-_-_-_-_-_-_-_-_

string util::showSAORIREQ()
{
	return (saoriReq);
}

string util::showSAORIVER()
{
	return (saoriVer);
}

string util::showSAORISECLV()
{
	return (saoriSecLV);
}

string util::showSAORICHAR()
{
	return (saoriChar);
}

string util::showSAORISENDER()
{
	return (saoriSender);
}

string util::showSAORIARG(int Request)
{
	if (Request < saoriArgSize)
	{
		return (saoriArg[Request]);
	}
	else
	{
		return ("No Argument Found");
	}
}

//_-_-_-_-_-_-_-_-_-_-_-_-_-_
// Function IntLen is from "Planet Source Code"
// By Morti.
//_-_-_-_-_-_-_-_-_-_-_-_-_-_

int util::IntLen(int Request)
{
	int div = 1;
	int len = 1;
	do
	{
		div *= 10;
		if ((int) ( (double)(Request) / (double)(div) ) == 0)
		{
			return len;
		}
		len += 1;
	} while (true);
}

//_-_-_-_-_-_-_-_-_-_-_-_-_-_
// Function IntToString is from "Planet Source Code"
// By Morti.
//_-_-_-_-_-_-_-_-_-_-_-_-_-_

string util::IntToString(int Request)
{
    string number;
    int temp;
    bool neg = false;	//Proof if the number has a negiote sign

	if ((Request * (-1)) > Request)
	{
		Request *= -1;
		neg = true;
	}

	for (int z = util::IntLen(Request); z >= 1; z--)
	{
        //Modulo to get the last number 
		temp = Request % 10;
		//add Number to the String
		number = ((char)(temp + 48))+ number;
		//Substract the last number from the Int
        Request = (Request - temp) /10;
        temp *= 10 ;
    }
    //Set the sign again
    if (neg==true)
	{
		number = "-" + number;
	}
    return number;
}