
//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	FILE	: Parser.cpp
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________


#include "stdinc.h"
#include "parser.h"


//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	DESC.	: constructor
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________

CTextParser::CTextParser()
{
	m_arFile = m_pWord = NULL;				// no file
	m_uLine = 0;							// no file
	ResetDefaults();						// parser default settings
}

//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	DESC.	: desstructor
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________

CTextParser::~CTextParser()
{
	ReleaseFile();
}


//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	DESC.	: parser customization
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________

void CTextParser::SetType( char* szChars, int Value)
{
	for(int i = strlen(szChars) -1; i>=0; i-- )
		m_arCode[(unsigned char)szChars[i]] = Value;
}

//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	DESC.	: parser default settings
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________

void CTextParser::ResetDefaults()
{
	for(int i = 0; i<33; i++)	m_arCode[i] = CTP_SEPARATOR;		// separators
	memset( m_arCode + (unsigned char)'!', 0, (256-(unsigned char)'!')* sizeof(DWORD));	// text
	for( i = 127; i<130; i++)	m_arCode[i] = CTP_SEPARATOR;		// separators
	for( i = 141; i<145; i++)	m_arCode[i] = CTP_SEPARATOR;		// separators
	m_arCode[157] = CTP_SEPARATOR;									// separator
	m_arCode[158] = CTP_SEPARATOR;									// separator
	m_arCode[(unsigned char)'\n'] = CTP_NEWLINE;					// the line feed cahracter is the default line separator
}

//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	DESC.	: file accessing - returns FALSE on failure
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________

BOOL CTextParser::AccessFile( char* szFile )
{
	HANDLE hFile = CreateFile( szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
	if(hFile == INVALID_HANDLE_VALUE) return FALSE;
	ReleaseFile();
	long  length = (long) GetFileSize( hFile, NULL);
	m_pCountStart = m_pCrt = m_pWord = m_arFile = new unsigned char[length + 1];
	m_arFile[length] = 0;
	m_pFileEnd = m_arFile + length + 1;
	DWORD nrC;
	ReadFile( hFile, m_arFile, length + 1, &nrC, NULL);
	CloseHandle( hFile );
	m_nCountCode = m_nCode = CTP_NEWLINE;
	return TRUE;
}

//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	DESC.	: file release
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________

void CTextParser::ReleaseFile()
{
	if(m_arFile) delete[] m_arFile;
	m_arFile = m_pWord = NULL;
	m_uLine = 1;
}

//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	DESC.	: next word
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________
DWORD CTextParser::Advance( DWORD *res)
{
	if(!m_arFile) return CTP_EOF;
	m_pCountStart = m_pCrt;
	DWORD rst, in;
	DWORD& result = res?*res:rst;
	m_nCountCode = result = in = m_nCode;
	while(in)
	{
		switch(in)
		{
		case CTP_EOF:{ 	m_nCode = CTP_EOF; return result|=CTP_EOF; } 
			case CTP_LINE_COMMENT:
				do in = GetUC();
				while(in != CTP_NEWLINE && in != CTP_EOF);
				break;
			case CTP_WORD_WRAPPER:
				m_pWord = m_pCrt;
				while( !(GetUC() & ( CTP_WORD_WRAPPER | CTP_EOF)));
				m_nCode = CTP_SEPARATOR;
				*(m_pCrt-1) = 0;
				return (result &= ~CTP_WORD_WRAPPER);
			case CTP_COMMENT:
				do in = GetUC();
				while(in != CTP_COMMENT && in != CTP_EOF);
			default: in = GetUC();			
		}
		result |= in;
		if( in == CTP_NEWLINE) m_uLine ++;
	}
	m_pWord = m_pCrt-1;
	while( !GetUC() );
	m_nCode = m_arCode[*(m_pCrt-1)];
	*(m_pCrt-1) = 0;
	return result;
}

//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	DESC.	: next word
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________
int CTextParser::GetCodeCount( DWORD Code)
{
	if(!m_arFile) return CTP_EOF;
	int count = 0;
	if(Code == m_nCountCode) count ++;
	DWORD crtCode;

	for(unsigned char* pCrt = m_pCountStart; pCrt<m_pWord; pCrt++)
		switch(crtCode = m_arCode[*pCrt])
		{
			case CTP_LINE_COMMENT:
				for( pCrt++; pCrt<m_pWord && m_arCode[*pCrt] != CTP_NEWLINE; pCrt++);
				break;
			case CTP_COMMENT:
				for( pCrt++; pCrt<m_pWord && m_arCode[*pCrt] != CTP_COMMENT; pCrt++);
				break;
			default:
				if(crtCode == Code) count ++;
		}
	return count;
}
