// EdIRGlob.cpp : implementation file
//

#include "stdafx.h"

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRGlob.hpp"

#include "EdIRStrg.hpp"

#include "acp_base.h"
#include "ITF.h"

//ANNECY CB
#include "EDIRBis.hpp"
#include "EDIRFlx.hpp"
BOOL gbFirstShowBrain = FALSE;
//END ANNECY

#define M_ISALNUM(c)				(isalnum(c) || (c)=='_' || (c)=='-' || (c)=='.')

/**********************************************************************************/
long fn_lSkipRightAlphaNumChar(CString &csString,long lIndex)
{
	long	lResultIndex=lIndex;
	long	lStringLen=csString.GetLength();

	if(lIndex<lStringLen)
	{
		while(lResultIndex<=lStringLen-1 && M_ISALNUM(csString[int(lResultIndex)]))
			lResultIndex++;
	}

	return lResultIndex;
}

/**********************************************************************************/
long fn_lSkipRightNonAlphaNumChar(CString &csString,long lIndex)
{
	long	lResultIndex=lIndex;
	long	lStringLen=csString.GetLength();

	if(lIndex<lStringLen)
	{
		while(lResultIndex<=lStringLen-1 && !M_ISALNUM(csString[int(lResultIndex)]))
			lResultIndex++;
	}

	return lResultIndex;
}

/*************************************************************************/
CString fn_csGetNextWord(CString &csString,long &lIndex)
{
#if 0
	long	lWordStartIndex=fn_lSkipRightNonAlphaNumChar(csString,lIndex);
	long	lWordEndIndex=fn_lSkipRightAlphaNumChar(csString,lWordStartIndex);

	lIndex=lWordEndIndex;

	return csString.Mid(lWordStartIndex,lWordEndIndex-lWordStartIndex);
#endif
	char	mot[4096];
	long	lmot = 0;
	LPCSTR	pin = (LPCSTR) csString;
	while(pin[lIndex] && !M_ISALNUM(pin[lIndex])) lIndex++;
	while(M_ISALNUM(pin[lIndex])) mot[lmot++] = pin[lIndex++];
	mot[lmot] = '\0';
	return mot;
}

/*************************************************************************/
//ANNECY CB
CString fn_csGetNextNonWord(CString &csString,long &lIndex)
//CString fn_csGetNextNonWord(CString csString,long &lIndex)
//END
{
	long	lWordStartIndex=lIndex;
	long	lWordEndIndex=fn_lSkipRightNonAlphaNumChar(csString,lIndex);

	lIndex=lWordEndIndex;

	return csString.Mid(lWordStartIndex,lWordEndIndex-lWordStartIndex);
}

/**********************************************************************************/
BOOL fn_bGetNextLine(CString csText,long &lIndex,CString &csLine)
{
	CString	csTemp=csText.Right(csText.GetLength()-lIndex);
	BOOL		bRes=FALSE;

	long	lTempIndex=csTemp.Find('\n');
	if(lTempIndex!=-1)
	{
		csLine=csTemp.Left(lTempIndex-1);
		lIndex+=lTempIndex+1;

		bRes=TRUE;
	}
	else
	if(!csTemp.IsEmpty())
	{
		csLine=csTemp;
		lIndex=csText.GetLength();

		bRes=TRUE;
	}
	else
		bRes=FALSE;

	return bRes;
}

/**********************************************************************************/

BOOL fn_bGetLine(CString csText,long lLineNumber,CString &csLine)
{
	CString	csTemp=csText;
	long		lCurLine=0;
	long		lIndex;
	long		lOldIndex=0;
	long		lStartIndex=-1;
	long		lEndIndex=-1;
	while((lIndex=csTemp.Find('\n'))!=-1 && lStartIndex==-1)
	{
		csTemp=csTemp.Right(csTemp.GetLength()-lIndex-1);

		if(lCurLine==lLineNumber)
		{
			lStartIndex=lOldIndex;
			//Subbs 2 because '\n' is two character
			lEndIndex=lOldIndex+lIndex-2;
		}

		lCurLine++;

		//Adds lIndex+1 because '\n' is two character
		lOldIndex+=lIndex+1;
	}

	if(lStartIndex==-1)
	{
		if(lCurLine==lLineNumber)
		{
			lStartIndex=lOldIndex;
			lEndIndex=csText.GetLength();
		}
		else
			return FALSE;
	}

	csLine=csText.Mid(lStartIndex,lEndIndex-lStartIndex+1);

	return TRUE;
}

/**********************************************************************************/
CString	fn_csReplaceChar(CString csText,char cOldChar,char cNewChar)
{
	CString	csNewText=csText;

	long	lIndex;
	long	lNewIndex=0;
	while((lIndex=csText.Find(cOldChar))!=-1)
	{
		csText=csText.Right(csText.GetLength()-lIndex-1);

		lNewIndex+=lIndex+1;
		csNewText.SetAt(lNewIndex-1,cNewChar);
	}

	return csNewText;
}

/**********************************************************************************/
char *fn_pc_StrStrWordOnly(char *pcStart,char *pcString)
{
	long	lTextLen=strlen(pcStart);
	long	lStringLen=strlen(pcString);
	char	*pcIndex=pcStart;
	BOOL	bFound=FALSE;
	while(pcIndex!=NULL && !bFound)
	{
		bFound=	(pcIndex>pcStart) &&
						(M_ISALNUM(pcIndex[-1])==FALSE) &&
						(pcIndex+lStringLen<=pcStart+lTextLen) &&
						(M_ISALNUM(pcIndex[lStringLen])==FALSE);

		if(!bFound)
			pcIndex++;
	}

	return pcIndex;
}

/**********************************************************************************/
char *fn_pc_StrStrNoCase(char *pcStart,char *pcString)
{
	long	lTextLen=strlen(pcStart);
	long	lStringLen=strlen(pcString);

	long	lIndex=0;
	char	*pcIndex=pcStart;
	while(lIndex<lTextLen)
	{
		if(strnicmp(pcIndex,pcString,lStringLen)==0)
			return pcIndex;

		lIndex++;
		pcIndex++;
	}

	return NULL;
}

/**********************************************************************************/
CString	fn_csReplaceString(CString csText,CString csOldString,CString csNewString)
{
	CString	csNewText;

	long	lTextLen=csText.GetLength();
	long	lOldStringLen=csOldString.GetLength();
	if(lTextLen!=0)
	{
		char	*pcText=new char[lTextLen+1];
		strcpy(pcText,LPCTSTR(csText));

		/* Processes replacement */
		char	*pcIndex;
		char	*pcOldIndex=pcText;

		pcIndex=fn_pc_StrStrNoCase(pcOldIndex,M_MAKECHAR(csOldString));

		while(pcIndex!=NULL)
		{
			//Adds old text
			char	cOldChar=*pcIndex;

			*pcIndex=0;
			csNewText+=pcOldIndex;
			*pcIndex=cOldChar;

			//Replaces old text with the new one
			csNewText+=csNewString;

			pcIndex+=csOldString.GetLength();

			pcOldIndex=pcIndex;
			pcIndex=fn_pc_StrStrNoCase(pcOldIndex,M_MAKECHAR(csOldString));
		}

		csNewText+=pcOldIndex;
	}

	return csNewText;
}

/**********************************************************************************/
void fn_vDisplayInfoInStatusBar(CString csMsg,int iInfoType)
{
	if((iInfoType == C_STATUS_ERROR) && (g_clParse.m_bNoError==FALSE))
	{
		csMsg = "ERROR: "+g_clParse.m_csMsgError+" ("+g_clLex.yytext+")";
		M_GetMainWnd()->UpdateStatus(M_MAKECHAR(csMsg),C_STATUSPANE_INFOS,iInfoType);
		g_clParse.m_bNoError=TRUE;
	}
	else
		M_GetMainWnd()->UpdateStatus(M_MAKECHAR(csMsg),C_STATUSPANE_INFOS,iInfoType);
}

#endif //D_ED_IR_ACTIVE
