// MiddleLanguage.cpp: implementation of the MiddleLanguage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AG_MDA.h"
#include "MiddleLanguage.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

MiddleLanguage::MiddleLanguage()
{

}

MiddleLanguage::~MiddleLanguage()
{

}

void MiddleLanguage::AddTail(CString tag )
{
	m_Stack.Add( new CString( tag ) );
}

BOOL MiddleLanguage::RemoveHead(CString &str)
{
	CString *pStr ;
	if( m_Stack.GetUpperBound() >= 0 )
	{
		pStr = m_Stack.GetAt( 0 );
		m_Stack.RemoveAt( 0 );
		str = *pStr;
		delete pStr;
		return TRUE;
	}
	else return FALSE;
}

void MiddleLanguage::WriteToFile(CString fn)
{
	CFile file;
	if( file.Open( fn , CFile::modeCreate | CFile::modeWrite ) )
	{
		DecodeStack waitQueue;
		CString str;
		int iTabNum = 0;
		while( RemoveHead( str ) )	
		{
			CString *pStr;
			if( waitQueue.GetTop(pStr) )
			{
				if( *pStr == str )
				{//配对期待成功
					waitQueue.Pop( pStr );
					delete pStr;
					iTabNum --;
					WriteTabToFile( file , iTabNum );
				}
				else
				{//配对起始标签失败
					if( str.GetLength() != 0 && str.GetAt( 0 ) == '<' )
					{//is tag
						CString txt;
						txt = str;
						txt.Insert( 1 , '/' );
						waitQueue.Push( new CString( txt ) );
						WriteTabToFile( file , iTabNum );
						iTabNum ++;
					}
					else
					{// is content
						if(str.GetLength() == 0 )
						{
							CString str;
							str = "----------------error------------------\r\n";
							file.Write( str,str.GetLength());
						}
						WriteTabToFile( file , iTabNum);
					}
				}
			}
			else
			{
				CString txt;
				txt = str;
				txt.Insert( 1 , '/' );
				iTabNum ++;
				waitQueue.Push( new CString( txt ) );
			}
			file.Write( str , str.GetLength() );
			file.Write("\r\n",2);
		}
		file.Close();
	}
}

void MiddleLanguage::WriteTabToFile(CFile &file, int num)
{
	for( int i = 0 ; i < num ; i ++)
	{
		file.Write("\t",1);
	}
}

void MiddleLanguage::WriteToFile2(CString fn)
{
	CFile file;
	if( file.Open( fn , CFile::modeCreate | CFile::modeWrite ) )
	{
		DecodeStack waitQueue;
		CString str;
		int iTabNum = 0;
		while( m_Stack.GetUpperBound() >= 0  )	
		{
			CString tag1,content,tag2;
			CString *pStr;
			if( GetFirstThree( tag1 , content , tag2 ) )
			{
				if( tag1.GetLength() && tag2.GetLength() && tag1.GetAt( 0 ) == '<' && tag1.GetAt( 1 ) != '/' && tag2.GetAt( 0 ) == '<' && tag2.GetAt( 1 ) == '/' )
				{
					CString t;
					RemoveHead( t );
					RemoveHead( t );
					RemoveHead( t );
					t = tag1;
					t += content;
					t += tag2;
					WriteTabToFile( file , iTabNum );
					file.Write( t , t.GetLength() );
					file.Write("\r\n",2);
					continue;
				}
			}
			RemoveHead( tag1 );
			if( waitQueue.GetTop( pStr ) )
			{
				if( *pStr == tag1 )
				{//匹配
					waitQueue.Pop( pStr );
					iTabNum --;
					WriteTabToFile( file , iTabNum );
					delete pStr;
				}
				else
				{//不匹配
					if( tag1.GetAt( 0 ) == '<' )
					{//是起始标签
						CString txt;
						txt = tag1;
						txt.Insert( 1 , '/' );
						waitQueue.Push( new CString(txt) );
						WriteTabToFile( file , iTabNum );
						iTabNum ++;
					}
					else
					{//是内容
						WriteTabToFile( file , iTabNum + 1 );
					}
				}
			}
			else
			{
				if( tag1.GetAt( 0 ) == '<' )
				{//是起始标签
					CString txt;
					txt = tag1;
					txt.Insert( 1 , '/' );
					waitQueue.Push( new CString(txt) );
					WriteTabToFile( file , iTabNum );
					iTabNum ++;
				}
				else
				{//是内容
					WriteTabToFile( file , iTabNum + 1 );
				}
			}
			file.Write( tag1 ,tag1.GetLength() );
			file.Write( "\r\n" , 2 );
		}
		file.Close();
	}
}

BOOL MiddleLanguage::GetFirstThree(CString &tag1, CString &content, CString &tag2)
{
	if( m_Stack.GetUpperBound() < 2 )
	{
		return FALSE;
	}
	tag1 = *m_Stack.GetAt( 0 );
	content = *m_Stack.GetAt( 1 );
	tag2 = *m_Stack.GetAt( 2 );
	return TRUE;
}
