#include "stdafx.h"

#include "Additional_functions.h"
#include "DB_Cost.h"

const int CONST_MAX_SIZE_MASS = 20;
string pMassStr[CONST_MAX_SIZE_MASS];
int vQ_Str=0;

template <class T>
bool fSet(T *pVar, T vSet)
{
	if (pVar == NULL) return FALSE;
	switch(typeid(T).hash_code)
	{
	case typeid(string).hash_code:
		*pVar = fCpyStr(vSet);
		break;
	default:
		*pVar = vSet;
	}
	return TRUE;
}

template <class T>
T fGet(T *pVar)
{
	if (pVar == NULL) return NULL;
	return *pVar;
}

string fCharToString(int vHex)
{
	char vChar = (char)vHex;
	int vLenStr = 2;
	string vStrOut = fCreateString2(vLenStr);//new char[vLenStr];
	char *pStr = vStrOut;
	*pStr = vChar;
	pStr++;
	*pStr = '\0';
	return vStrOut;
}

string fCpyStr(string vStr)
{
	if (vStr == NULL) return NULL;
	int vLenStr = strlen(vStr) + 1;
	string vStrOut = fCreateString2(vLenStr);//new char[vLenStr];
	char *pStr = vStrOut;
	memcpy(pStr,vStr,strlen(vStr));
	pStr += strlen(vStr);
	*pStr = '\0';
	return vStrOut;
}

bool fCmpStr(string vStr1, string vStr2)
{
	if (vStr1 == NULL || vStr2 == NULL) return FALSE;
	int vLenStrMin = 0;
	int vLenStr1 = strlen(vStr1);
	int vLenStr2 = strlen(vStr2);
	if (vLenStr1 < vLenStr2)
	{
		vLenStrMin = vLenStr1;
	}
	else
	{
		vLenStrMin = vLenStr2;
	}
	return memcmp(vStr1,vStr2,vLenStrMin) == 0;
}

string fAddString(bool vClean, string vFirst, ...)
{
	if (vFirst == NULL) return NULL;
	va_list ap;
	int vLenStr = 0;
	int vLenStrNew = 0;
	char *p;
    char *s;
	string vStrNew = NULL;

	vLenStr = strlen(vFirst);
    vLenStrNew += vLenStr;
	va_start(ap, vFirst);
	while (true)
	{
		s = va_arg(ap, string);
		if (!s) break;
		vLenStr = strlen(s);
        vLenStrNew += vLenStr;
	}
    va_end(ap);
	vStrNew = fCreateString(vLenStr+1,vClean);
	p = vStrNew;
	vLenStr = strlen(vFirst);
    memcpy(p,vFirst,vLenStr);
	p += vLenStr;
	va_start(ap, vFirst);
	while (true)
	{
		s = va_arg(ap, string);
		if (!s) break;
		vLenStr = strlen(s);
        memcpy(p,s,vLenStr);
		p += vLenStr;
	}
    va_end(ap);
	*p = '\0';
	return vStrNew;
}

string fAddString2(string vFirst, ...)
{
	if (vFirst == NULL) return NULL;
	va_list ap;
	int vLenStr = 0;
	int vLenStrNew = 0;
	char *p;
    char *s;
	string vStrNew = NULL;

	vLenStr = strlen(vFirst);
    vLenStrNew += vLenStr;
	va_start(ap, vFirst);
	while (true)
	{
		s = va_arg(ap, string);
		if (!s) break;
		vLenStr = strlen(s);
        vLenStrNew += vLenStr;
	}
    va_end(ap);
	vStrNew = fCreateString2(vLenStrNew+1);
	p = vStrNew;
	vLenStr = strlen(vFirst);
    memcpy(p,vFirst,vLenStr);
	p += vLenStr;
	va_start(ap, vFirst);
	while (true)
	{
		s = va_arg(ap, string);
		if (!s) break;
		vLenStr = strlen(s);
        memcpy(p,s,vLenStr);
		p += vLenStr;
	}
    va_end(ap);
	*p = '\0';
	return vStrNew;
}

string fCreateStringFmt(bool vClean, char *fmt, ...)
{
	va_list ap;
    char *s;
	int vLenStrNew = 0;
	int vLenStr = 0;
	char *p;
	string vStrNew = NULL;

   va_start(ap, fmt);
    while (*fmt)
        switch (*fmt++) {
        case 's':              /* string */
            s = va_arg(ap, string);
			vLenStr = strlen(s);
            vLenStrNew += vLenStr;
            break;
        }
    va_end(ap);
	vStrNew = fCreateString2(vLenStrNew+1);
	p = vStrNew;
	va_start(ap, fmt);
    while (*fmt)
        switch (*fmt++) {
        case 's':              /* string */
            s = va_arg(ap, string);
			vLenStr = strlen(s);
            memcpy(p,s,vLenStr);
			p += vLenStr;
            break;
        }
    va_end(ap);
	*p = '\0';
	return vStrNew;
}

string fCreateString2(integer vLenStr)
{
	return new char[vLenStr];
	//return (string) malloc(sizeof(char) * vLenStr);
}

void fFreeString2(string vStr)
{
	//if (//(!fNull((unsigned int)vStr)) &&
		//(vStr[0] != 'y') &&
		//(vStr[0] != 'Ý') &&
		//(vStr[0] != (char)0xcd)
	//	)
	//{
		delete[] vStr;
		//free(vStr);
		//delete vStr;
	//}
	//else
	//{
		//vStr = vStr;
	//}
}

string fCreateString(integer vLenStr, bool vClean)
{
	if (vClean == TRUE)
	{
		fCleanString();
	}
	if (vQ_Str < 0 && vQ_Str >= CONST_MAX_SIZE_MASS) return NULL;
	pMassStr[vQ_Str] = fCreateString2(vLenStr);
	vQ_Str++;
	return pMassStr[vQ_Str-1];
}

void fCleanString()
{
	for (int vvi = 0; vvi < vQ_Str; vvi++)
	{
		//delete pMassStr[vvi];
		fFreeString2(pMassStr[vvi]);
		pMassStr[vvi] = NULL;
	}
	vQ_Str = 0;
}

integer GetUnitGoldCost(integer unitid)
{
	integer vGold;
	integer vWood;
	fGetResoursesItem(unitid,&vGold,&vWood);
	return vGold;
}

integer GetUnitWoodCost(integer unitid)
{
	integer vGold;
	integer vWood;
	fGetResoursesItem(unitid,&vGold,&vWood);
	return vWood;
}

bool fNull(unsigned int vUI)
{
	if (vUI == 0x00000000 ||
		vUI == 0x00000001 ||
		vUI == 0xdddddddd ||
		vUI == 0xcdcdcdcd ||
		vUI == 0xcccccccc ||
		vUI == 0xfeeefeee)
	{
		return TRUE;
	}
	return FALSE;
}