#include "pch.h"
#include "MxHead.h"
#include "MxReadOpt.h"
#include "MxReadFilter.h"
#include "DxfLib\dl_dxf.h"
#include "Tool.h"
#include "MxReadContext.h"

MxReadOpt::MxReadOpt(void)
{
}

MxReadOpt::~MxReadOpt(void)
{
}



bool MxReadOpt::Do(McDbDatabase* pDesDatabase,
		LPCTSTR pszFileName
		)
{
	ASSERT(pDesDatabase != NULL);

	MxString sFileDir;
	if(!VerifyFilePath(pszFileName,sFileDir) )
		return false;

	MxDocArx* pDoc = NULL;
	if(pDesDatabase->GetDocument() != NULL)
		pDoc = pDesDatabase->GetDocument()->Doc();


	MxReadFilter readFilter;

	DL_Dxf* pDxf = new DL_Dxf;
	if(pDxf == NULL)
	{
		mx_console_log(_T("memory faild"));
		return false;
	}

	std::auto_ptr<DL_Dxf> spDxf(pDxf);

	MxReadContext readContext(pDesDatabase,pDxf,&readFilter);
	readFilter.SetContext(&readContext);

	//bool success = pDxf->in((const char*)(CW2A(pszFileName,CP_ACP)), &readFilter);

	bool success = pDxf->in(MXDXF_W2A(pszFileName), &readFilter);
	if (success == false) 
	{
		return false;
	}

	readContext.EndReadEvent();

	return true;
}



bool MxReadOpt::VerifyFilePath(LPCTSTR pszFileName,
							   OUT MxString& sFileDir)
{
	MxString sFilePath(pszFileName);
	sFilePath.Replace(_T("/"), _T("\\"));
	if(sFilePath.IsEmpty() )
	{
		m_sError.Format(_T("文件路径为空"));
		return false;
	}

	int iFind = sFilePath.ReverseFind(_T('\\'));
	if(iFind != -1)
	{
		sFileDir = sFilePath.Left(iFind);
	}

	if(!Mx::isFileExist(sFilePath) )
	{
		m_sError.Format(_T("文件%s不存在"),sFilePath);
		return false;
	}

	return true;
}