www.pudn.com > MarkupDemo.rar > MarkupDemoDoc.cpp


// MarkupDemoDoc.cpp : implementation of the CMarkupDemoDoc class 
// 
 
#include "stdafx.h" 
#include "MarkupDemo.h" 
 
#include "MarkupDemoDoc.h" 
 
#include "MarkupDemoView.h" 
#include "MainFrm.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMarkupDemoDoc 
 
IMPLEMENT_DYNCREATE(CMarkupDemoDoc, CDocument) 
 
BEGIN_MESSAGE_MAP(CMarkupDemoDoc, CDocument) 
	//{{AFX_MSG_MAP(CMarkupDemoDoc) 
	ON_COMMAND(ID_FILE_PARSE, OnFileParse) 
	ON_UPDATE_COMMAND_UI(ID_FILE_PARSE, OnUpdateFileParse) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMarkupDemoDoc construction/destruction 
 
CMarkupDemoDoc::CMarkupDemoDoc() 
{ 
	m_bIsParsed = TRUE; 
} 
 
CMarkupDemoDoc::~CMarkupDemoDoc() 
{ 
} 
 
BOOL CMarkupDemoDoc::OnNewDocument() 
{ 
	if (!CDocument::OnNewDocument()) 
		return FALSE; 
 
	m_csText = _T("\r\n\r\n"); 
	m_doc.SetDoc( m_csText ); 
 
	return TRUE; 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CMarkupDemoDoc serialization 
 
void CMarkupDemoDoc::Serialize(CArchive& ar) 
{ 
	if (ar.IsStoring()) 
	{ 
		// TODO: add storing code here 
	} 
	else 
	{ 
		// TODO: add loading code here 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMarkupDemoDoc diagnostics 
 
#ifdef _DEBUG 
void CMarkupDemoDoc::AssertValid() const 
{ 
	CDocument::AssertValid(); 
} 
 
void CMarkupDemoDoc::Dump(CDumpContext& dc) const 
{ 
	CDocument::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMarkupDemoDoc commands 
 
 
void CMarkupDemoDoc::TimeBefore() 
{ 
	// Keep track of time before operation 
	GetSystemTime( &m_stBefore ); 
} 
 
void CMarkupDemoDoc::TimeAfter(LPCTSTR lpszTitle, LPCTSTR szOp) 
{ 
	// Determine time span 
	SYSTEMTIME stAfter; 
	GetSystemTime( &stAfter ); 
	int nBefore = m_stBefore.wMilliseconds + m_stBefore.wSecond * 1000 + m_stBefore.wMinute * 60000; 
	int nAfter = stAfter.wMilliseconds + stAfter.wSecond * 1000 + stAfter.wMinute * 60000; 
	int nDiff = nAfter - nBefore; 
	if ( m_stBefore.wHour < stAfter.wHour ) 
		nDiff += 24*60000; 
 
	// Display in status bar 
	CString csSpan; 
	csSpan.Format( _T("%s of %s took about %d milliseconds"), szOp, lpszTitle, nDiff ); 
	((CMainFrame*)(AfxGetApp()->m_pMainWnd))->SetStatus( csSpan ); 
} 
 
CString CMarkupDemoDoc::TitleFromPath(LPCTSTR lpszPathName) 
{ 
	// Determine file name from pathname 
	CString csTitle = lpszPathName; 
	for ( int nPathChar=0; lpszPathName[nPathChar]; ++nPathChar ) 
	{ 
		TCHAR cChar = lpszPathName[nPathChar]; 
		if ( cChar == '\\' || cChar == '/' || cChar == ':') 
			csTitle = &lpszPathName[nPathChar+1]; 
	} 
	return csTitle; 
} 
 
void CMarkupDemoDoc::ShowError(LPCTSTR lpszTitle, LPCTSTR szError) 
{ 
	// Display in status bar 
	CString csError; 
	csError.Format( _T("%s: %s"), lpszTitle, szError ); 
	((CMainFrame*)(AfxGetApp()->m_pMainWnd))->SetStatus( csError ); 
} 
 
BOOL CMarkupDemoDoc::OnOpenDocument(LPCTSTR lpszPathName)  
{ 
	// Load up buffer 
	unsigned char* pBuffer = NULL; 
	int nFileLen = 0; 
 
	try 
	{ 
		CFile file( lpszPathName, CFile::modeRead ); 
		nFileLen = file.GetLength(); 
 
		// Allocate Buffer for Ansi file data 
		pBuffer = new unsigned char[nFileLen + 1]; 
		nFileLen = file.Read( pBuffer, nFileLen ); 
		file.Close(); 
		pBuffer[nFileLen] = '\0'; 
	} 
	catch (CFileException*) 
	{ 
		if ( pBuffer ) 
			delete pBuffer; 
		return FALSE; 
	} 
 
#if defined(_UNICODE) 
	// Convert file to UNICODE if necessary 
	int nWideSize = MultiByteToWideChar(CP_UTF8,0,(const char*)pBuffer,nFileLen,m_csText.GetBuffer(nFileLen),nFileLen); 
	m_csText.ReleaseBuffer(nWideSize); 
#else 
	m_csText = (char*)pBuffer; 
#endif 
 
	// Convert newlines to CRLFs for CEdit 
	CString csCRLFText; 
	const _TCHAR* pSource = (LPCTSTR)m_csText; 
	_TCHAR* pDest = csCRLFText.GetBuffer(m_csText.GetLength() * 2); 
	int nSrcChar = 0, nDestChar = 0; 
	while ( pSource[nSrcChar] ) 
	{ 
		if ( pSource[nSrcChar] == '\n' && (nSrcChar == 0 || pSource[nSrcChar-1]!='\r') ) 
			pDest[nDestChar++] = '\r'; 
		pDest[nDestChar++] = pSource[nSrcChar++]; 
	} 
	csCRLFText.ReleaseBuffer(nDestChar); 
	m_csText = csCRLFText; 
 
	// Parse 
	TimeBefore(); 
	m_bIsParsed = m_doc.SetDoc( m_csText ); 
	if ( m_bIsParsed ) 
		TimeAfter( TitleFromPath(lpszPathName), _T("parse") ); 
	else 
		ShowError( TitleFromPath(lpszPathName), m_doc.GetError() ); 
 
	delete [] pBuffer; 
	SetModifiedFlag( FALSE ); 
	return TRUE; 
} 
 
BOOL CMarkupDemoDoc::OnSaveDocument(LPCTSTR lpszPathName)  
{ 
	// Get text out of view 
	CString csViewText; 
	POSITION pos = GetFirstViewPosition(); 
	if ( pos ) 
	{ 
		CMarkupDemoView* pView = (CMarkupDemoView*)GetNextView(pos); 
		pView->GetEditText( csViewText ); 
	} 
 
#if defined( _UNICODE ) 
	CString csDoc = m_doc.GetDoc(); 
	if ( ! IsParsed() ) 
		csDoc = csViewText; 
	int nUTF8Len = WideCharToMultiByte(CP_UTF8,0,csDoc,csDoc.GetLength(),NULL,0,NULL,NULL); 
	char* pBuffer = new char[nUTF8Len+1]; 
	WideCharToMultiByte(CP_UTF8,0,csDoc,csDoc.GetLength(),pBuffer,nUTF8Len+1,NULL,NULL); 
	try 
	{ 
		CFile file( lpszPathName, CFile::modeWrite | CFile::modeCreate ); 
		file.Write( pBuffer, nUTF8Len ); 
		file.Close(); 
	} 
	catch (CFileException*) 
	{ 
		return FALSE; 
	} 
#else 
	CString csDoc = m_doc.GetDoc(); 
	if ( ! IsParsed() ) 
		csDoc = csViewText; 
	try 
	{ 
		CFile file( lpszPathName, CFile::modeWrite | CFile::modeCreate ); 
		file.Write( csDoc.GetBuffer(0), csDoc.GetLength() ); 
		file.Close(); 
	} 
	catch (CFileException*) 
	{ 
		return FALSE; 
	} 
#endif 
	SetModifiedFlag( FALSE ); 
	return TRUE; 
} 
 
void CMarkupDemoDoc::OnCloseDocument()  
{ 
	CWaitCursor wait; 
	CDocument::OnCloseDocument(); 
} 
 
void CMarkupDemoDoc::OnFileParse()  
{ 
	POSITION pos = GetFirstViewPosition(); 
	if ( pos ) 
	{ 
		CMarkupDemoView* pView = (CMarkupDemoView*)GetNextView(pos); 
		pView->GetEditText( m_csText ); 
		TimeBefore(); 
		BOOL bParsed = m_doc.SetDoc( m_csText ); 
		if ( bParsed ) 
			TimeAfter( GetTitle(), _T("parse") ); 
		else 
			ShowError( GetTitle(), m_doc.GetError() ); 
		UpdateAllViews( NULL ); 
		m_bIsParsed = bParsed; 
	} 
} 
 
void CMarkupDemoDoc::OnUpdateFileParse(CCmdUI* pCmdUI)  
{ 
	pCmdUI->Enable( ! m_bIsParsed ); 
}