📄 pmarkupdemodoc.cpp
字号:
// PMarkupDemoDoc.cpp : implementation of the CPMarkupDemoDoc class
//
#include "stdafx.h"
#include "PMarkupDemo.h"
#include "PMarkupDemoDoc.h"
#include "PMarkupDemoView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPMarkupDemoDoc
IMPLEMENT_DYNCREATE(CPMarkupDemoDoc, CDocument)
BEGIN_MESSAGE_MAP(CPMarkupDemoDoc, CDocument)
//{{AFX_MSG_MAP(CPMarkupDemoDoc)
ON_COMMAND(ID_FILE_PARSE, OnFileParse)
ON_UPDATE_COMMAND_UI(ID_FILE_PARSE, OnUpdateFileParse)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPMarkupDemoDoc construction/destruction
CPMarkupDemoDoc::CPMarkupDemoDoc()
{
m_bIsParsed = TRUE;
}
CPMarkupDemoDoc::~CPMarkupDemoDoc()
{
}
BOOL CPMarkupDemoDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
m_csText = _T("<?xml version=\"1.0\"?>\r\n<NEW/>\r\n");
m_doc.SetDoc( m_csText );
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CPMarkupDemoDoc serialization
void CPMarkupDemoDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CPMarkupDemoDoc diagnostics
#ifdef _DEBUG
void CPMarkupDemoDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CPMarkupDemoDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CPMarkupDemoDoc commands
void CPMarkupDemoDoc::TimeBefore()
{
// Keep track of time before operation
GetSystemTime( &m_stBefore );
}
void CPMarkupDemoDoc::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 CPMarkupDemoDoc::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 CPMarkupDemoDoc::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 CPMarkupDemoDoc::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 CPMarkupDemoDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// Get text out of view
CString csViewText;
POSITION pos = GetFirstViewPosition();
if ( pos )
{
CPMarkupDemoView* pView = (CPMarkupDemoView*)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 CPMarkupDemoDoc::OnCloseDocument()
{
CWaitCursor wait;
CDocument::OnCloseDocument();
}
void CPMarkupDemoDoc::OnFileParse()
{
POSITION pos = GetFirstViewPosition();
if ( pos )
{
CPMarkupDemoView* pView = (CPMarkupDemoView*)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 CPMarkupDemoDoc::OnUpdateFileParse(CCmdUI* pCmdUI)
{
pCmdUI->Enable( ! m_bIsParsed );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -