#include "stdafx.h"
#include "War3TypesAndConstants.h"
#include "TrigString.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTrigString::CTrigString()
{
	m_sStrValue	= TSTR_UNKNOWN_VALUE;
	m_iTrigID	= TSTR_UNKNOWN_ID;
}

CTrigString::CTrigString(CString str, int ID)
{
	m_sStrValue	= str;
	m_iTrigID	= ID;
}

CTrigString::~CTrigString()
{

}


/*
GetTrigStringID
***************
 Returns the ID number of the trigger string.

In:
 none

Out:
 none

Return:
 int: ID value

*/
int CTrigString::GetTrigStringID()
{
	return m_iTrigID;
}

/*
SetTrigStringID
***************
 Sets the ID number of the trigger string to ID if ID is positive.

In:
 int ID: ID number

Out:
 none

Return:
 none

*/
void CTrigString::SetTrigStringID(int ID)
{
	if (ID >= 0)
	{m_iTrigID = ID;}
	//else
	//{TRACE("Warning: SetTrigID: invalid ID specified = %i\n", ID);}
}


/*
GetTrigStringName
*****************
 Returns a trigger string that can be used in a w3i or in jass file.

In:
 none

Out:
 none

Return:
 CString:
  -a string like "TRIGSTR_053" if ID is not negative (here =53) and the string is not null;
  -a null string if the string contains nothing;
  -the string content if it's not null and the ID is negative.

*/
CString CTrigString::GetTrigStringName()
{
	CString str;
	char *pBuf;
	if (m_iTrigID >= 0)
	{
		pBuf = new char[strlen(TSTR_TRIGSTR)+10];
		sprintf(pBuf,"%s%03i\0",TSTR_TRIGSTR,m_iTrigID);
		str = pBuf;
		delete []pBuf;
	}
	else
	{
		str = "";
	}
	return str;
}


/*
SetTrigStringValue
******************
Sets the trigger string string content.

In:
 CString str: new string value to put for this trigger string.

Out:
 none

Return:
 none

*/
void CTrigString::SetTrigStringValue(CString str)
{
	m_sStrValue = str;
}


/*
GetTrigStringValue
******************
Returns the trigger string content.

In:
 none

Out:
 none

Return:
 CString: the trigger string string value.

*/
CString CTrigString::GetTrigStringValue()
{
	return m_sStrValue;
}


/*
static GetTrigStringValue
*************************
This function intend to get the trigger string number from a string.
Returns a positive number which is the trigger ID number if the string passed as argument
is a trigger string, returns a negative value (-1) otherwise.

In:
 CString trigstr: a string

Out:
 none

Return:
 int: the trigger ID number (which can be 0) if str is a trigger string, -1 otherwise.

*/
int CTrigString::GetTrigStringID(CString trigstr)
{
	CString str;
	str = trigstr;
	int i = -1;

	if (str.length() > 0)
	{
		try
		{
			// tirgger string command is case sensitive
			i = strncmp(TSTR_TRIGSTR, str.c_str(), TSTR_TRIGSTR_LEN);
			if (!i)
			{
				str = str.substr(0,TSTR_TRIGSTR_LEN);
				i = atoi(str.c_str());
			}
			else
			{
				i = -1;
			}
		}
		catch(...)
		{
			i = -1;
			//TRACE("Error: GetTrigStringID: unable to create a buffer!\n");
		}
	}
	return i;
}

/*
UnsetTrigString
***************
Resets trigger string content and ID.

In:
 none

Out:
 none

Return:
 none

*/
void CTrigString::UnsetTrigString()
{
	m_sStrValue	= TSTR_UNKNOWN_VALUE;
	m_iTrigID	= TSTR_UNKNOWN_ID;
}


/*
IsTrigStringName
****************
Says if yes or no, a string is a trigger string name ("TRIGSTR_xxx").

In:
 CString str: the string to check

Out:
 none

Return:
 BOOL: TRUE if str is a trigger string name, FALSE otherwise

*/
BOOL CTrigString::IsTrigStringName(CString str)
{
	BOOL ret = FALSE;
	CString strtest = str;

	if (strtest.length() > TSTR_TRIGSTR_LEN)
	{
		if (!strncmp(TSTR_TRIGSTR, strtest.c_str(), TSTR_TRIGSTR_LEN))
		{
			ret = TRUE;
		}
	}
	return ret;
}
