MarkupDoc.cpp
资源名称:CMarkup.rar [点击查看]
上传用户:hawkcdm
上传日期:2013-02-10
资源大小:411k
文件大小:6k
源码类别:
xml/soap/webservice
开发平台:
Visual C++
- // MarkupDoc.cpp : implementation of the CMarkupDoc class
- //
- // Markup Release 6.1 Lite
- // Copyright (C) 1999-2001 First Objective Software, Inc. All rights reserved
- // This entire notice must be retained in this source code
- // Redistributing this source code requires written permission
- // This software is provided "as is", with no warranty.
- // Latest fixes enhancements and documentation at www.firstobject.com
- #include "stdafx.h"
- #include "MarkupApp.h"
- #include "MarkupDoc.h"
- #include "MarkupView.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMarkupDoc
- IMPLEMENT_DYNCREATE(CMarkupDoc, CDocument)
- BEGIN_MESSAGE_MAP(CMarkupDoc, CDocument)
- //{{AFX_MSG_MAP(CMarkupDoc)
- ON_COMMAND(ID_FILE_PARSE, OnFileParse)
- ON_UPDATE_COMMAND_UI(ID_FILE_PARSE, OnUpdateFileParse)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMarkupDoc construction/destruction
- CMarkupDoc::CMarkupDoc()
- {
- m_bIsParsed = TRUE;
- }
- CMarkupDoc::~CMarkupDoc()
- {
- }
- BOOL CMarkupDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- m_csText = _T("<?xml version="1.0"?>rn<NEW/>rn");
- m_doc.SetDoc( m_csText );
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMarkupDoc serialization
- void CMarkupDoc::Serialize(CArchive& ar)
- {
- if ( ar.IsStoring() )
- {
- // Write
- }
- else
- {
- // Read
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMarkupDoc diagnostics
- #ifdef _DEBUG
- void CMarkupDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CMarkupDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMarkupDoc commands
- void CMarkupDoc::TimeBefore()
- {
- // Keep track of time before operation
- GetSystemTime( &m_stBefore );
- }
- void CMarkupDoc::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 CMarkupDoc::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 CMarkupDoc::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 CMarkupDoc::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] = ' ';
- }
- 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 CMarkupDoc::OnSaveDocument(LPCTSTR lpszPathName)
- {
- // Get text out of view
- CString csViewText;
- POSITION pos = GetFirstViewPosition();
- if ( pos )
- {
- CMarkupView* pView = (CMarkupView*)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 CMarkupDoc::OnCloseDocument()
- {
- CWaitCursor wait;
- CDocument::OnCloseDocument();
- }
- void CMarkupDoc::OnFileParse()
- {
- POSITION pos = GetFirstViewPosition();
- if ( pos )
- {
- CMarkupView* pView = (CMarkupView*)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 CMarkupDoc::OnUpdateFileParse(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable( ! m_bIsParsed );
- }