#pragma warning ( disable:4786 )
#pragma	warning (disable:4278)
#include "XmlRcString.h"

#include	<windows.h>
#include	<msxml2.h>
#include	<atlbase.h>
#import		"msxml3.dll"
#include	<string>
#include	<vector>
#include	<conio.h>
//----------------------------------------------------------------------------------
inline void TESTHR( HRESULT hr )
{
	if( FAILED( hr ) )
		throw hr;
}
//----------------------------------------------------------------------------------
void	XmlRcString::_push( int ID , char* Str )
{
	while((int) _StringDB.size() <= ID )
	{
		_StringDB.push_back("");
	}

	if( _StringDB[ ID ].length() != 0 )
	{
		char Buf[512];
		sprintf( Buf , "XMLRCString %d 號碼重複 " , ID );
		OutputDebugString( Buf );
	}

	_StringDB[ ID ] = Str;
}
//----------------------------------------------------------------------------------
//初始化，設定字串資料檔案
bool	XmlRcString::Init( string File )
{
	Release();
	return OpenFile( File );
}
//----------------------------------------------------------------------------------
//可以重複載入多個檔案
bool	XmlRcString::OpenFile( string File )
{
	bool Ret;
	CoInitialize( NULL );
	Ret = _baseopenfile( File );
	CoUninitialize( );

	return Ret;
}

//----------------------------------------------------------------------------------
bool	XmlRcString::_baseopenfile(  string File  )
{
	char		Buf[512];
	int			i;
	_variant_t	varValue;


	MSXML2::IXMLDOMDocumentPtr			docPtr;
	MSXML2::IXMLDOMNodePtr				nodPtr;
	MSXML2::IXMLDOMNodeListPtr			nodlistPtr;
	MSXML2::IXMLDOMNamedNodeMapPtr		namePtr;



	try
	{
		TESTHR(docPtr.CreateInstance("msxml2.domdocument"));
		
		//讀取檔案資料
		_variant_t varXml( File.c_str() );
		_variant_t varOut( true );
		varOut = docPtr->load( varXml );

		if( (bool)varOut == false )
		{
			MSXML2::IXMLDOMParseErrorPtr errPtr = docPtr->GetparseError();

			sprintf( Buf , "Error :\n" );
			OutputDebugString( Buf );
			sprintf( Buf , "Code = 0x%x\n" , errPtr->errorCode );
			OutputDebugString( Buf );
			sprintf( Buf , "Source = Line : %ld , Char : %ld\n" , errPtr->line  , errPtr->linepos );
			OutputDebugString( Buf );
			sprintf( Buf , "Error Description = %s \n" , (char*) errPtr->reason );
			OutputDebugString( Buf );
		}
		else
		{
			nodlistPtr = docPtr->getElementsByTagName( "str" );
			
			for( i = 0 ; i < nodlistPtr->length ; i++ )
			{
				nodPtr = nodlistPtr->nextNode();
				namePtr = nodPtr->Getattributes();

				varValue = namePtr->getNamedItem("id")->GetnodeValue();

				int Index = atol(_bstr_t(varValue));

				_push( Index , nodPtr->Gettext() );
			}

		}

	}

	catch( _com_error &e )
	{
		sprintf( Buf , "Error:\n" );
		OutputDebugString( Buf );
		sprintf( Buf , "Code = %08lx\n" , e.Error() );
		OutputDebugString( Buf );
		sprintf( Buf , "Code meaning = %s \n" , e.ErrorMessage() );
		OutputDebugString( Buf );
		sprintf( Buf , "Source = %s\n", (char*) e.Source() );
		OutputDebugString( Buf );
		sprintf( Buf , "Error Description = %s \n " , (char*) e.Description() );
		OutputDebugString( Buf );
	}
	catch( ... )
	{
		OutputDebugString( "Unknow Error!" );
	}

	return true;
	//----------------------------------------------------------------------------------

}
//----------------------------------------------------------------------------------
//釋放資料
bool	XmlRcString::Release( )
{	
	_StringDB.clear();
	return true;
}
//----------------------------------------------------------------------------------
//字串資料取得
char*	XmlRcString::Get( int StrID )
{
	if( _StringDB.size() <= (unsigned int) StrID )
		return " ";
	
	char* Ret = (char*)_StringDB[ StrID ].c_str() ;

	if( Ret == NULL )
		return " ";

	return Ret;
}
//----------------------------------------------------------------------------------