Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
MarkupDlg.cpp
Package: cmd2xml.zip [view]
Upload User: wbm1988
Upload Date: 2022-08-06
Package Size: 3696k
Code Size: 81k
Category:
Windows Develop
Development Platform:
Visual C++
- // MarkupDlg.cpp : implementation file
- //
- // Markup Release 9.0
- // Copyright (C) 1999-2007 First Objective Software, Inc. All rights reserved
- // Go to www.firstobject.com for the latest CMarkup and EDOM documentation
- // Use in commercial applications requires written permission
- // This software is provided "as is", with no warranty.
- #include "stdafx.h"
- #include "MarkupApp.h"
- #include "MarkupDlg.h"
- #include <afxpriv.h>
- #include <io.h>
- #include <locale.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define OPSETSIZE 8
- typedef int Status;
- #ifdef MARKUP_MSXML
- #include "MarkupMSXML.h"
- #define MCD_STR CString
- #define MCD_2PCSZ(s) ((LPCTSTR)s)
- #define MCD_STRCLEAR(s) s.Empty()
- #define MCD_STRISEMPTY(s) s.IsEmpty()
- #else
- #include "Markup.h"
- #endif
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- unsigned char Prior[8][8] = { // 表3.1 算符间的优先关系
- // '+' '-' '*' '/' '(' ')' '#' '^'
- /*'+'*/'>','>','<','<','<','>','>','<',
- /*'-'*/'>','>','<','<','<','>','>','<',
- /*'*'*/'>','>','>','>','<','>','>','<',
- /*'/'*/'>','>','>','>','<','>','>','<',
- /*'('*/'<','<','<','<','<','=',' ','<',
- /*')'*/'>','>','>','>',' ','>','>','>',
- /*'#'*/'<','<','<','<','<',' ','=','<',
- /*'^'*/'>','>','>','>','<','>','>','>'
- };
- CMarkup xml;
- /////////////////////////////////////////////////////////////////////////////
- // CMarkupDlg dialog
- CMarkupDlg::CMarkupDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMarkupDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CMarkupDlg)
- m_cmdEdit = _T("");
- //}}AFX_DATA_INIT
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CMarkupDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMarkupDlg)
- DDX_Text(pDX, IDC_CMD_EDIT, m_cmdEdit);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CMarkupDlg, CDialog)
- //{{AFX_MSG_MAP(CMarkupDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
- ON_BN_CLICKED(IDC_BUTTON_PARSE, OnButtonParse)
- ON_WM_DESTROY()
- ON_BN_CLICKED(IDC_TEST_XML_NEW, OnTestXmlNew)
- ON_BN_CLICKED(IDC_COMPILE, OnCompile)
- ON_BN_CLICKED(IDC_CLEAR, OnClear)
- ON_BN_CLICKED(IDC_OPEN_EXISTS, OnOpenExists)
- ON_BN_CLICKED(IDC_SAVE_AS, OnSaveAs)
- ON_WM_TIMER()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- void CMarkupDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- HCURSOR CMarkupDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMarkupDlg message handlers
- BOOL CMarkupDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- SystemSettings( TRUE );
- m_nErrorCount = 0;
- m_nTotalZones = 0;
- m_nTotalChecks = 0;
- SetWindowText( _T("CMarkup Evaluation Test Dialog") );
- // Determine version
- CString csVersion, csV0, csV1;
- CString csTitle;
- csVersion.LoadString( ID_APP_VERSION );
- AfxExtractSubString( csV0, csVersion, 0, ',' );
- AfxExtractSubString( csV1, csVersion, 1, ',' );
- CString csClass = _T("CMarkup");
- CString csBuild;
- #if defined( MARKUP_MSXML )
- csClass += _T("MSXML");
- #if defined( MARKUP_MSXML3 )
- csClass += _T(" MSXML3");
- #elif defined( MARKUP_MSXML4 )
- csClass += _T(" MSXML4");
- #endif
- #endif
- #if defined( MARKUP_STL )
- csBuild += _T(" STL");
- #endif
- #if defined( _DEBUG )
- csBuild += _T(" Debug");
- #endif
- #if defined( _UNICODE )
- csBuild += _T(" Unicode");
- #endif
- #if defined( _MBCS )
- csBuild += _T(" MBCS");
- #endif
- csTitle.Format( _T("%s %s.%s%srn"), csClass, csV0, csV1, csBuild );
- OutputTestResults( csTitle );
- OutputParseResults( _T("") );
- RunTest();
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CMarkupDlg::OnDestroy()
- {
- SystemSettings( FALSE );
- CDialog::OnDestroy();
- }
- void CMarkupDlg::OutputTestResults( CString csMsg )
- {
- m_csTestResults += csMsg;
- // GetDlgItem( IDC_ST_TEST_RESULTS )->SetWindowText( m_csTestResults );
- }
- int CMarkupDlg::Alert( CString csMsg )
- {
- m_csChecks += csMsg + _T("rn");
- ++m_nErrorCount;
- return -1;
- }
- void CMarkupDlg::StartCheckZone( CString csCheckZone )
- {
- m_nCheckCount = 0;
- ++m_nTotalZones;
- m_csCheckZone = csCheckZone;
- }
- int CMarkupDlg::Check( BOOL bCorrect )
- {
- ++m_nCheckCount;
- ++m_nTotalChecks;
- if ( ! bCorrect )
- {
- if ( m_csCheckZone.IsEmpty() )
- m_csCheckZone = _T("Unknown Check Zone");
- CString csMsg;
- csMsg.Format( _T("Error: %s, check %d"), m_csCheckZone, m_nCheckCount );
- return Alert( csMsg );
- }
- return 0;
- }
- #ifdef MARKUP_MSXML
- void SetEntry( CMarkupMSXML& xml, MCD_STR strSection, MCD_STR strEntry, MCD_STR strValue )
- #else
- void SetEntry( CMarkup& xml, MCD_STR strSection, MCD_STR strEntry, MCD_STR strValue )
- #endif
- {
- // Find/Create root element of xml document
- xml.ResetPos();
- if ( ! xml.FindElem() )
- xml.AddElem( _T("sbml") ); // or whatever root element name is
- // Find/Create section
- BOOL bFoundSection = FALSE;
- while ( xml.FindChildElem(_T("Section")) )
- {
- // Is this the right section?
- if ( xml.GetChildAttrib(_T("name")) == strSection )
- {
- bFoundSection = TRUE;
- break;
- }
- }
- if ( ! bFoundSection )
- {
- xml.AddChildElem( _T("Section") );
- xml.SetChildAttrib( _T("name"), strSection );
- }
- // Find/Create entry
- xml.IntoElem();
- BOOL bFoundEntry = FALSE;
- while ( xml.FindChildElem(_T("Entry")) )
- {
- // Is this the right entry?
- if ( xml.GetChildAttrib(_T("name")) == strEntry )
- {
- bFoundEntry = TRUE;
- break;
- }
- }
- if ( ! bFoundEntry )
- {
- xml.AddChildElem( _T("Entry") );
- xml.SetChildAttrib( _T("name"), strEntry );
- }
- // Set value
- xml.SetChildData( strValue );
- }
- #ifdef MARKUP_MSXML
- bool FindEntry( CMarkupMSXML& xml, MCD_STR strSection, MCD_STR strEntry, MCD_STR& strValue )
- #else
- bool FindEntry( CMarkup& xml, MCD_STR strSection, MCD_STR strEntry, MCD_STR& strValue )
- #endif
- {
- // Loop through sections
- xml.ResetPos();
- while ( xml.FindChildElem(_T("Section")) )
- {
- // Is this the right section?
- if ( xml.GetChildAttrib(_T("name")) == strSection )
- {
- // Check entries in this section
- xml.IntoElem();
- while ( xml.FindChildElem(_T("Entry")) )
- {
- // Is this the right entry?
- if ( xml.GetChildAttrib(_T("name")) == strEntry )
- {
- strValue = xml.GetChildData();
- return TRUE;
- }
- }
- break; // don't check any other sections
- }
- }
- return FALSE;
- }
- CString CMarkupDlg::GetSpecialPath( int nFolderID )
- {
- CString csPath;
- TCHAR szSpecialPath[MAX_PATH];
- LPITEMIDLIST pidl;
- if( SUCCEEDED(SHGetSpecialFolderLocation(0,nFolderID,&pidl)) )
- {
- SHGetPathFromIDList( pidl, szSpecialPath );
- LPMALLOC pMalloc = NULL;
- SHGetMalloc(&pMalloc);
- pMalloc->Free( pidl );
- pMalloc->Release();
- csPath = szSpecialPath;
- csPath += "\";
- }
- return csPath;
- }
- #if ! defined(_UNICODE)
- CString CMarkupDlg::WinAToUTF8( CString csText )
- {
- int nMBLen = csText.GetLength();
- if ( nMBLen )
- {
- wchar_t* pwszWide = new wchar_t[nMBLen+1];
- int nWideLen = MultiByteToWideChar(CP_ACP,0,csText,nMBLen,pwszWide,nMBLen);
- pwszWide[nWideLen] = (wchar_t)0;
- int nUTF8Len = nWideLen * 4 + 1;
- char* pUTF8 = csText.GetBuffer( nUTF8Len );
- nUTF8Len = WideCharToMultiByte(CP_UTF8,0,pwszWide,nWideLen,pUTF8,nUTF8Len,NULL,NULL);
- delete[] pwszWide;
- csText.ReleaseBuffer( nUTF8Len );
- }
- return csText;
- }
- CString CMarkupDlg::WinUTF8ToA( CString csText )
- {
- int nUTF8Len = csText.GetLength();
- if ( nUTF8Len )
- {
- wchar_t* pwszWide = new wchar_t[nUTF8Len+1];
- int nWideLen = MultiByteToWideChar(CP_UTF8,0,csText,nUTF8Len,pwszWide,nUTF8Len);
- pwszWide[nWideLen] = (wchar_t)0;
- int nMBLen = nWideLen * 2 + 1;
- char* pMB = csText.GetBuffer( nMBLen );
- BOOL bUsedDefault = FALSE;
- nMBLen = WideCharToMultiByte(CP_ACP,0,pwszWide,nWideLen,pMB,nMBLen,"?",&bUsedDefault);
- delete[] pwszWide;
- csText.ReleaseBuffer( nMBLen );
- }
- return csText;
- }
- #endif
- void CMarkupDlg::SystemSettings( BOOL bLoad )
- {
- #ifdef MARKUP_MSXML
- CMarkupMSXML xmlSettings;
- #else
- CMarkup xmlSettings;
- #endif
- // Find/Create app data folder
- CString csUserAppDataPath = GetSpecialPath( 0x001a ); // CSIDL_APPDATA
- if ( csUserAppDataPath.IsEmpty() )
- return;
- CString csCompanyFolder = csUserAppDataPath + "firstobject";
- CString csSettingsFolder = csCompanyFolder + "\CMarkup";
- CString csSettingsPathName = csSettingsFolder + "\settings.xml";
- #if defined( _MBCS )
- setlocale( LC_ALL, "C" );
- setlocale( LC_ALL, "" );
- #endif
- if ( ! xmlSettings.Load((LPCTSTR)csSettingsPathName) )
- {
- if ( _taccess(csCompanyFolder,0) != 0 )
- {
- if ( ! CreateDirectory(csCompanyFolder,NULL) )
- return;
- }
- if ( _taccess(csSettingsFolder,0) != 0 )
- {
- if ( ! CreateDirectory(csSettingsFolder,NULL) )
- return;
- }
- }
- if ( bLoad )
- {
- MCD_STR strFilename;
- if ( FindEntry(xmlSettings,_T("Settings"),_T("Filename"),strFilename) && strFilename != _T("") )
- {
- CString csFilename = MCD_2PCSZ(strFilename);
- #if ! defined(_UNICODE) && ! defined(_MBCS)
- csFilename = WinUTF8ToA( csFilename );
- #endif
- GetDlgItem( IDC_EDIT_FILE )->SetWindowText( csFilename );
- }
- MCD_STR csPos;
- if ( FindEntry(xmlSettings,_T("Settings"),_T("Position"),csPos) )
- {
- CPoint ptDlg;
- if ( _stscanf( MCD_2PCSZ(csPos), _T("%d,%d"), &ptDlg.x, &ptDlg.y ) == 2 )
- {
- CRect rect;
- GetWindowRect( &rect );
- if ( rect.Width() > 10 )
- {
- rect.OffsetRect( ptDlg.x - rect.left, ptDlg.y - rect.top );
- MoveWindow( &rect );
- }
- }
- }
- }
- else // save
- {
- CString csFilename;
- GetDlgItem( IDC_EDIT_FILE )->GetWindowText( csFilename );
- #if ! defined(_UNICODE) && ! defined(_MBCS)
- csFilename = WinAToUTF8( csFilename );
- #endif
- MCD_STR strFilename = csFilename;
- SetEntry( xmlSettings, _T("Settings"), _T("Filename"), strFilename );
- CRect rect;
- GetWindowRect( &rect );
- CString csPos;
- csPos.Format( _T("%d,%d"), rect.left, rect.top );
- MCD_STR strPos = csPos;
- SetEntry( xmlSettings, _T("Settings"), _T("Position"), strPos );
- xmlSettings.Save( (LPCTSTR)csSettingsPathName );
- }
- }
- #ifdef MARKUP_MSXML
- void SimpleMerge( CMarkupMSXML& xmlMaster, CMarkupMSXML& xmlUpdate )
- #else
- void SimpleMerge( CMarkup& xmlMaster, CMarkup& xmlUpdate )
- #endif
- {
- // Generic merge xmlUpdate into xmlMaster when element names are unique among siblings
- // removing elements from xmlUpdate as added to or overrided in xmlMaster
- //
- MCD_STR strMergeName;
- xmlMaster.ResetPos();
- xmlUpdate.ResetPos();
- BOOL bMergeFinished = FALSE;
- if ( ! xmlMaster.FindChildElem() )
- {
- xmlMaster = xmlUpdate;
- bMergeFinished = TRUE;
- }
- xmlUpdate.FindChildElem();
- while ( ! bMergeFinished )
- {
- // Process Element
- xmlMaster.IntoElem();
- xmlUpdate.IntoElem();
- strMergeName = xmlMaster.GetTagName();
- // Did this one match?
- xmlUpdate.ResetMainPos();
- BOOL bMatched = xmlUpdate.FindElem( strMergeName );
- if ( bMatched )
- {
- // Merge attributes
- for ( int nAttrib=0; !MCD_STRISEMPTY((strMergeName=xmlUpdate.GetAttribName(nAttrib))); ++nAttrib )
- xmlMaster.SetAttrib( strMergeName, xmlUpdate.GetAttrib(strMergeName) );
- }
- // Next element (depth first)
- BOOL bChildFound = xmlMaster.FindChildElem();
- while ( ! bChildFound && ! bMergeFinished )
- {
- if ( bMatched )
- {
- while ( xmlUpdate.FindChildElem() )
- {
- xmlMaster.AddChildSubDoc( xmlUpdate.GetChildSubDoc() );
- xmlUpdate.RemoveChildElem();
- }
- xmlUpdate.RemoveElem();
- }
- if ( xmlMaster.OutOfElem() )
- {
- xmlUpdate.OutOfElem();
- bChildFound = xmlMaster.FindChildElem();
- if ( ! bChildFound )
- {
- bMatched = TRUE;
- xmlUpdate.ResetChildPos();
- }
- }
- else
- bMergeFinished = TRUE;
- }
- }
- }
- CString TruncToLastEOL( CString csVal )
- {
- // locate and truncate from final EOL
- // also cuts any trailing indentation
- int nLength = csVal.GetLength();
- const _TCHAR* pszVal = (const _TCHAR*)csVal;
- int nLastEOL = nLength;
- int nChar = 0;
- while ( nChar < nLength )
- {
- // Go to next EOL
- nLastEOL = nLength;
- while ( nChar < nLength && pszVal[nChar] != _T('r')
- && pszVal[nChar] != _T('n') )
- nChar += (int)_tclen(&pszVal[nChar]);
- nLastEOL = nChar;
- while ( nChar < nLength
- && _tcschr(_T(" tnr"),pszVal[nChar]) )
- ++nChar;
- }
- if ( nLastEOL < nLength )
- csVal = csVal.Left( nLastEOL );
- return csVal;
- }
- int CMarkupDlg::RunTest()
- {
- // Instantiate XML objects for use in tests
- // Mostly the same code can be used to test the different versions of CMarkup
- // However, to convert both CString and std::string to const char *, use MCD_2PCSZ()
- #ifdef MARKUP_MSXML
- CMarkupMSXML xml, xml2, xml3;
- #else
- CMarkup xml, xml2, xml3;
- #endif
- // Integer Method Test
- StartCheckZone( _T("Integer Method Test") );
- xml.SetDoc( NULL );
- xml.AddElem( _T("R"), 5 );
- Check( _ttoi(MCD_2PCSZ(xml.GetData())) == 5 );
- xml.SetData( _T("") );
- Check( xml.InsertChildElem( _T("C"), 1 ) );
- Check( xml.AddChildElem( _T("C"), 3 ) );
- Check( xml.IntoElem() );
- Check( xml.InsertElem( _T("C"), 2 ) );
- xml.ResetMainPos();
- Check( xml.FindElem() );
- Check( _ttoi(MCD_2PCSZ(xml.GetData())) == 1 );
- Check( xml.FindElem() );
- Check( _ttoi(MCD_2PCSZ(xml.GetData())) == 2 );
- Check( xml.FindElem() );
- Check( _ttoi(MCD_2PCSZ(xml.GetData())) == 3 );
- // Quote and special chars in attrib value test
- StartCheckZone( _T("Attrib Value Test") );
- Check( xml.SetDoc( _T("<H g1=">" g2='>' d="d'd" s='s"s'/>") ) );
- Check( xml.FindElem() );
- Check( xml.GetAttrib(_T("d")) == _T("d'd") );
- Check( xml.GetAttrib(_T("s")) == _T("s"s") );
- Check( xml.SetAttrib( _T("d"), _T("d'd") ) );
- Check( xml.GetAttrib(_T("d")) == _T("d'd") );
- Check( xml.GetAttrib(_T("s")) == _T("s"s") );
- Check( xml.GetAttrib(_T("g1")) == _T(">") );
- Check( xml.GetAttrib(_T("g2")) == _T(">") );
- // FindElem Tag Name Test
- StartCheckZone( _T("FindElem Tag Name Test") );
- xml.SetDoc( NULL );
- xml.AddElem( _T("R") );
- xml.AddChildElem( _T("N"), _T("A") );
- xml.AddChildElem( _T("Name"), _T("B") );
- xml.AddChildElem( _T("Na"), _T("C") );
- xml.AddChildElem( _T("Nam"), _T("D") );
- xml.ResetChildPos();
- Check( xml.FindChildElem(_T("Name")) );
- Check( xml.GetChildData() == _T("B") );
- Check( xml.FindChildElem(_T("Nam")) );
- Check( xml.GetChildData() == _T("D") );
- // Copy Constructor Heap Test
- #ifndef MARKUP_MSXML
- StartCheckZone( _T("Copy Constructor Heap Test") );
- Check( AfxCheckMemory() );
- CMarkup xmlTest(xml);
- Check( AfxCheckMemory() );
- xmlTest.AddElem( _T("AfterCopyEmpty") );
- Check( AfxCheckMemory() );
- xmlTest.SetDoc( NULL );
- xml = xmlTest;
- xml.AddElem( _T("AfterCopyEmpty") );
- Check( AfxCheckMemory() );
- #endif
- // CDATA Section Test
- StartCheckZone( _T("CDATA Section Test") );
- xml.SetDoc( NULL );
- Check( xml.AddElem( _T("R") ) );
- Check( xml.AddChildElem( _T("C") ) );
- Check( xml.SetChildData( _T("a]]>b]]>c]]>d"), 1 ) );
- Check( xml.GetChildData() == _T("a]]>b]]>c]]>d") );
- Check( xml.IntoElem() );
- Check( xml.SetData( _T("]]>"), 1 ) );
- Check( xml.GetData() == _T("]]>") );
- MCD_STR strCDATASubDoc = _T("<C><![CDATA[a]]>]]><![CDATA[a]]></C>");
- Check( xml.AddSubDoc( strCDATASubDoc ) );
- Check( xml.GetData() == _T("a]]>a") );
- // Underscore tag name check
- StartCheckZone( _T("Tag Name Test") );
- xml.SetDoc( NULL );
- Check( xml.AddElem( _T("_Underscore") ) );
- MCD_STR strTagNameDoc = xml.GetDoc();
- Check( xml.SetDoc( strTagNameDoc ) );
- xml.FindElem();
- Check( xml.GetTagName() == _T("_Underscore") );
- // ORDER Test, simple XML example
- StartCheckZone( _T("ORDER Test") );
- xml.SetDoc( NULL );
- Check( xml.AddElem( _T("ORDER") ) );
- Check( xml.IntoElem() );
- Check( xml.AddElem( _T("ITEM") ) );
- Check( xml.AddChildElem( _T("NAME"), _T("carrots") ) );
- Check( xml.AddChildElem( _T("QTY"), _T("1") ) );
- Check( xml.AddChildElem( _T("PRICE"), _T(".98") ) );
- Check( xml.AddChildAttrib( _T("unit"), _T("1 lb") ) );
- Check( xml.AddElem( _T("ITEM") ) );
- Check( xml.AddChildElem( _T("NAME"), _T("onions") ) );
- Check( xml.AddChildElem( _T("QTY"), _T("1") ) );
- Check( xml.AddChildElem( _T("PRICE"), _T("1.10") ) );
- Check( xml.AddChildAttrib( _T("unit"), _T("3 lb bag") ) );
- Check( xml.AddChildElem( _T("SUPPLIER"), _T("Hanover") ) );
- // Create List
- StartCheckZone( _T("List Test") );
- MCD_STR strList;
- xml.ResetPos();
- Check( xml.FindElem( _T("ORDER") ) );
- while ( xml.FindChildElem( _T("ITEM") ) )
- {
- xml.IntoElem();
- xml.FindChildElem( _T("NAME") );
- strList += xml.GetChildData();
- strList += _T("n");
- #ifndef MARKUP_MSXML
- xml.SavePos();
- #endif
- xml.OutOfElem();
- }
- #if ! defined( MARKUP_MSXML )
- xml.RestorePos();
- Check( xml.GetChildData() == _T("onions") );
- #endif
- Check( strList == _T("carrotsnonionsn") );
- // December Test, add and remove five elements repeatedly
- StartCheckZone( _T("December Test") );
- struct TestData { _TCHAR* szName; _TCHAR* szValue; } td[] =
- {
- _T("patridge"), _T("PEARTREE"),
- _T("2turtle"), _T("DOVES"),
- _T("3french"), _T("HENS"),
- _T("4calling"), _T("BIRDS"),
- _T("5golden"), _T("RINGS"),
- };
- int nTD;
- xml.SetDoc( _T("") );
- Check( xml.AddElem( _T("RESULT") ) );
- for ( nTD=0; nTD<=4; ++nTD )
- {
- xml.AddChildElem( _T("VAR"), td[nTD].szValue );
- xml.SetChildAttrib( _T("name"), td[nTD].szName );
- }
- for ( nTD=0; nTD<=4; ++nTD )
- {
- xml.ResetPos();
- while ( xml.FindChildElem( _T("VAR") ) )
- if ( xml.GetChildAttrib( _T("name") ) == td[nTD].szName )
- {
- xml.RemoveChildElem();
- break;
- }
- xml.AddChildElem( _T("VAR"), td[nTD].szValue );
- xml.SetChildAttrib( _T("name"), td[nTD].szName );
- }
- for ( nTD=4; nTD>=0; --nTD )
- {
- xml.ResetPos();
- while ( xml.FindChildElem( _T("VAR") ) )
- if ( xml.GetChildAttrib( _T("name") ) == td[nTD].szName )
- {
- xml.RemoveChildElem();
- break;
- }
- xml.ResetChildPos();
- xml.AddChildElem( _T("VAR"), td[nTD].szValue );
- xml.SetChildAttrib( _T("name"), td[nTD].szName );
- }
- // <RESULT>
- // <VAR name="5golden">RINGS</VAR>
- // <VAR name="4calling">BIRDS</VAR>
- // <VAR name="3french">HENS</VAR>
- // <VAR name="2turtle">DOVES</VAR>
- // <VAR name="patridge">PEARTREE</VAR>
- // </RESULT>
- xml.ResetPos();
- for ( nTD=4; nTD>=0; --nTD )
- {
- Check( xml.FindChildElem() );
- Check( xml.GetChildData() == td[nTD].szValue );
- Check( xml.GetChildAttrib( _T("name") ) == td[nTD].szName );
- }
- // Add Insert Test, test siblings created in correct sequence
- _TCHAR* aszNums[] = { _T("zero"),_T("one"),_T("two"),_T("three"),_T("four"),_T("five"),_T("six") };
- MCD_STR strInside = _T("_in6");
- StartCheckZone( _T("Insert Test") );
- xml.SetDoc( _T("") );
- xml.AddElem( _T("root") );
- xml.AddChildElem( aszNums[1] );
- xml.ResetChildPos();
- xml.AddChildElem( aszNums[3] );
- xml.ResetChildPos();
- xml.FindChildElem();
- xml.InsertChildElem( aszNums[0] );
- xml.RemoveChildElem();
- xml.InsertChildElem( aszNums[0] );
- xml.FindChildElem();
- xml.AddChildElem( aszNums[2] );
- xml.FindChildElem();
- xml.AddChildElem( aszNums[4] );
- xml.FindChildElem();
- xml.AddChildElem( aszNums[5] );
- xml.ResetChildPos();
- xml.IntoElem();
- xml.FindElem();
- xml.RemoveElem();
- xml.InsertElem( aszNums[0] );
- xml.ResetMainPos();
- xml.AddElem( aszNums[6] );
- xml.IntoElem();
- xml.AddElem( aszNums[1] + strInside );
- xml.ResetMainPos();
- xml.AddElem( aszNums[3] + strInside );
- xml.ResetMainPos();
- xml.FindElem();
- xml.InsertElem( aszNums[0] + strInside );
- xml.RemoveElem();
- xml.ResetMainPos();
- xml.InsertElem( aszNums[0] + strInside );
- xml.FindElem();
- xml.AddElem( aszNums[2] + strInside );
- xml.FindElem();
- xml.AddElem( aszNums[4] + strInside );
- xml.FindElem();
- xml.AddElem( aszNums[5] + strInside );
- // Should result in the following (indented to illustrate):
- // <root>
- // <zero/>
- // <one/>
- // <two/>
- // <three/>
- // <four/>
- // <five/>
- // <six>
- // <zero_in6/>
- // <one_in6/>
- // <two_in6/>
- // <three_in6/>
- // <four_in6/>
- // <five_in6/>
- // </six>
- // </root>
- xml.ResetPos();
- for ( int nIT=0; nIT<7; ++nIT )
- {
- Check( xml.FindChildElem() );
- Check( xml.IntoElem() );
- Check( xml.GetTagName() == aszNums[nIT] );
- if ( nIT == 6 )
- {
- for ( int nITinner=0; nITinner<6; ++nITinner )
- {
- Check( xml.FindChildElem() );
- Check( xml.GetChildTagName() == aszNums[nITinner]+strInside );
- }
- }
- Check( xml.OutOfElem() );
- }
- // Palmer Test
- StartCheckZone( _T("Palmer Test") );
- xml.SetDoc( _T("") );
- Check( xml.AddElem( _T("one") ) );
- Check( xml.AddChildElem( _T("two") ) );
- Check( xml.RemoveChildElem() );
- Check( xml.AddChildElem( _T("three") ) );
- // <one>
- // <three/>
- // </one>
- xml.ResetPos();
- Check( xml.FindChildElem() );
- Check( xml.GetChildTagName() == _T("three") );
- // SetData Test, check empty elements, normal, and CDATA
- StartCheckZone( _T("SetData Test") );
- Check( xml.SetDoc( _T("<?xml version="1.0"?>rn<ROOT>data</ROOT>") ) );
- Check( xml.FindElem() );
- xml.SetData( _T("moredata<>&") );
- Check( xml.GetData() == _T("moredata<>&") );
- xml.SetData( _T("n<6"), 1 );
- Check( xml.GetData() == _T("n<6") );
- xml.SetData( _T("") );
- xml.AddChildElem( _T("CHILD") );
- xml.SetChildData( _T("data<>&") );
- Check( xml.GetChildData() == _T("data<>&") );
- xml.SetChildData( _T("n<6"), 1 );
- xml.InsertChildElem( _T("CHILD") );
- xml.IntoElem();
- Check( xml.SetData(_T("")) );
- xml.SetData( _T("final") );
- Check( xml.GetData() == _T("final") );
- xml.FindElem();
- Check( xml.GetData() == _T("n<6") );
- // Car Test, add and replace randomly chosen attributes
- StartCheckZone( _T("Random Car Part Test") );
- struct CarTestData { _TCHAR* szLow; _TCHAR* szUp; _TCHAR* szMix; } aCT[] =
- {
- _T("up"), _T("SKY"), _T("Light"),
- _T("down"), _T("FLOOR"), _T("Dust"),
- _T("left"), _T("DOOR"), _T("Handle"),
- _T("right"), _T("SEAT"), _T("Gear"),
- _T("back"), _T("TRUNK"), _T("Tread"),
- _T("forward"), _T("GRILL"), _T("Motor"),
- _T(""), _T(""), _T("")
- };
- xml.SetDoc( _T("") );
- xml.AddElem( _T("CAR") );
- int nAt, nPart;
- LPCTSTR szLow, szUp, szMix;
- for ( nAt = 0; nAt < 20; ++nAt )
- {
- szLow = aCT[RandInt(6)].szLow;
- szMix = aCT[RandInt(6)].szMix;
- xml.SetAttrib( szLow, szMix );
- Check( xml.GetAttrib(szLow) == szMix );
- }
- for ( nPart=0; nPart<100; ++nPart )
- {
- xml.AddChildElem( aCT[RandInt(6)].szUp );
- for ( nAt = 0; nAt < 8; ++nAt )
- {
- szLow = aCT[RandInt(6)].szLow;
- szMix = aCT[RandInt(6)].szMix;
- xml.SetChildAttrib( szLow, szMix );
- Check( xml.GetChildAttrib(szLow) == szMix );
- }
- szLow = aCT[RandInt(6)].szLow;
- szUp = aCT[RandInt(6)].szUp;
- xml.SetChildAttrib( szLow, szUp );
- Check( xml.GetChildAttrib(szLow) == szUp );
- }
- MCD_STR strCarDoc = xml.GetDoc();
- Check( xml.SetDoc( strCarDoc ) );
- // Car Attrib Speed Test
- StartCheckZone( _T("Car Attrib Speed Test") );
- xml.SetDoc( NULL );
- xml.AddElem( _T("CARSPEED") );
- TimeStart();
- for ( nPart=0; nPart<200; ++nPart )
- {
- xml.AddChildElem( aCT[RandInt(6)].szUp );
- for ( nAt = 0; nAt < 4; ++nAt )
- {
- szLow = aCT[RandInt(6)].szLow;
- szMix = aCT[RandInt(6)].szMix;
- xml.SetChildAttrib( szLow, szMix );
- szLow = aCT[RandInt(6)].szLow;
- szUp = aCT[RandInt(6)].szUp;
- xml.SetChildAttrib( szLow, szUp );
- }
- }
- int nAttribCreateTime = TimeStop();
- xml.ResetPos();
- TimeStart();
- MCD_STR strAttribName, strAttribs;
- while ( xml.FindChildElem() )
- {
- xml.IntoElem();
- nAt = 0;
- MCD_STRCLEAR(strAttribs);
- strAttribName = xml.GetAttribName( nAt );
- while ( ! MCD_STRISEMPTY(strAttribName) )
- {
- strAttribs += xml.GetAttrib( strAttribName );
- ++nAt;
- strAttribName = xml.GetAttribName( nAt );
- }
- xml.OutOfElem();
- }
- int nAttribNavTime = TimeStop();
- if ( nAttribCreateTime > 400 || nAttribNavTime > 400 )
- {
- CString strAttribTime;
- strAttribTime.Format( _T("Attribute Performance Results: create %d search %drn"), nAttribCreateTime, nAttribNavTime );
- m_csChecks += strAttribTime;
- }
- // SubDoc Test, use subdocument methods
- StartCheckZone( _T("SubDoc Test") );
- MCD_STR strTestAttrib = _T("<<&"symb>>");
- MCD_STR strSubDoc, strChildSubDoc;
- xml.SetDoc( _T("") );
- Check( xml.AddElem(_T("ORDER")) );
- xml.AddChildElem( _T("ITEM") );
- xml.IntoElem();
- Check( xml.AddChildSubDoc(_T("<?xml version="1.0"?>rn<ITEM/>rn")) );
- Check( xml.AddChildAttrib(_T("test"), strTestAttrib) );
- Check( xml.IntoElem() );
- Check( xml.AddChildElem(_T("NAME"), _T("carrots")) );
- Check( xml.AddChildElem(_T("QTY"), _T("1")) );
- strSubDoc = xml.GetSubDoc();
- Check( xml.OutOfElem() );
- strChildSubDoc = xml.GetChildSubDoc();
- Check( strSubDoc == strChildSubDoc );
- // Set xml2 from xml subdocument and modify
- Check( xml2.SetDoc(xml.GetChildSubDoc()) );
- Check( xml2.FindElem() );
- Check( xml2.SetAttrib(_T("subtest"), strTestAttrib) );
- Check( xml2.SetAttrib(_T("test"), _T("")) );
- Check( xml2.FindChildElem(_T("QTY")) );
- Check( xml2.RemoveChildElem() );
- strSubDoc = xml2.GetDoc();
- #if ! defined( MARKUP_MSXML )
- // MSXML: Invalid subdocument causes first-chance exception
- // It works, but this #ifdef avoids alarming us with the Debug output
- Check( ! xml.AddChildSubDoc(_T("invalidsub<doc>")) );
- #endif
- Check( xml.AddChildSubDoc(strSubDoc) );
- Check( xml.IntoElem() );
- Check( xml.RemoveElem() );
- Check( xml.AddSubDoc(strSubDoc) );
- Check( xml.GetAttrib(_T("subtest")) == strTestAttrib );
- Check( xml.OutOfElem() );
- Check( xml.InsertChildSubDoc(strSubDoc) );
- xml.ResetChildPos();
- Check( xml.InsertChildSubDoc(strSubDoc) );
- xml.IntoElem();
- xml.FindChildElem();
- Check( xml.GetChildData() == _T("carrots") );
- // Brannan Test: subdocument with no children getting flagged as empty in 7.0
- xml.ResetChildPos();
- xml.FindElem();
- xml.FindChildElem( _T("QTY") );
- xml.AddChildSubDoc( xml.GetChildSubDoc() );
- Check( xml.GetChildData() == _T("1") );
- // Comments Test, make sure it bypasses declaration, DTD, and comments
- StartCheckZone( _T("Comments Test") );
- CString csPTDoc =
- _T("<?xml version="1.0"?>n")
- _T("<!DOCTYPE PARSETEST [n")
- _T("<!ELEMENT PARSETEST (ITEM*)>n")
- _T("<!ATTLIST PARSETEST v CDATA "" s CDATA "">n")
- _T("<!ELEMENT ITEM ANY>n")
- _T("<!ATTLIST ITEM note CDATA "">n")
- _T("]>n")
- _T("<PARSETEST v="1" s='6'>n")
- _T("<!--- </P>& special chars -->n")
- _T("<ITEM note="Here's to "us""/>n")
- _T("<ITEM note="see data">Here's to "us"</ITEM>n")
- _T("</PARSETEST>n")
- ;
- Check( xml.SetDoc( csPTDoc ) );
- Check( xml.FindChildElem() );
- Check( xml.GetAttrib( _T("v") ) == _T("1") );
- Check( xml.GetAttribName(0) == _T("v") );
- Check( xml.GetAttribName(1) == _T("s") );
- Check( xml.GetAttribName(2) == _T("") );
- Check( xml.GetChildAttrib(_T("note")) == _T("Here's to "us"") );
- Check( xml.IntoElem() );
- Check( xml.FindElem() );
- Check( xml.GetData() == _T("Here's to "us"") );
- #if ! defined( MARKUP_MSXML )
- // Save Restore Position Test
- StartCheckZone( _T("Save Restore Position Test") );
- xml.SetDoc( NULL );
- xml.AddElem( _T("SavePosTest") );
- Check( ! xml.RestorePos() ); // crash in 6.6 and 7.x when no saved pos yet
- Check( xml.SavePos(_T("xD7")) ); // crash in 7.0 to 8.2 with non-ASCII
- xml.IntoElem(); // no main position
- xml.SavePos();
- xml.ResetPos();
- xml.RestorePos();
- Check( xml.OutOfElem() );
- Check( xml.GetTagName() == _T("SavePosTest") );
- CStringArray csaPosNames;
- int nSavePos;
- const int nSavePosTestSize = 100;
- for ( nSavePos=0; nSavePos<nSavePosTestSize; ++nSavePos )
- {
- // Find an unused random name
- MCD_STR strPosName;
- while ( 1 )
- {
- int nPosNameLen = RandInt(10)+1;
- for ( int nPosNameChar=0; nPosNameChar<nPosNameLen; ++nPosNameChar )
- strPosName += (_TCHAR)(_T('A')+RandInt(26));
- if ( ! xml.RestorePos(strPosName) )
- break;
- }
- xml.AddChildElem( strPosName );
- xml.SavePos( strPosName );
- csaPosNames.Add( MCD_2PCSZ(strPosName) );
- }
- xml2 = xml;
- xml = xml2;
- for ( nSavePos=0; nSavePos<csaPosNames.GetSize(); ++nSavePos )
- {
- Check( xml.RestorePos((LPCTSTR)csaPosNames[nSavePos]) );
- Check( xml.GetChildTagName() == (LPCTSTR)csaPosNames[nSavePos] );
- // Move saved position to TEMP2 child
- Check( xml.IntoElem() );
- Check( xml.AddChildElem( _T("TEMP") ) );
- Check( xml.IntoElem() );
- Check( xml.AddChildElem( _T("TEMP2") ) );
- Check( xml.IntoElem() );
- xml.SavePos( (LPCTSTR)csaPosNames[nSavePos] );
- if ( RandInt(2) ) // 1 in 2
- Check( xml.OutOfElem() );
- if ( RandInt(2) ) // 1 in 2
- Check( xml.OutOfElem() );
- if ( nSavePos % 2 )
- xml.RemoveElem();
- }
- for ( nSavePos=0; nSavePos<csaPosNames.GetSize(); ++nSavePos )
- {
- if ( nSavePos % 2 )
- Check( ! xml.RestorePos((LPCTSTR)csaPosNames[nSavePos]) );
- else
- {
- Check( xml.RestorePos((LPCTSTR)csaPosNames[nSavePos]) );
- Check( xml.OutOfElem() );
- Check( xml.OutOfElem() );
- Check( xml.GetTagName() == (LPCTSTR)csaPosNames[nSavePos] );
- }
- }
- // Save Restore Example
- int anSomeData[] = { 10, 30, 50, 20, 90, 0 };
- int nD;
- StartCheckZone( _T("Save Restore Example") );
- xml.SetDoc( NULL );
- xml.AddElem( _T("SavePosExample") );
- xml.IntoElem(); // no main position
- xml.AddElem( _T("Total") );
- xml.SavePos( _T("Total") );
- xml.AddElem( _T("Data") );
- xml.SavePos( _T("Data") );
- for ( nD=0; anSomeData[nD]; ++nD )
- {
- xml.RestorePos( _T("Total") );
- xml.SetData( _ttoi(MCD_2PCSZ(xml.GetData())) + anSomeData[nD] );
- xml.RestorePos( _T("Data") );
- xml.AddChildElem( _T("Value"), anSomeData[nD] );
- }
- xml2.SetDoc( NULL );
- xml2.AddElem( _T("SavePosExample") );
- xml2.IntoElem();
- xml2.AddElem( _T("Total") );
- xml2.AddElem( _T("Data") );
- for ( nD=0; anSomeData[nD]; ++nD )
- {
- xml2.ResetMainPos();
- xml2.FindElem();
- xml2.SetData( _ttoi(MCD_2PCSZ(xml2.GetData())) + anSomeData[nD] );
- xml2.FindElem();
- xml2.AddChildElem( _T("Value"), anSomeData[nD] );
- }
- Check( xml.GetDoc() == xml2.GetDoc() );
- // Prepend XML Declaration test
- // (not implemented in MSXML because modifying XML Declaration is restricted)
- StartCheckZone( _T("Prepend XML Declaration") );
- Check( ! xml.SetDoc( _T("<?xml version="1.0"?>rn") ) );
- Check( xml.AddElem( _T("ROOT") ) );
- Check( xml.IntoElem() );
- Check( xml.AddElem( _T("CHILD") ) );
- xml.ResetPos();
- Check( xml.FindElem( _T("ROOT") ) );
- Check( xml.FindChildElem( _T("CHILD") ) );
- // Copy Constructor Test (not MSXML!)
- StartCheckZone( _T("Copy Constructor Test non-MSXML") );
- xml.SetDoc( NULL );
- xml2 = xml;
- xml.AddElem( _T("R") );
- xml.AddChildElem( _T("C") );
- xml2 = xml;
- xml2.FindChildElem();
- Check( xml2.GetChildTagName() == xml.GetChildTagName() );
- xml.IntoElem();
- xml.SavePos();
- xml.RemoveElem();
- xml2 = xml;
- Check( ! xml2.RestorePos() );
- // Ill-formed Markup Test
- StartCheckZone( _T("Ill-formed Markup Test") );
- Check( ! xml.SetDoc( _T("<P>Hello<BR>Hi") ) );
- Check( xml.FindElem( _T("P") ) );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("Hello") );
- Check( xml.FindElem( _T("BR") ) );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("Hi") );
- Check( xml.AddElem( _T("P"), _T("Hola") ) );
- Check( xml.GetData() == _T("Hola") );
- Check( ! xml.SetDoc( _T("<P>Hello<BR>Hi</P>") ) );
- Check( xml.FindElem( _T("P") ) );
- xml.SetDocFlags( xml.MDF_IGNORECASE );
- Check( xml.SetDoc( _T("<A></a>") ) );
- Check( xml.FindElem( _T("a") ) );
- Check( xml.SetAttrib( _T("a1"), _T("V1") ) );
- Check( xml.SetAttrib( _T("A1"), _T("v1") ) );
- Check( ! xml.AddSubDoc( _T("<b>") ) );
- xml.ResetPos();
- Check( xml.FindElem( _T("a") ) );
- Check( xml.GetAttrib(_T("A1")) == _T("v1") );
- Check( xml.GetAttrib(_T("a1")) == _T("v1") );
- Check( xml.FindElem( _T("B") ) );
- xml.SetDocFlags( 0 );
- Check( ! xml.SetDoc( _T("<A><B></C></B></A>") ) );
- Check( xml.FindElem() );
- Check( xml.IntoElem() );
- Check( xml.FindElem( _T("B") ) );
- Check( xml.IntoElem() );
- Check( xml.FindNode() == xml.MNT_LONE_END_TAG );
- Check( xml.GetTagName() == _T("C") );
- Check( xml.GetData() == _T("C") );
- Check( xml.OutOfElem() );
- Check( xml.SetElemContent( _T("data") ) );
- Check( ! xml.InsertSubDoc( _T("<S/><S/>") ) );
- Check( ! xml.SetElemContent( _T("<SC>") ) );
- Check( xml.FindElem( _T("S") ) );
- // Multi-Rooted Subdoc Test
- StartCheckZone( _T("Multi-Rooted Subdoc Test") );
- Check( xml.SetElemContent( _T("<SC><SCC/></SC><SC><SCC/></SC><SC/>") ) );
- Check( xml.FindChildElem( _T("SC") ) );
- Check( xml.SetChildAttrib( _T("asc"), _T("verbose") ) );
- Check( xml.IntoElem() );
- Check( xml.FindChildElem( _T("SCC") ) );
- Check( xml.SetChildAttrib( _T("ascc"), _T("verbose") ) );
- Check( xml.OutOfElem() );
- xml.ResetMainPos();
- Check( xml.FindElem( _T("S") ) );
- Check( xml.FindChildElem( _T("SC") ) );
- Check( xml.SetChildAttrib( _T("asc"), _T("verbose") ) );
- Check( xml.SetChildAttrib( _T("asc2"), 2 ) );
- Check( xml.FindElem( _T("S") ) );
- Check( xml.FindChildElem( _T("SC") ) );
- Check( xml.FindChildElem( _T("SC") ) );
- Check( xml.FindElem( _T("B") ) );
- // Don't mistake end slash on non-quoted value for empty element (see cDminus2)
- StartCheckZone( _T("Non Quoted Attribute Test") );
- Check( xml.SetDoc( _T("<A href=/path/>ok</A>") ) );
- Check( xml.FindElem() );
- Check( xml.GetAttrib(_T("href")) == _T("/path/") );
- // AddElem Flags Test
- StartCheckZone( _T("AddElem Flags Test") );
- xml.SetDoc( NULL );
- xml.AddElem( _T("ROOT"), NULL, xml.MNF_WITHNOLINES );
- xml.AddChildElem( _T("A"), _T(""), xml.MNF_WITHNOEND|xml.MNF_WITHNOLINES );
- Check( xml.IntoElem() );
- Check( xml.AddElem( _T("A"), _T(""), xml.MNF_WITHNOEND ) );
- Check( xml.AddChildElem( _T("B") ) );
- Check( xml.AddChildElem( _T("B"), _T("D") ) );
- Check( xml.OutOfElem() );
- xml.AddChildElem( _T("BR"), _T(""), xml.MNF_WITHXHTMLSPACE );
- xml.SetChildAttrib( _T("class"), _T("MinorBrk") );
- Check( ! xml.SetDoc( _T("<A>test") ) );
- Check( xml.FindElem() );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("test") );
- Check( ! xml.SetDoc( _T("<A>testrn") ) );
- Check( xml.FindElem() );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( TruncToLastEOL(MCD_2PCSZ(xml.GetData())) == _T("test") );
- Check( ! xml.SetDoc( _T("<A>testrn <B>okay") ) );
- Check( xml.FindElem() );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( TruncToLastEOL(MCD_2PCSZ(xml.GetData())) == _T("test") );
- Check( xml.FindElem() );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( TruncToLastEOL(MCD_2PCSZ(xml.GetData())) == _T("okay") );
- xml.ResetMainPos();
- Check( xml.FindElem() );
- Check( xml.SetData(_T("t2")) );
- Check( xml.GetData() == _T("t2") );
- Check( xml.SetData(_T("test3")) );
- Check( xml.GetData() == _T("test3") );
- xml.SetDoc( NULL );
- xml.AddElem( _T("P"), _T(""), xml.MNF_WITHNOLINES );
- xml.IntoElem();
- Check( xml.AddNode( xml.MNT_TEXT, _T("First line") ) );
- Check( xml.AddElem( _T("BR"), _T(""), xml.MNF_WITHNOEND | xml.MNF_WITHNOLINES ) );
- Check( xml.AddNode( xml.MNT_TEXT, _T("Second line") ) );
- Check( xml.GetDoc() == _T("<P>First line<BR>Second line</P>") );
- xml.OutOfElem();
- xml.SetElemContent( _T("First line<BR>Second line") );
- Check( xml.GetDoc() == _T("<P>First line<BR>Second line</P>") );
- // Ill-formed SubDoc Fragment Test
- StartCheckZone( _T("SubDoc Fragment Test") );
- Check( ! xml.SetDoc( _T("<A/><B/>") ) );
- Check( ! xml.SetDoc( _T(" something ") ) );
- Check( ! xml.AddSubDoc( _T("<A>") ) );
- Check( ! xml.AddSubDoc( _T("<B>") ) );
- Check( ! xml.AddSubDoc( _T("</A>") ) ); // lone end tag
- Check( xml.AddSubDoc( _T("<C/>") ) );
- Check( ! xml.SetElemContent( _T("<CC>") ) );
- Check( xml.GetTagName() == _T("C") );
- Check( xml.FindChildElem() );
- Check( xml.GetChildTagName() == _T("CC") );
- Check( xml.SetElemContent( _T("d") ) );
- Check( xml.GetData() == _T("d") );
- Check( ! xml.SetElemContent( _T("<DD>") ) );
- Check( xml.FindChildElem() );
- Check( xml.GetChildTagName() == _T("DD") );
- // Replace element between two text nodes
- StartCheckZone( _T("Replace Element Test") );
- Check( xml.SetDoc( _T("T<A/>T") ) );
- Check( xml.FindElem( _T("A") ) );
- Check( xml.RemoveNode() );
- Check( xml.GetNodeType() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("T") );
- Check( xml.AddNode( xml.MNT_ELEMENT, _T("B") ) );
- Check( xml.SetAttrib( _T("v"), 1 ) );
- Check( xml.GetTagName() == _T("B") );
- Check( xml.GetAttrib( _T("v") ) == _T("1") );
- Check( xml.IntoElem() );
- Check( xml.AddNode( xml.MNT_TEXT, _T("T0") ) );
- Check( xml.AddNode( xml.MNT_ELEMENT, _T("C1") ) );
- Check( xml.AddNode( xml.MNT_TEXT, _T("T1") ) );
- Check( xml.AddNode( xml.MNT_ELEMENT, _T("C2") ) );
- Check( xml.AddNode( xml.MNT_TEXT, _T("T2") ) );
- xml.ResetMainPos();
- Check( xml.FindElem( _T("C1") ) );
- Check( xml.RemoveNode() );
- Check( xml.GetNodeType() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("T0") );
- Check( xml.AddNode( xml.MNT_ELEMENT, _T("C1") ) );
- Check( xml.FindElem( _T("C2") ) );
- Check( xml.RemoveNode() );
- Check( xml.GetNodeType() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("T1") );
- Check( xml.AddNode( xml.MNT_ELEMENT, _T("C2") ) );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("T2") );
- xml.ResetMainPos();
- Check( xml.FindElem( _T("C2") ) );
- Check( xml.IntoElem() );
- Check( xml.AddNode( xml.MNT_ELEMENT, _T("D3") ) );
- Check( xml.AddNode( xml.MNT_TEXT, _T("T0") ) );
- Check( xml.AddNode( xml.MNT_ELEMENT, _T("D4") ) );
- Check( xml.IntoElem() );
- Check( xml.AddNode( xml.MNT_ELEMENT, _T("E1") ) );
- Check( xml.SetData( _T("X") ) );
- Check( xml.OutOfElem() );
- Check( xml.OutOfElem() );
- xml.ResetMainPos();
- Check( xml.FindElem() );
- Check( xml.GetTagName() == _T("C1") );
- Check( xml.IntoElem() );
- Check( xml.AddNode( xml.MNT_TEXT, _T("T1") ) );
- Check( xml.AddNode( xml.MNT_ELEMENT, _T("D2") ) );
- Check( xml.AddNode( xml.MNT_TEXT, _T("T2") ) );
- xml.ResetMainPos();
- Check( xml.FindElem() );
- Check( xml.GetTagName() == _T("D2") );
- xml.ResetMainPos();
- Check( xml.InsertNode( xml.MNT_ELEMENT, _T("D1") ) );
- Check( xml.InsertNode( xml.MNT_TEXT, _T("TEST") ) );
- Check( xml.SetData( _T("T0") ) );
- Check( xml.FindNode() == xml.MNT_ELEMENT );
- Check( xml.SetData( _T("X") ) );
- Check( xml.FindElem( _T("D2") ) );
- xml.ResetMainPos();
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("T0") );
- Check( xml.OutOfElem() );
- xml.ResetMainPos();
- Check( xml.FindElem( _T("C2") ) );
- Check( xml.IntoElem() );
- Check( xml.FindNode() == xml.MNT_ELEMENT );
- Check( xml.GetTagName() == _T("D3") );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("T0") );
- Check( xml.FindNode() == xml.MNT_ELEMENT );
- Check( xml.GetTagName() == _T("D4") );
- Check( xml.FindChildElem() );
- Check( xml.GetChildData() == _T("X") );
- // With Entities Test
- StartCheckZone( _T("With Entities Test") );
- xml.SetDoc(NULL);
- Check( xml.AddElem( _T("A"), _T(" "), xml.MNF_WITHREFS ) );
- Check( xml.GetData() == _T(" ") );
- Check( xml.AddChildElem( _T("CH"), _T("&"), xml.MNF_WITHREFS ) );
- Check( xml.GetChildData() == _T("&") );
- Check( xml.SetChildData( _T(" "), xml.MNF_WITHREFS ) );
- Check( xml.GetChildData() == _T(" ") );
- Check( xml.SetChildData( _T("&"), xml.MNF_WITHREFS ) );
- Check( xml.GetChildData() == _T("&") );
- Check( xml.SetChildData( _T("&&"), xml.MNF_WITHREFS ) );
- Check( xml.GetChildData() == _T("&&") );
- Check( xml.SetChildData( _T(" 2"), xml.MNF_WITHREFS ) );
- Check( xml.GetChildData() == _T(" 2") );
- Check( xml.EscapeText( _T(" 2"), xml.MNF_WITHREFS ) == _T("&#3 2") );
- Check( xml.EscapeText( _T("""), xml.MNF_ESCAPEQUOTES ) == _T(""") );
- Check( xml.EscapeText( _T(""") ) == _T(""") );
- Check( xml.EscapeText( _T(" '"), xml.MNF_WITHREFS|xml.MNF_ESCAPEQUOTES ) == _T(" '") );
- // Node Error Test -- even gibberish will not stop node navigation
- // But there is no guaranteed node parsing system with gibberish
- StartCheckZone( _T("Node Error Test") );
- Check( ! xml.SetDoc( _T("<) <> <* ><<>>") ) );
- BOOL bReachedEndOfGibberish = FALSE;
- int nGibberishNodes = 0;
- while ( nGibberishNodes < 100 )
- {
- int nGNType = xml.FindNode();
- if ( nGNType == 0 )
- {
- bReachedEndOfGibberish = TRUE;
- break;
- }
- else if ( nGNType == xml.MNT_NODE_ERROR )
- {
- MCD_STR strJunkNode = xml.GetData();
- Check( ! MCD_STRISEMPTY(strJunkNode) );
- }
- ++nGibberishNodes;
- }
- Check( bReachedEndOfGibberish );
- #endif
- // Character Reference Test, check various character decoding issues
- StartCheckZone( _T("Character Reference Test") );
- Check( xml.SetDoc(
- _T("<?xml version="1.0" encoding="UTF-8"?>n")
- _T("<CHAR v='C""S'>n")
- _T("<CORE><<>>&&''""</CORE>n")
- _T("<SPACE xml:space="preserve">  		 
</SPACE>n")
- _T("<REF>中国人</REF>n")
- _T("</CHAR>n")
- ) );
- Check( xml.FindChildElem(_T("CORE")) );
- Check( xml.GetAttrib( _T("v") ) == _T("C""S") );
- Check( xml.GetChildData() == _T("<<>>&&''""") );
- Check( xml.FindChildElem(_T("SPACE")) );
- Check( xml.GetChildData() == _T(" ttnn") );
- Check( xml.FindChildElem(_T("REF")) );
- #if defined( _UNICODE )
- Check( xml.GetChildData() == _T("x4E2Dx56FDx4EBA") );
- #elif ! defined( MARKUP_MSXML )
- #if defined( _MBCS )
- setlocale( LC_ALL, "C" );
- if ( setlocale(LC_CTYPE,".936") ) // is GB2312 DBCS code page available?
- Check( xml.GetChildData() == _T("xD6xD0xB9xFAxC8xCB") ); // DBCS
- else
- Check( xml.GetChildData() == _T("中国人") ); // unmodified
- #else
- // UTF-8 (note: MSXML converts chinese character reference to UCS-2, but not UTF-8)
- Check( xml.GetChildData() == _T("xE4xB8xADxE5x9BxBDxE4xBAxBA") );
- #endif
- #endif
- #if defined( _MBCS )
- // GB2312 Test, check getting and setting of sample GB2312 chars
- StartCheckZone( _T("GB2312 Test") );
- MCD_STR strSampleGB2312Chars = _T("xD6xD0xB9xFAxC8xCB");
- Check( xml.SetDoc(
- _T("<?xml version="1.0" encoding="GB2312"?>n")
- _T("<CHAR>xD6xD0xB9xFAxC8xCB</CHAR>")
- ) );
- Check( xml.FindElem() );
- Check( xml.GetData() == strSampleGB2312Chars );
- Check( xml.SetAttrib(_T("v"),strSampleGB2312Chars) );
- Check( xml.GetAttrib(_T("v")) == strSampleGB2312Chars );
- #endif
- // Empty Data Test
- StartCheckZone( _T("Empty Data") );
- xml.SetDoc( NULL );
- Check( xml.AddElem(_T("A"),_T("data")) );
- Check( xml.SetData(_T("")) );
- Check( xml.AddChildElem(_T("B"),_T("data")) );
- Check( xml.SetChildData(_T("")) );
- Check( xml.IntoElem() );
- Check( xml.IntoElem() );
- Check( xml.AddElem(_T("C"),_T("data")) );
- Check( xml.OutOfElem() );
- Check( xml.OutOfElem() );
- Check( xml.GetChildTagName() == _T("B") );
- Check( xml.GetTagName() == _T("A") );
- // Depth First Traversal Test, loop through all elements
- StartCheckZone( _T("Depth First Traversal Test") );
- xml.SetDoc( _T("<A><B><C/><C/><C><D/></C></B><B><C/></B></A>") );
- CString csListOfTagNames;
- BOOL bFinished = FALSE;
- if ( xml.FindElem() )
- csListOfTagNames = MCD_2PCSZ(xml.GetTagName());
- if ( ! xml.FindChildElem() )
- bFinished = TRUE;
- while ( ! bFinished )
- {
- // Process Element
- xml.IntoElem();
- csListOfTagNames += MCD_2PCSZ(xml.GetTagName());
- // Next element (depth first)
- BOOL bFound = xml.FindChildElem();
- while ( ! bFound && ! bFinished )
- {
- if ( xml.OutOfElem() )
- bFound = xml.FindChildElem();
- else
- bFinished = TRUE;
- }
- }
- Check( csListOfTagNames == _T("ABCCCDBC") );
- // Depth First Traversal Test 2, include root in loop
- StartCheckZone( _T("Depth First Traversal Test 2") );
- csListOfTagNames.Empty();
- bFinished = FALSE;
- xml.ResetPos();
- if ( ! xml.FindElem() )
- bFinished = TRUE;
- while ( ! bFinished )
- {
- // Process element
- CString csTag = MCD_2PCSZ(xml.GetTagName());
- csListOfTagNames += csTag;
- // Next element (depth first)
- BOOL bFound = xml.FindChildElem();
- while ( ! bFound && ! bFinished )
- {
- if ( xml.OutOfElem() )
- bFound = xml.FindChildElem();
- else
- bFinished = TRUE;
- }
- if ( bFound )
- xml.IntoElem();
- }
- Check( csListOfTagNames == _T("ABCCCDBC") );
- // INI-Style Section Entry Test
- StartCheckZone( _T("INI-Style Section Entry Test") );
- MCD_STR strValue;
- xml.SetDoc( NULL );
- SetEntry( xml, _T("Settings"), _T("Count"), _T("10") );
- SetEntry( xml, _T("Settings"), _T("User"), _T("John Smith") );
- SetEntry( xml, _T("Files"), _T("Database"), _T("test1.txt") );
- SetEntry( xml, _T("Files"), _T("Database"), _T("test2.txt") ); // overwrite
- Check( FindEntry(xml,_T("Settings"),_T("Count"),strValue) );
- Check( strValue == _T("10") );
- Check( FindEntry(xml,_T("Files"),_T("Database"),strValue) );
- Check( strValue == _T("test2.txt") );
- // Load/Save Test
- StartCheckZone( _T("Load/Save Test") );
- MCD_STR strLSFilename = _T("CMarkupRunTest.xml");
- BOOL bLSFileLoaded = xml.Load( strLSFilename );
- if ( bLSFileLoaded )
- {
- // Load/Save
- xml.FindElem();
- Check( xml.Save(strLSFilename) );
- Check( xml2.Load(strLSFilename) );
- Check( xml2.FindElem() );
- Check( xml2.GetTagName() == xml.GetTagName() );
- }
- else
- m_csChecks += _T("File I/O tests skippedrn");
- // Node Test, navigate/modify comments and other nodes
- StartCheckZone( _T("Node Test") );
- csPTDoc =
- _T("<?xml version="1.0"?>n")
- // To get MSXML to preserve whitespace in mixed content
- // you can use a DTD and an xml:space attribute
- // otherwise MSXML would not have a whitespace node between </B> <I>
- // See "White Space Handling" in the XML Specification
- // Also note the mixed content declaration of ITEM
- _T("<!DOCTYPE PARSETEST [n")
- _T("<!ELEMENT PARSETEST (ITEM*)>n")
- _T("<!ATTLIST PARSETEST v CDATA '' s CDATA ''>n")
- _T("<!ELEMENT ITEM (#PCDATA|B|I)*>n")
- _T("<!ATTLIST ITEM note CDATA '' xml:space (default|preserve) 'preserve'>n")
- _T("<!ELEMENT B ANY>n")
- _T("<!ELEMENT I ANY>n")
- _T("]>n")
- _T("<!--tightcomment-->n")
- _T("<PARSETEST v="1" s='6'>n")
- _T("<!-- mid comment -->n")
- _T("<ITEM note="hi"/>n")
- _T("<ITEM note="see data">hi</ITEM>n")
- _T("<ITEM> mixed <B>content</B> <I>okay</I></ITEM>n")
- _T("</PARSETEST>n")
- _T("<!-- end comment -->n")
- ;
- Check( xml.SetDoc(csPTDoc) );
- Check( xml.FindNode() == xml.MNT_PROCESSING_INSTRUCTION );
- Check( xml.GetData() == _T("xml version="1.0"") );
- Check( xml.FindNode(xml.MNT_COMMENT) );
- Check( xml.GetData() == _T("tightcomment") );
- Check( xml.SetData( _T("comment 1 changed") ) );
- Check( xml.GetData() == _T("comment 1 changed") );
- Check( xml.FindNode(xml.MNT_EXCLUDE_WHITESPACE) == xml.MNT_ELEMENT );
- Check( xml.GetTagName() == _T("PARSETEST") );
- Check( xml.IntoElem() );
- Check( xml.FindNode(xml.MNT_EXCLUDE_WHITESPACE) == xml.MNT_COMMENT );
- Check( xml.GetData() == _T(" mid comment ") );
- Check( xml.FindNode(xml.MNT_EXCLUDE_WHITESPACE) == xml.MNT_ELEMENT );
- Check( xml.FindNode(xml.MNT_EXCLUDE_WHITESPACE) == xml.MNT_ELEMENT );
- Check( xml.FindNode(xml.MNT_EXCLUDE_WHITESPACE) == xml.MNT_ELEMENT );
- Check( xml.IntoElem() );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( xml.GetData() == _T(" mixed ") );
- Check( xml.FindNode() == xml.MNT_ELEMENT );
- Check( xml.GetTagName() == _T("B") );
- Check( xml.IntoElem() );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("content") );
- Check( xml.FindNode() == 0 );
- Check( xml.OutOfElem() );
- Check( xml.FindNode() == xml.MNT_WHITESPACE );
- Check( xml.GetData() == _T(" ") );
- Check( xml.FindNode() == xml.MNT_ELEMENT );
- Check( xml.GetTagName() == _T("I") );
- Check( xml.IntoElem() );
- Check( xml.FindNode() == xml.MNT_TEXT );
- Check( xml.GetData() == _T("okay") );
- Check( xml.FindNode() == 0 );
- Check( xml.OutOfElem() );
- Check( xml.FindNode() == 0 );
- Check( xml.OutOfElem() );
- // Create a document xml2 by copying all nodes of document xml
- // Resulting document xml2 differs only by attributes and CRLF instead of just LF
- xml2.SetDoc( NULL );
- xml.ResetPos();
- BOOL bBypassWhitespace = FALSE;
- while ( 1 )
- {
- int nType = xml.FindNode();
- if ( nType == 0 )
- {
- if ( ! xml.OutOfElem() )
- break;
- xml2.OutOfElem();
- }
- else
- {
- if ( bBypassWhitespace && nType == xml.MNT_WHITESPACE )
- bBypassWhitespace = FALSE;
- else
- {
- if ( nType == xml.MNT_ELEMENT )
- {
- xml2.AddNode( nType, xml.GetTagName() );
- xml.IntoElem();
- xml2.IntoElem();
- }
- else
- {
- xml2.AddNode( nType, xml.GetData() );
- if ( nType & ( xml.MNT_PROCESSING_INSTRUCTION|xml.MNT_COMMENT|xml.MNT_DOCUMENT_TYPE ) )
- {
- // Bypass whitespace on nodes that automatically add it
- xml2.FindNode( xml2.MNT_WHITESPACE );
- bBypassWhitespace = TRUE;
- }
- }
- }
- }
- }
- // Create a document representing depth first traversal of nodes excluding whitespace
- xml3.SetDoc( NULL );
- xml3.AddElem( _T("NodeTraversal") );
- xml2.ResetPos();
- while ( 1 )
- {
- int nType = xml2.FindNode();
- if ( nType == 0 )
- {
- if ( ! xml2.OutOfElem() )
- break;
- }
- else if ( nType != xml2.MNT_WHITESPACE )
- {
- if ( nType == xml2.MNT_ELEMENT )
- xml3.AddChildElem( _T("Node"), xml2.GetTagName() );
- else
- xml3.AddChildElem( _T("Node"), xml2.GetData() );
- xml3.AddChildAttrib( _T("type"), xml2.GetNodeType() );
- if ( nType == xml2.MNT_ELEMENT )
- xml2.IntoElem();
- }
- }
- // Now double check node traversal in xml3 against original xml
- // This helps verify xml --> xml2 node by node copy
- // and xml2 --> xml3 node traversal transformation into elements
- xml.ResetPos();
- xml3.ResetPos();
- while ( 1 )
- {
- int nType = xml.FindNode();
- if ( nType == 0 )
- {
- if ( ! xml.OutOfElem() )
- break;
- }
- else if ( nType != xml.MNT_WHITESPACE )
- {
- if ( ! xml3.FindChildElem() )
- {
- Alert( _T("Error: missing element in traversal double check") );
- break;
- }
- if ( nType != _ttoi(MCD_2PCSZ(xml3.GetChildAttrib(_T("type")))) )
- {
- Alert( _T("Error: wrong type in traversal double check") );
- break;
- }
- if ( nType == xml.MNT_ELEMENT )
- xml.IntoElem();
- }
- }
- // Add element and remove it using RemoveNode
- // Note that AddElem adds a CRLF (whitespace) after the element
- // which is removed as a separate node
- xml.ResetPos();
- xml.FindElem();
- xml.InsertChildElem( _T("ITEM"), _T("RemoveNode") );
- xml.IntoElem();
- xml.RemoveNode();
- if ( xml.FindNode( xml.MNT_WHITESPACE ) )
- xml.RemoveNode();
- // Remove all whitespace between nodes
- xml3.ResetPos();
- while ( 1 )
- {
- int nType = xml3.FindNode( xml3.MNT_WHITESPACE | xml3.MNT_ELEMENT );
- if ( nType == 0 )
- {
- if ( ! xml3.OutOfElem() )
- break;
- }
- else if ( nType == xml3.MNT_WHITESPACE )
- {
- if ( ! xml3.RemoveNode() )
- Alert( _T("Error: RemoveNode()") );
- }
- else // element
- {
- xml3.IntoElem();
- }
- }
- // Success?
- CString csResults;
- if ( ! m_nErrorCount )
- csResults.Format( _T("RunTest complete %d tests and %d checks for this buildrn"), m_nTotalZones, m_nTotalChecks );
- else
- csResults.Format( _T("Errors: %drn"), m_nErrorCount );
- OutputTestResults( csResults + m_csChecks );
- return 0;
- }
- void CMarkupDlg::OnButtonBrowse()
- {
- LPCTSTR szTypes =
- _T("XML Files (*.xml)|*.xml|")
- _T("Text Files (*.txt)|*.txt|")
- _T("All Files (*.*)|*.*||");
- CFileDialog dlg( TRUE, _T("xml"), NULL, OFN_HIDEREADONLY, szTypes );
- if ( dlg.DoModal() == IDCANCEL )
- return;
- CString csFilename = dlg.GetPathName();
- GetDlgItem( IDC_EDIT_FILE )->SetWindowText( csFilename );
- OutputParseResults( _T("") );
- }
- int CMarkupDlg::RandInt( int nNumber )
- {
- //
- // Return an integer between 0 and nNumber
- //
- static BOOL bSeeded = FALSE;
- if ( ! bSeeded )
- {
- srand( (unsigned)time( NULL ) );
- bSeeded = TRUE;
- }
- // Sometimes rand() returns something close enough to 1 to make nRandom == nNumber
- int nRandom = rand() * nNumber / RAND_MAX;
- if ( nRandom == nNumber )
- --nRandom;
- return nRandom;
- }
- void CMarkupDlg::TimeStart()
- {
- // Keep track of time before operation
- GetSystemTime( &m_stBefore );
- }
- int CMarkupDlg::TimeStop()
- {
- // 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;
- return nDiff;
- }
- void CMarkupDlg::OutputParseResults( CString csMsg )
- {
- GetDlgItem( IDC_ST_PARSE_RESULTS )->SetWindowText( csMsg );
- }
- void CMarkupDlg::OnButtonParse()
- {
- OutputParseResults( _T("") );
- // Get pathname
- CString csPath;
- GetDlgItem( IDC_EDIT_FILE )->GetWindowText( csPath );
- if ( csPath.IsEmpty() )
- {
- OnButtonBrowse();
- GetDlgItem( IDC_EDIT_FILE )->GetWindowText( csPath );
- if ( csPath.IsEmpty() )
- return;
- RedrawWindow();
- }
- // Load and parse with CMarkup
- CWaitCursor wait;
- #ifdef MARKUP_MSXML
- CMarkupMSXML xml;
- #else
- CMarkup xml;
- #endif
- TimeStart();
- BOOL bResult = xml.Load( (LPCTSTR)csPath );
- int nTime = TimeStop();
- // Display results
- CString csMsg;
- CString csError = MCD_2PCSZ(xml.GetError());
- if ( bResult )
- csMsg.Format( _T("Loaded and parsed in %s milliseconds"), AddNumSeps(nTime) );
- else
- csMsg.Format( _T("Load or parse error (after %s milliseconds)"), AddNumSeps(nTime) );
- if ( ! csError.IsEmpty() )
- csMsg = csMsg + _T("rn") + csError;
- OutputParseResults( csMsg );
- }
- void CMarkupDlg::OnTestXmlNew()
- {
- // TODO: Add your control notification handler code here
- // CString csFilename;
- // GetDlgItem( IDC_EDIT_FILE )->GetWindowText( csFilename );
- // #if ! defined(_UNICODE) && ! defined(_MBCS)
- // csFilename = WinAToUTF8( csFilename );
- // #endif
- // MCD_STR strFilename = csFilename;
- // SetEntry( xmlSettings, _T("Settings"), _T("Filename"), strFilename );
- // CRect rect;
- // GetWindowRect( &rect );
- // CString csPos;
- // csPos.Format( _T("%d,%d"), rect.left, rect.top );
- // MCD_STR strPos = csPos;
- // CString pTargetFilePath="d:\1.xml";
- // CMarkup xml;
- // SetEntry( xml, _T("Settings"), _T("Position"), "社会主义好" );
- // xml.Save( (LPCTSTR)pTargetFilePath );
- // AfxMessageBox("生成完毕,请查看是否生成成功!");
- // StartXMLContent();//必须要先设定parameters,才能设定Reactions
- // StartAddListOfSpecies();
- // AddListOfSpecies("Node1",0);
- // AddListOfSpecies("Node2",0);
- // AddListOfSpecies("Node3",0);
- // AddListOfSpecies("Node0",10);
- // AddListOfSpecies("Node4",0);
- // EndAddListOfSpecies();
- //
- // StartAddListOfParameters();
- // AddListOfParameters("J0_J0_k",0.1);
- // AddListOfParameters("J1_J1_k",0.1);
- // AddListOfParameters("J2_J2_k",0.1);
- // AddListOfParameters("J3_J3_k",0.1);
- // AddListOfParameters("J0_k",0.1);
- // AddListOfParameters("J1_k",0.1);
- // AddListOfParameters("J2_k",0.1);
- // AddListOfParameters("J3_k",0.1);
- // EndAddListOfParameters();
- //
- // StartReactionContent();
- // AddListOfReaction("J0","Node0","Node1","J0_J0_k*Node0");
- // AddListOfReaction("J1","Node1","Node2","J1_J1_k*Node1");
- // AddListOfReaction("J2","Node2","Node3","J2_J2_k*Node2");
- // AddListOfReaction("J3","Node3","Node4","J3_J3_k*Node3");
- // EndReactionContent();
- //
- // EndXMLContent();
- StartXMLContent();//必须要先设定parameters,才能设定Reactions
- StartAddListOfSpecies();
- AddListOfSpecies("E1",0.5);
- AddListOfSpecies("E2",0.5);
- AddListOfSpecies("E3",0.5);
- AddListOfSpecies("E4",0.5);
- AddListOfSpecies("E5",0.5);
- EndAddListOfSpecies();
- StartAddListOfParameters();
- AddListOfParameters("J0_D1",1.4);
- AddListOfParameters("J0_Km1",0.01);
- AddListOfParameters("J1_D2",0.9);
- AddListOfParameters("J1_Km2",0.01);
- AddListOfParameters("J2_D2",0.9);
- AddListOfParameters("J2_Km2",0.01);
- AddListOfParameters("J3_D1",1.4);
- AddListOfParameters("J3_Input",0.5);
- AddListOfParameters("J3_Km1",0.01);
- AddListOfParameters("J4_Km1",0.01);
- AddListOfParameters("J5_D2",0.9);
- AddListOfParameters("J5_Km2",0.01);
- AddListOfParameters("J4_D1",1.4);
- EndAddListOfParameters();
- StartReactionContent();
- AddListOfReaction("J0","E4","E3","J0_D1*E2*E4/(J0_Km1+E4)");
- AddListOfReaction("J1","E1","E2","J1_D2*E1/(J1_Km2+E1)");
- AddListOfReaction("J2","E5","E6","J2_D2*E5/(J2_Km2+E5)");
- AddListOfReaction("J3","E2","E1","J3_D1*(J3_Input+E6)*E2/(J3_Km1+E2)");
- AddListOfReaction("J4","E6","E5","J4_D1*E4*E6/(J4_Km1+E6)");
- AddListOfReaction("J5","E3","E4","J5_D2*E3/(J5_Km2+E3)");
- EndReactionContent();
- EndXMLContent();
- }
- void CMarkupDlg::AddListOfReaction(CString pReactionName,CString pNameOfReactants,CString pNameOfProducts,CString pWayOfCalc)
- {
- // CString str;
- // str.Format("%s %s %c",numLeft,numRight,pWayOfCalc.GetAt(i));
- // AfxMessageBox(str);
- CString numLeft,numRight;
- for(int i=0;i<pWayOfCalc.GetLength();i++)
- {
- if(pWayOfCalc.GetAt(i)=='+' || pWayOfCalc.GetAt(i)=='-' || pWayOfCalc.GetAt(i)=='*' || pWayOfCalc.GetAt(i)=='/')
- {
- numLeft=pWayOfCalc.Left(i);
- numRight=pWayOfCalc.Right(pWayOfCalc.GetLength()-i-1);
- // num1=atof(numLeft.GetBuffer(numLeft.GetLength()));
- // num2=atof(numRight.GetBuffer(numRight.GetLength()));
- break;
- }
- }
- MCD_STR strNumLeft=numLeft;
- MCD_STR strNumRight=numRight;
- MCD_STR strReactionName = pReactionName;
- MCD_STR strNameOfReactants=pNameOfReactants;
- MCD_STR strNameOfProducts=pNameOfProducts;
- xml.AddElem("reaction");
- xml.SetAttrib("id",strReactionName);
- xml.SetAttrib("reversible","false");
- xml.IntoElem();//into reaction
- xml.AddElem("listOfReactants");
- xml.IntoElem();//into listOfReactants
- xml.AddElem("speciesReference");
- xml.SetAttrib("species",strNameOfReactants);
- xml.OutOfElem();//listOfReactants
- xml.AddElem("listOfProducts");
- xml.IntoElem();//into listOfProducts
- xml.AddElem("speciesReference");
- xml.SetAttrib("species",strNameOfProducts);
- xml.OutOfElem();//speciesReference
- xml.AddElem("kineticLaw");
- xml.IntoElem();////into kineticLaw
- xml.AddElem("math");
- xml.IntoElem();//into math
- xml.AddElem("apply");
- xml.IntoElem();//into apply
- switch (pWayOfCalc.GetAt(i))
- {
- case '+':
- xml.AddElem("plus");
- break;
- case '-':
- xml.AddElem("minus");
- break;
- case '*':
- xml.AddElem("times");
- break;
- case '/':
- xml.AddElem("divide");
- break;
- default:
- return;
- }
- xml.AddElem("ci",strNumLeft);
- xml.AddElem("ci",strNumRight);
- xml.OutOfElem();//apply
- xml.OutOfElem();//math
- xml.OutOfElem();//kineticLaw
- xml.OutOfElem();//reaction
- }
- void CMarkupDlg::AddListOfSpecies(CString pID,double quantity)
- {
- CString str;
- str.Format("%f",quantity);
- MCD_STR strQuantity=str;
- MCD_STR strID=pID;
- xml.AddElem("species");
- xml.SetAttrib("id",strID);
- xml.SetAttrib("initialConcentration",strQuantity);
- xml.SetAttrib("compartment","compartment");
- }
- void CMarkupDlg::AddListOfParameters(CString pID,double quantity)
- {
- CString str;
- str.Format("%f",quantity);
- MCD_STR strQuantity=str;
- MCD_STR strID=pID;
- xml.AddElem("parameter");
- xml.SetAttrib("id",strID);
- xml.SetAttrib("value",strQuantity);
- }
- void CMarkupDlg::StartAddListOfSpecies()
- {
- xml.AddElem("listOfCompartments");
- xml.IntoElem();//into listOfCompartments
- xml.AddElem("compartment");
- xml.SetAttrib("id","compartment");
- xml.OutOfElem();//listOfCompartments
- xml.AddElem("listOfSpecies");
- xml.IntoElem();//into listOfSpecies
- }
- void CMarkupDlg::EndAddListOfSpecies()
- {
- xml.OutOfElem();//listOfSpecies
- }
- void CMarkupDlg::StartAddListOfParameters()
- {
- xml.AddElem("listOfParameters");
- xml.IntoElem();//into listOfParameters
- }
- void CMarkupDlg::EndAddListOfParameters()
- {
- xml.OutOfElem();//listOfParameters
- }
- void CMarkupDlg::StartReactionContent()
- {
- xml.AddElem("listOfReactions");
- xml.IntoElem();
- }
- void CMarkupDlg::EndReactionContent()
- {
- xml.OutOfElem();//listOfReactions
- }
- void CMarkupDlg::StartXMLContent()
- {
- xml.ResetPos();
- xml.SetDoc("<?xml version="1.0" encoding="UTF-8"?>");
- xml.AddElem("sbml");
- xml.SetAttrib("level",2);
- xml.SetAttrib("version",1);
- xml.SetAttrib("xmlns:jd2","http://www.sys-bio.org/sbml/jd2");
- xml.IntoElem();//into sbml
- xml.AddElem("model");
- xml.SetAttrib("id","LinearChain");
- xml.SetAttrib("name","Simple Linear Pathway");
- xml.IntoElem();//into model
- xml.AddElem("annotation");
- // xml.IntoElem();//into annotation
- // xml.OutOfElem();//annotation
- }
- void CMarkupDlg::EndXMLContent()
- {
- xml.OutOfElem();//model
- xml.OutOfElem();//sbml
- CString str;
- GetDlgItem( IDC_EDIT_FILE )->GetWindowText(str);
- xml.Save((LPCTSTR)str);
- SetTimer(1,500,NULL);
- }
- // bool Load( MCD_CSTR szFileName );
- // bool SetDoc( MCD_PCSZ szDoc );
- // bool SetDoc( const MCD_STR& strDoc );
- // bool IsWellFormed();
- // bool FindElem( MCD_CSTR szName=NULL );
- // bool FindChildElem( MCD_CSTR szName=NULL );
- // bool IntoElem();
- // bool OutOfElem();
- // void ResetChildPos() { x_SetPos(m_iPosParent,m_iPos,0); };
- // void ResetMainPos() { x_SetPos(m_iPosParent,0,0); };
- // void ResetPos() { x_SetPos(0,0,0); };
- // MCD_STR GetTagName() const;
- // MCD_STR GetChildTagName() const { return x_GetTagName(m_iPosChild); };
- // MCD_STR GetData() const { return x_GetData(m_iPos); };
- // MCD_STR GetChildData() const { return x_GetData(m_iPosChild); };
- // MCD_STR GetElemContent() const { return x_GetElemContent(m_iPos); };
- // MCD_STR GetAttrib( MCD_CSTR szAttrib ) const { return x_GetAttrib(m_iPos,szAttrib); };
- // MCD_STR GetChildAttrib( MCD_CSTR szAttrib ) const { return x_GetAttrib(m_iPosChild,szAttrib); };
- // MCD_STR GetAttribName( int n ) const;
- // int FindNode( int nType=0 );
- // int GetNodeType() { return m_nNodeType; };
- // bool SavePos( MCD_CSTR szPosName=_T("") );
- // bool RestorePos( MCD_CSTR szPosName=_T("") );
- // const MCD_STR& GetError() const { return m_strError; };
- // int GetDocFlags() const { return m_nFlags; };
- // void SetDocFlags( int nFlags ) { m_nFlags = nFlags; };
- //
- // // Create
- // bool Save( MCD_CSTR szFileName );
- // const MCD_STR& GetDoc() const { return m_strDoc; };
- // bool AddElem( MCD_CSTR szName, MCD_CSTR szData=NULL, int nFlags=0 ) { return x_AddElem(szName,szData,nFlags); };
- // bool InsertElem( MCD_CSTR szName, MCD_CSTR szData=NULL, int nFlags=0 ) { return x_AddElem(szName,szData,nFlags|MNF_INSERT); };
- // bool AddChildElem( MCD_CSTR szName, MCD_CSTR szData=NULL, int nFlags=0 ) { return x_AddElem(szName,szData,nFlags|MNF_CHILD); };
- // bool InsertChildElem( MCD_CSTR szName, MCD_CSTR szData=NULL, int nFlags=0 ) { return x_AddElem(szName,szData,nFlags|MNF_INSERT|MNF_CHILD); };
- // bool AddElem( MCD_CSTR szName, int nValue, int nFlags=0 ) { return x_AddElem(szName,nValue,nFlags); };
- // bool InsertElem( MCD_CSTR szName, int nValue, int nFlags=0 ) { return x_AddElem(szName,nValue,nFlags|MNF_INSERT); };
- // bool AddChildElem( MCD_CSTR szName, int nValue, int nFlags=0 ) { return x_AddElem(szName,nValue,nFlags|MNF_CHILD); };
- // bool InsertChildElem( MCD_CSTR szName, int nValue, int nFlags=0 ) { return x_AddElem(szName,nValue,nFlags|MNF_INSERT|MNF_CHILD); };
- // bool AddAttrib( MCD_CSTR szAttrib, MCD_CSTR szValue ) { return x_SetAttrib(m_iPos,szAttrib,szValue); };
- // bool AddChildAttrib( MCD_CSTR szAttrib, MCD_CSTR szValue ) { return x_SetAttrib(m_iPosChild,szAttrib,szValue); };
- // bool AddAttrib( MCD_CSTR szAttrib, int nValue ) { return x_SetAttrib(m_iPos,szAttrib,nValue); };
- // bool AddChildAttrib( MCD_CSTR szAttrib, int nValue ) { return x_SetAttrib(m_iPosChild,szAttrib,nValue); };
- // bool AddSubDoc( MCD_CSTR szSubDoc ) { return x_AddSubDoc(szSubDoc,0); };
- // bool InsertSubDoc( MCD_CSTR szSubDoc ) { return x_AddSubDoc(szSubDoc,MNF_INSERT); };
- // MCD_STR GetSubDoc() const { return x_GetSubDoc(m_iPos); };
- // bool AddChildSubDoc( MCD_CSTR szSubDoc ) { return x_AddSubDoc(szSubDoc,MNF_CHILD); };
- // bool InsertChildSubDoc( MCD_CSTR szSubDoc ) { return x_AddSubDoc(szSubDoc,MNF_CHILD|MNF_INSERT); };
- // MCD_STR GetChildSubDoc() const { return x_GetSubDoc(m_iPosChild); };
- // bool AddNode( int nType, MCD_CSTR szText ) { return x_AddNode(nType,szText,0); };
- // bool InsertNode( int nType, MCD_CSTR szText ) { return x_AddNode(nType,szText,MNF_INSERT); };
- //
- // // Modify
- // bool RemoveElem();
- // bool RemoveChildElem();
- // bool RemoveNode();
- // bool SetAttrib( MCD_CSTR szAttrib, MCD_CSTR szValue ) { return x_SetAttrib(m_iPos,szAttrib,szValue); };
- // bool SetChildAttrib( MCD_CSTR szAttrib, MCD_CSTR szValue ) { return x_SetAttrib(m_iPosChild,szAttrib,szValue); };
- // bool SetAttrib( MCD_CSTR szAttrib, int nValue ) { return x_SetAttrib(m_iPos,szAttrib,nValue); };
- // bool SetChildAttrib( MCD_CSTR szAttrib, int nValue ) { return x_SetAttrib(m_iPosChild,szAttrib,nValue); };
- // bool SetData( MCD_CSTR szData, int nFlags=0 ) { return x_SetData(m_iPos,szData,nFlags); };
- // bool SetChildData( MCD_CSTR szData, int nFlags=0 ) { return x_SetData(m_iPosChild,szData,nFlags); };
- // bool SetData( int nValue ) { return x_SetData(m_iPos,nValue); };
- // bool SetChildData( int nValue ) { return x_SetData(m_iPosChild,nValue); };
- // bool SetElemContent( MCD_CSTR szContent ) { return x_SetElemContent(szContent); };
- //
- // // Utility
- // static bool ReadTextFile( MCD_CSTR szFileName, MCD_STR& strDoc, MCD_STR* pstrError=NULL, int* pnFlags=NULL );
- // static bool WriteTextFile( MCD_CSTR szFileName, MCD_STR& strDoc, MCD_STR* pstrError=NULL, int* pnFlags=NULL );
- // static MCD_STR EscapeText( MCD_CSTR szText, int nFlags = 0 );
- // static MCD_STR UnescapeText( MCD_CSTR szText, int nTextLength = -1 );
- // static int UTF16To8( char *pszUTF8, const wchar_t* pwszUTF16, int nUTF8Count );
- // static int DecodeCharUTF8( const char*& pszUTF8 );
- // static void EncodeCharUTF16( int nUChar, wchar_t* pwszUTF16, int& nWideLen );
- // static int UTF8To16( wchar_t* pwszUTF16, const char* pszUTF8, int nUTF8Count );
- // static int DecodeCharUTF16( const wchar_t*& pwszUTF16 );
- // static void EncodeCharUTF8( int nUChar, char* pszUTF8, int& nUTF8Len );
- // static MCD_STR UTF8ToA( MCD_CSTR pszUTF8, int* pnFailed = NULL );
- // static MCD_STR AToUTF8( MCD_CSTR pszANSI );
- // static MCD_STR GetDeclaredEncoding( MCD_CSTR szDoc );
- //
- //
- // SC* CMarkupDlg::Push(SC *s,char c)
- // {
- // SC *p=(SC*)malloc(sizeof(SC));
- // p->c=c;
- // p->next=s;
- // return p;
- // }
- // SF* CMarkupDlg::Push(SF *s,float f)
- // {
- // SF *p=(SF*)malloc(sizeof(SF));
- // p->f=f;
- // p->next=s;
- // return p;
- // }
- // SC* CMarkupDlg::Pop(SC *s)
- // {
- // SC *q=s;
- // s=s->next;
- // free(q);
- // return s;
- // }
- // SF* CMarkupDlg::Pop(SF *s)
- // {
- // SF *q=s;
- // s=s->next;
- // free(q);
- // return s;
- // }
- //
- //
- //
- // float CMarkupDlg::Operate(float a,unsigned char theta, float b)
- // {
- // switch(theta)
- // {
- // case '+': return a+b;
- // case '-': return a-b;
- // case '*': return a*b;
- // case '/': return a/b;
- // case '^': return pow(a,b);
- // default : return 0;
- // }
- // }
- // char OPSET[OPSETSIZE]={'+' , '-' , '*' , '/' ,'(' , ')' , '#','^'};
- //
- // Status CMarkupDlg::In(char Test,char* TestOp)
- // {
- // int Find=false;
- // for (int i=0; i< OPSETSIZE; i++)
- // {
- // if (Test == TestOp[i]) Find= true;
- // }
- // return Find;
- // }
- // CMarkupDlg::ReturnOpOrd(char op,char* TestOp)
- // {
- // for(int i=0; i< OPSETSIZE; i++)
- // if (op == TestOp[i]) return i;
- // }
- // char CMarkupDlg::precede(char Aop, char Bop)
- // {
- // return Prior[ReturnOpOrd(Aop,OPSET)][ReturnOpOrd(Bop,OPSET)];
- // }
- // void CMarkupDlg::EvaluateExpression(char* MyExpression)
- // { // 算法3.4
- // // 算术表达式求值的算符优先算法。
- // // 设OPTR和OPND分别为运算符栈和运算数栈,OP为运算符集合。
- // UINT count=0;
- // char ss[11111];
- // SC *OPTR=NULL; // 运算符栈,字符元素
- // vuPara.clear();
- // SC *OPOP=NULL;//opeartion
- // vPara.clear();
- // vPara2.clear();
- // OPOP=Push(OPOP,'#');
- // char TempData[20];
- // CString Data,a,b;
- // char theta,Dr[]="#";
- // OPTR=Push(OPTR,'#');
- // strcpy(ss,MyExpression);
- // strcat(ss,Dr);
- // //strcpy(c,ss);
- // strcpy(TempData,"");//字符串拷贝函数
- // char *p=ss;
- // while (*p!= '#' || OPTR->c!='#')
- // {
- // if (!In(*p, OPSET))
- // {
- // Dr[0]=*p;
- // strcat(TempData,Dr);//字符串连接函数
- // p=p+1;
- // if (In(*p, OPSET))
- // {
- // Data=_T(TempData);//字符串转换函数(double)
- // vuPara.push_back(Data);
- // strcpy(TempData,"");
- // }
- // }
- // else
- // { // 不是运算符则进栈
- // switch (precede(OPTR->c, *p))
- // {
- // case '<': // 栈顶元素优先权低
- // OPTR=Push(OPTR, *p);
- // p++;
- // break;
- // case '=': // 脱括号并接收下一字符
- // OPTR=Pop(OPTR);
- // p++;
- // break;
- // case '>': // 退栈并将运算结果入栈
- // theta=OPTR->c;OPTR=Pop(OPTR);
- //
- // iter=vuPara.end();
- // b=*(iter-1);
- // vuPara.pop_back();
- //
- // iter=vuPara.end();
- // a=*(iter-1);
- // vuPara.pop_back();
- // CString str;
- //
- // str.Format("#%d",count);
- // vuPara.push_back(str);
- // OPOP=Push(OPOP,theta);
- // vPara.push_back(a);
- // vPara2.push_back(b);
- // count++;
- // // sprintf(ss,"%f%c%f",a,theta,b);
- // // puts(ss);
- // // system("pause");
- // break;
- // } // switch
- // }
- // } // while
- //
- // // while(OPOP->c!='#')
- // // {
- // // theta=OPOP->c;OPOP=Pop(OPOP);
- // //
- // // iter=vPara.end();
- // // b=*(iter-1);
- // // vPara.pop_back();
- // // iter=vPara.end();
- // // a=*(iter-1);
- // // vPara.pop_back();
- // // str+=a;
- // // str+=_T(theta);
- // // str+=b;
- // // str+=_T("rn");
- // // }
- // CString str="";
- //
- // theta=OPOP->c;OPOP=Pop(OPOP);
- //
- // iter=vPara.end();
- // b=*(iter-1);
- //
- // iter=vPara2.end();
- // a=*(iter-1);
- // str+=a;
- // str+=_T(theta);
- // str+=b;
- //
- //
- // AfxMessageBox(str);
- // FormFormula(str);
- // xml.Save("D:\系统数据\My Documents\SBML Models\new.xml");
- // AfxMessageBox("生成完毕,请查看是否生成成功!");
- // return ;
- // } // EvaluateExpression
- //
- //
- // void CMarkupDlg::OnCalcTest()
- // {
- // // TODO: Add your control notification handler code here
- // // EvaluateExpression("1+2*3");
- // // visit("1+#2");
- // // xml.Save("D:\系统数据\My Documents\SBML Models\new.xml");
- // // AfxMessageBox("生成完毕,请查看是否生成成功!");
- //
- // }
- // void CMarkupDlg::visit(CString str)
- // {
- // CString numLeft,numRight;
- // int cutArea;
- // for(int i=0;i<str.GetLength();i++)
- // {
- // if(str.GetAt(i)=='+' || str.GetAt(i)=='-' || str.GetAt(i)=='*' || str.GetAt(i)=='/')
- // {
- //
- // numLeft=str.Left(i);
- // numRight=str.Right(str.GetLength()-i-1);
- // // num1=atof(numLeft.GetBuffer(numLeft.GetLength()));
- // // num2=atof(numRight.GetBuffer(numRight.GetLength()));
- // break;
- // }
- // }
- // cutArea=i;
- // MCD_STR strNumLeft=numLeft;
- // MCD_STR strNumRight=numRight;
- //
- // BOOLEAN returnValLeft=FALSE , returnValRight=FALSE;
- // for(i=0;i<numLeft.GetLength();i++)
- // {
- // if(numLeft.GetAt(i)=='#')
- // {
- // returnValLeft=TRUE;
- // break;
- // }
- // }
- // for(int j=0;j<numRight.GetLength();j++)
- // {
- // if(numRight.GetAt(j)=='#')
- // {
- // returnValRight=TRUE;
- // break;
- // }
- // }
- // if(returnValLeft==TRUE)
- // {
- // xml.AddElem("apply");
- // switch (str.GetAt(cutArea))
- // {
- // case '+':
- // xml.AddChildElem("plus");
- // break;
- // case '-':
- // xml.AddChildElem("minus");
- // break;
- // case '*':
- // xml.AddChildElem("times");
- // break;
- // case '/':
- // xml.AddChildElem("divide");
- // break;
- // default:
- // return;
- // }
- // return;
- //
- // }
- //
- // if(returnValRight==TRUE)
- // {
- // xml.AddElem("apply");
- //
- //
- // switch (str.GetAt(j))
- // {
- // case '+':
- // xml.AddChildElem("plus");
- // break;
- // case '-':
- // xml.AddChildElem("minus");
- // break;
- // case '*':
- // xml.AddChildElem("times");
- // break;
- // case '/':
- // xml.AddChildElem("divide");
- // break;
- // default:
- // return;
- // }
- // return;
- // }
- // if(returnValLeft==FALSE || returnValRight==FALSE)
- // {
- // xml.AddElem("apply");
- // xml.IntoElem();//into apply
- //
- // switch (str.GetAt(j))
- // {
- // case '+':
- // xml.AddElem("plus");
- // break;
- // case '-':
- // xml.AddElem("minus");
- // break;
- // case '*':
- // xml.AddElem("times");
- // break;
- // case '/':
- // xml.AddElem("divide");
- // break;
- // default:
- // return;
- // }
- //
- // xml.AddElem("ci",strNumLeft);
- // xml.AddElem("ci",strNumRight);
- // xml.OutOfElem();//apply
- // }
- //
- // }
- //
- // void CMarkupDlg::FormFormula(CString str)
- // {
- // CString numLeft,numRight;
- // int cutArea;
- // for(int i=0;i<str.GetLength();i++)
- // {
- // if(str.GetAt(i)=='+' || str.GetAt(i)=='-' || str.GetAt(i)=='*' || str.GetAt(i)=='/')
- // {
- //
- // numLeft=str.Left(i);
- // numRight=str.Right(str.GetLength()-i-1);
- // // num1=atof(numLeft.GetBuffer(numLeft.GetLength()));
- // // num2=atof(numRight.GetBuffer(numRight.GetLength()));
- // break;
- // }
- // }
- // cutArea=i;
- // if(cutArea==str.GetLength())
- // return;
- // MCD_STR strNumLeft=numLeft;
- // MCD_STR strNumRight=numRight;
- //
- // BOOLEAN returnValLeft=FALSE , returnValRight=FALSE;
- // for(i=0;i<numLeft.GetLength();i++)
- // {
- // if(numLeft.GetAt(i)=='#')
- // {
- // returnValLeft=TRUE;
- // break;
- // }
- // }
- // for(int j=0;j<numRight.GetLength();j++)
- // {
- // if(numRight.GetAt(j)=='#')
- // {
- // returnValRight=TRUE;
- // break;
- // }
- // }
- // if(!(returnValLeft==FALSE && returnValRight==FALSE)) //递归调用的终止条件。如果是空树,则不执行任何操作
- // {
- // visit(str); //访问树根。注意T既表示树,也表示树根。
- // iter=vPara.begin();
- // if(returnValLeft==TRUE)
- // numLeft=*(iter+atoi(numLeft.Right(numLeft.GetLength()-1)));
- // iter=vPara2.begin();
- // if(returnValRight==TRUE)
- // numRight=*(iter+atoi(numRight.Right(numRight.GetLength()-1)));
- //
- // AfxMessageBox(numLeft);
- // AfxMessageBox(numRight);
- // FormFormula(numLeft); //先序遍历左子树
- // FormFormula(numRight); //先序遍历右子树
- // }
- // else
- // visit(str);
- // }
- void CMarkupDlg::OnCompile()
- {
- // TODO: Add your control notification handler code here
- GetDlgItem(IDC_ST_PARSE_RESULTS)->SetWindowText("Compiling Scamp files...");
- CompileUse.clear();
- int StartPos;
- int EndPos;
- CompileUse.clear();
- CompileUse2.clear();
- CompileUse3.clear();
- CompileUse4.clear();
- CompileUse5.clear();
- CompileUse6.clear();
- UpdateData();
- if(m_cmdEdit=="")
- {
- GetDlgItem(IDC_ST_PARSE_RESULTS)->SetWindowText("Error:Have not found any code here in the code area...");
- return;
- }
- m_cmdEdit.Remove(' ');
- //Species
- StartPos=m_cmdEdit.Find("Dec");
- if(StartPos==-1)
- {
- GetDlgItem(IDC_ST_PARSE_RESULTS)->SetWindowText("Error:Cannot find any node declarations. Not a standard Scamp file...");
- return;
- }
- int junction=StartPos+3;
- CString str="";
- char *pBuf;
- for(int i=StartPos+3;m_cmdEdit.GetAt(i-1)!=';';i++)
- {
- if (m_cmdEdit.GetAt(i+1)==',' || m_cmdEdit.GetAt(i+1)==';')
- {
- pBuf=new char[i-junction+3];
- pBuf[i-junction+1]=0;
- for(int j=junction;j<=i;j++)
- pBuf[j-junction]=m_cmdEdit[j];
- CompileUse.push_back(pBuf);
- junction=i+2;
- i++;
- delete pBuf;
- }
- }
- //Reactions
- StartPos=m_cmdEdit.Find("Reactions");
- CString strTemp;
- strTemp=m_cmdEdit.Right(m_cmdEdit.GetLength()-StartPos-11);
- EndPos=strTemp.Find("end;");
- CString reTemp;
- reTemp=strTemp.Left(EndPos);
- // AfxMessageBox(reTemp);
- // int atemp;
- // atemp=reTemp.Find("[");
- // CString sdd;
- // sdd.Format("%d",atemp);
- // AfxMessageBox(sdd);
- // static int startSite,endSite;
- // for(i=0;i<reTemp.GetLength();i++)
- // {
- // if(reTemp.GetAt(i)=='[')
- // startSite=i;
- // if(reTemp.GetAt(i)==']')
- // {
- // endSite=i;
- // CString str;
- // str.Mid(startSite,endSite-startSite);
- // AfxMessageBox(str);
- // }
- // }
- reTemp.Remove('[');
- reTemp.Replace(']',' ');
- reTemp.Replace("->"," ");
- reTemp.Replace(';',' ');
- reTemp.Remove('$');
- // AfxMessageBox(reTemp);
- CFile wrrfile("reactions.tmp",CFile::modeCreate|CFile::modeWrite);
- wrrfile.Write(reTemp,strlen(reTemp));
- wrrfile.Close();
- FILE *ff;
- if((ff=fopen("reactions.tmp","r"))==NULL)
- {
- AfxMessageBox("cuowu");
- return;
- }
- else
- {
- while(!feof(ff))
- {
- char ctemp[4][111];
- fscanf(ff,"%s %s %s %s n",ctemp[0],ctemp[1],ctemp[2],ctemp[3]);
- CompileUse3.push_back(ctemp[0]);
- CompileUse4.push_back(ctemp[1]);
- CompileUse5.push_back(ctemp[2]);
- CompileUse6.push_back(ctemp[3]);
- }
- fclose(ff);
- }
- //Initialize Conditions
- float paraArray[111];
- StartPos=m_cmdEdit.Find("Initialise");
- if(StartPos==-1)
- {
- GetDlgItem(IDC_ST_PARSE_RESULTS)->SetWindowText("Error:Cannot find the keyword - Initialise . Not a standard Scamp file...");
- return;
- }
- str=m_cmdEdit.Right(m_cmdEdit.GetLength()-StartPos-12);
- EndPos=str.Find("end;");
- if(EndPos==-1)
- {
- GetDlgItem(IDC_ST_PARSE_RESULTS)->SetWindowText("Error:Cannot find the end flag. Not a standard Scamp file...");
- return;
- }
- CString temp;
- temp=str.Left(EndPos);
- temp.Replace('=',' ');
- temp.Replace(';',' ');
- CFile wrfile("conditions.tmp",CFile::modeCreate|CFile::modeWrite);
- wrfile.Write(temp,strlen(temp));
- wrfile.Close();
- FILE *fp;
- if((fp=fopen("conditions.tmp","r"))==NULL)
- {
- AfxMessageBox("cuowu");
- return;
- }
- else
- {
- int arrayCount=0;
- while(!feof(fp))
- {
- char ctemp[111];
- fscanf(fp,"%s %f n",ctemp,¶Array[arrayCount++]);
- CompileUse2.push_back(ctemp);
- }
- fclose(fp);
- }
- //开始搞
- StartXMLContent();
- StartAddListOfSpecies();
- for(int sd=0;sd<CompileUse.size();sd++)
- {
- AddListOfSpecies(CompileUse2.at(sd),paraArray[sd]);
- }
- EndAddListOfSpecies();
- StartAddListOfParameters();
- for(sd=CompileUse.size();sd<CompileUse2.size();sd++)
- {
- AddListOfParameters(CompileUse2.at(sd),paraArray[sd]);
- }
- EndAddListOfParameters();
- StartReactionContent();
- for(sd=0;sd<CompileUse3.size();sd++)
- {
- AddListOfReaction(CompileUse3.at(sd),CompileUse4.at(sd),CompileUse5.at(sd),CompileUse6.at(sd));
- }
- EndReactionContent();
- EndXMLContent();
- //CString str;
- // str.Format("%c",m_cmdEdit.GetAt(StartPos));
- // AfxMessageBox(str);
- }
- //
- // StartXMLContent();//必须要先设定parameters,才能设定Reactions
- // StartAddListOfSpecies();
- // AddListOfSpecies("E1",0.5);
- // AddListOfSpecies("E2",0.5);
- // AddListOfSpecies("E3",0.5);
- // AddListOfSpecies("E4",0.5);
- // AddListOfSpecies("E5",0.5);
- // EndAddListOfSpecies();
- //
- // StartAddListOfParameters();
- // AddListOfParameters("J0_D1",1.4);
- // AddListOfParameters("J0_Km1",0.01);
- // AddListOfParameters("J1_D2",0.9);
- // AddListOfParameters("J1_Km2",0.01);
- // AddListOfParameters("J2_D2",0.9);
- // AddListOfParameters("J2_Km2",0.01);
- // AddListOfParameters("J3_D1",1.4);
- // AddListOfParameters("J3_Input",0.5);
- // AddListOfParameters("J3_Km1",0.01);
- // AddListOfParameters("J4_Km1",0.01);
- // AddListOfParameters("J5_D2",0.9);
- // AddListOfParameters("J5_Km2",0.01);
- // AddListOfParameters("J4_D1",1.4);
- // EndAddListOfParameters();
- //
- // StartReactionContent();
- // AddListOfReaction("J0","E4","E3","J0_D1*E2*E4/(J0_Km1+E4)");
- // AddListOfReaction("J1","E1","E2","J1_D2*E1/(J1_Km2+E1)");
- // AddListOfReaction("J2","E5","E6","J2_D2*E5/(J2_Km2+E5)");
- // AddListOfReaction("J3","E2","E1","J3_D1*(J3_Input+E6)*E2/(J3_Km1+E2)");
- // AddListOfReaction("J4","E6","E5","J4_D1*E4*E6/(J4_Km1+E6)");
- // AddListOfReaction("J5","E3","E4","J5_D2*E3/(J5_Km2+E3)");
- // EndReactionContent();
- //
- // EndXMLContent();
- void CMarkupDlg::OnClear()
- {
- // TODO: Add your control notification handler code here
- m_cmdEdit="";
- UpdateData(FALSE);
- }
- void CMarkupDlg::OnOpenExists()
- {
- // TODO: Add your control notification handler code here
- CString pDocRoute;
- CFileDialog fileDlg(TRUE);
- fileDlg.m_ofn.lpstrTitle="Choose the cmd file you want to load...";
- fileDlg.m_ofn.lpstrFilter="Scamp Files(*.cmd)*.cmd";
- if(IDOK==fileDlg.DoModal())
- {
- pDocRoute=fileDlg.GetPathName();
- }
- CFile file(pDocRoute,CFile::modeRead);
- char *pBuf;
- DWORD dwFileLen;
- dwFileLen=file.GetLength();
- pBuf=new char[dwFileLen+1];
- pBuf[dwFileLen]=0;
- file.Read(pBuf,dwFileLen);
- file.Close();
- m_cmdEdit=pBuf;
- UpdateData(FALSE);
- }
- void CMarkupDlg::OnSaveAs()
- {
- // TODO: Add your control notification handler code here
- UpdateData();
- CFileDialog ldFile(false,NULL,NULL,OFN_OVERWRITEPROMPT,"*.cmd"); //false类型对话框为保存文件对话框
- ldFile.m_ofn.lpstrTitle="Choose the path you want to save...";
- ldFile.m_ofn.lpstrFilter="Scamp files(*.cmd)*.cmdAll Files(*.*)*.*";
- if(IDOK==ldFile.DoModal())
- {
- CFile wrfile(ldFile.GetPathName(),CFile::modeCreate|CFile::modeWrite);
- wrfile.Write(m_cmdEdit,strlen(m_cmdEdit));
- wrfile.Close();
- AfxMessageBox("保存成功!");
- }
- }
- void CMarkupDlg::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- if(nIDEvent==1)
- {
- KillTimer(1);
- GetDlgItem(IDC_ST_PARSE_RESULTS)->SetWindowText("Compilation has successfully Finished!!!!");
- }
- CDialog::OnTimer(nIDEvent);
- }