home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 217 / DPCS0306DVD.ISO / Toolkit / Internet / FileZilla / Server / FileZilla_Server-0.9.11.exe / source / interface / misc / MarkupSTL.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-12-23  |  7.5 KB  |  203 lines

  1. // Markup.h: interface for the CMarkupSTL class.
  2. //
  3. // Markup Release 6.3
  4. // Copyright (C) 1999-2002 First Objective Software, Inc. All rights reserved
  5. // Go to www.firstobject.com for the latest CMarkupSTL and EDOM documentation
  6. // Use in commercial applications requires written permission
  7. // This software is provided "as is", with no warranty.
  8.  
  9. #if !defined(AFX_MARKUP_H__948A2705_9E68_11D2_A0BF_00105A27C570__INCLUDED_)
  10. #define AFX_MARKUP_H__948A2705_9E68_11D2_A0BF_00105A27C570__INCLUDED_
  11.  
  12. #if _MSC_VER > 1000
  13. #pragma once
  14. #endif // _MSC_VER > 1000
  15.  
  16. #ifdef _DEBUG
  17. #define _DS(i) (i?&((LPCTSTR)m_csDoc)[m_aPos[i].nStartL]:0)
  18. #define MARKUP_SETDEBUGSTATE m_pMainDS=_DS(m_iPos); m_pChildDS=_DS(m_iPosChild)
  19. #else
  20. #define MARKUP_SETDEBUGSTATE
  21. #endif
  22.  
  23. class CMarkupSTL  
  24. {
  25. public:
  26.     CMarkupSTL() { SetDoc( NULL ); };
  27.     CMarkupSTL( LPCTSTR szDoc ) { SetDoc( szDoc ); };
  28.     CMarkupSTL( const CMarkupSTL& markup ) { *this = markup; };
  29.     void operator=( const CMarkupSTL& markup );
  30.     virtual ~CMarkupSTL() {};
  31.  
  32.     // Navigate
  33.     bool Load( LPCTSTR szFileName );
  34.     bool SetDoc( LPCTSTR szDoc );
  35.     bool IsWellFormed();
  36.     bool FindElem( LPCTSTR szName=NULL );
  37.     bool FindChildElem( LPCTSTR szName=NULL );
  38.     bool IntoElem();
  39.     bool OutOfElem();
  40.     void ResetChildPos() { x_SetPos(m_iPosParent,m_iPos,0); };
  41.     void ResetMainPos() { x_SetPos(m_iPosParent,0,0); };
  42.     void ResetPos() { x_SetPos(0,0,0); };
  43.     CString GetTagName() const;
  44.     CString GetChildTagName() const { return x_GetTagName(m_iPosChild); };
  45.     CString GetData() const { return x_GetData(m_iPos); };
  46.     CString GetChildData() const { return x_GetData(m_iPosChild); };
  47.     CString GetAttrib( LPCTSTR szAttrib ) const { return x_GetAttrib(m_iPos,szAttrib); };
  48.     CString GetChildAttrib( LPCTSTR szAttrib ) const { return x_GetAttrib(m_iPosChild,szAttrib); };
  49.     CString GetAttribName( int n ) const;
  50.     bool SavePos( LPCTSTR szPosName=_T("") );
  51.     bool RestorePos( LPCTSTR szPosName=_T("") );
  52.     bool GetOffsets( int& nStart, int& nEnd ) const;
  53.     CString GetError() const { return m_csError; };
  54.  
  55.     enum MarkupNodeType
  56.     {
  57.         MNT_ELEMENT                    = 1,  // 0x01
  58.         MNT_TEXT                    = 2,  // 0x02
  59.         MNT_WHITESPACE                = 4,  // 0x04
  60.         MNT_CDATA_SECTION            = 8,  // 0x08
  61.         MNT_PROCESSING_INSTRUCTION    = 16, // 0x10
  62.         MNT_COMMENT                    = 32, // 0x20
  63.         MNT_DOCUMENT_TYPE            = 64, // 0x40
  64.         MNT_EXCLUDE_WHITESPACE        = 123,// 0x7b
  65.     };
  66.  
  67.     // Create
  68.     bool Save( LPCTSTR szFileName );
  69.     CString GetDoc() const { return m_csDoc; };
  70.     bool AddElem( LPCTSTR szName, LPCTSTR szData=NULL ) { return x_AddElem(szName,szData,false,false); };
  71.     bool InsertElem( LPCTSTR szName, LPCTSTR szData=NULL ) { return x_AddElem(szName,szData,true,false); };
  72.     bool AddChildElem( LPCTSTR szName, LPCTSTR szData=NULL ) { return x_AddElem(szName,szData,false,true); };
  73.     bool InsertChildElem( LPCTSTR szName, LPCTSTR szData=NULL ) { return x_AddElem(szName,szData,true,true); };
  74.     bool AddAttrib( LPCTSTR szAttrib, LPCTSTR szValue ) { return x_SetAttrib(m_iPos,szAttrib,szValue); };
  75.     bool AddChildAttrib( LPCTSTR szAttrib, LPCTSTR szValue ) { return x_SetAttrib(m_iPosChild,szAttrib,szValue); };
  76.     bool AddAttrib( LPCTSTR szAttrib, int nValue ) { return x_SetAttrib(m_iPos,szAttrib,nValue); };
  77.     bool AddChildAttrib( LPCTSTR szAttrib, int nValue ) { return x_SetAttrib(m_iPosChild,szAttrib,nValue); };
  78.     bool AddChildAttrib( LPCTSTR szAttrib, __int64 nValue ) { return x_SetAttrib(m_iPosChild,szAttrib,nValue); };
  79.     bool AddChildSubDoc( LPCTSTR szSubDoc ) { return x_AddSubDoc(szSubDoc,false,true); };
  80.     bool InsertChildSubDoc( LPCTSTR szSubDoc ) { return x_AddSubDoc(szSubDoc,true,true); };
  81.     CString GetChildSubDoc() const;
  82.  
  83.     // Modify
  84.     bool RemoveElem();
  85.     bool RemoveChildElem();
  86.     bool SetAttrib( LPCTSTR szAttrib, LPCTSTR szValue ) { return x_SetAttrib(m_iPos,szAttrib,szValue); };
  87.     bool SetChildAttrib( LPCTSTR szAttrib, LPCTSTR szValue ) { return x_SetAttrib(m_iPosChild,szAttrib,szValue); };
  88.     bool SetAttrib( LPCTSTR szAttrib, int nValue ) { return x_SetAttrib(m_iPos,szAttrib,nValue); };
  89.     bool SetChildAttrib( LPCTSTR szAttrib, int nValue ) { return x_SetAttrib(m_iPosChild,szAttrib,nValue); };
  90.     bool SetData( LPCTSTR szData, int nCDATA=0 ) { return x_SetData(m_iPos,szData,nCDATA); };
  91.     bool SetChildData( LPCTSTR szData, int nCDATA=0 ) { return x_SetData(m_iPosChild,szData,nCDATA); };
  92.  
  93. protected:
  94.  
  95. #ifdef _DEBUG
  96.     LPCTSTR m_pMainDS;
  97.     LPCTSTR m_pChildDS;
  98. #endif
  99.  
  100.     CString m_csDoc;
  101.     CString m_csError;
  102.  
  103.     struct ElemPos
  104.     {
  105.         ElemPos() { Clear(); };
  106.         ElemPos( const ElemPos& pos ) { *this = pos; };
  107.         bool IsEmptyElement() const { return (nStartR == nEndL + 1); };
  108.         void Clear()
  109.         {
  110.             nStartL=0; nStartR=0; nEndL=0; nEndR=0; nReserved=0;
  111.             iElemParent=0; iElemChild=0; iElemNext=0;
  112.         };
  113.         void AdjustStart( int n ) { nStartL+=n; nStartR+=n; };
  114.         void AdjustEnd( int n ) { nEndL+=n; nEndR+=n; };
  115.         int nStartL;
  116.         int nStartR;
  117.         int nEndL;
  118.         int nEndR;
  119.         int nReserved;
  120.         int iElemParent;
  121.         int iElemChild;
  122.         int iElemNext;
  123.     };
  124.  
  125.     std::vector<ElemPos> m_aPos;
  126.     int m_iPosParent;
  127.     int m_iPos;
  128.     int m_iPosChild;
  129.     int m_iPosFree;
  130.     int m_nNodeType;
  131.  
  132.     struct TokenPos
  133.     {
  134.         TokenPos( LPCTSTR sz ) { Clear(); szDoc = sz; };
  135.         bool IsValid() const { return (nL <= nR); };
  136.         void Clear() { nL=0; nR=-1; nNext=0; bIsString=false; };
  137.         bool Match( LPCTSTR szName )
  138.         {
  139.             int nLen = nR - nL + 1;
  140.         // To ignore case, define MARKUP_IGNORECASE
  141.         #ifdef MARKUP_IGNORECASE
  142.             return ( (_tcsncicmp( &szDoc[nL], szName, nLen ) == 0)
  143.         #else
  144.             return ( (_tcsnccmp( &szDoc[nL], szName, nLen ) == 0)
  145.         #endif
  146.                 && ( szName[nLen] == _T('\0') || _tcschr(_T(" =/["),szName[nLen]) ) );
  147.         };
  148.         int nL;
  149.         int nR;
  150.         int nNext;
  151.         LPCTSTR szDoc;
  152.         bool bIsString;
  153.     };
  154.  
  155.     struct SavedPos
  156.     {
  157.         int iPosParent;
  158.         int iPos;
  159.         int iPosChild;
  160.     };
  161.     std::map<CString, SavedPos> m_mapSavedPos;
  162.  
  163.     void x_SetPos( int iPosParent, int iPos, int iPosChild )
  164.     {
  165.         m_iPosParent = iPosParent;
  166.         m_iPos = iPos;
  167.         m_iPosChild = iPosChild;
  168.         m_nNodeType = iPos?MNT_ELEMENT:0;
  169.         MARKUP_SETDEBUGSTATE;
  170.     };
  171.  
  172.     int x_GetFreePos();
  173.     int x_ReleasePos();
  174.  
  175.     int x_ParseElem( int iPos );
  176.     int x_ParseError( LPCTSTR szError, LPCTSTR szName = NULL );
  177.     static bool x_FindChar( LPCTSTR szDoc, int& nChar, _TCHAR c );
  178.     static bool x_FindToken( TokenPos& token );
  179.     CString x_GetToken( const TokenPos& token ) const;
  180.     int x_FindElem( int iPosParent, int iPos, LPCTSTR szPath );
  181.     CString x_GetTagName( int iPos ) const;
  182.     CString x_GetData( int iPos ) const;
  183.     CString x_GetAttrib( int iPos, LPCTSTR szAttrib ) const;
  184.     bool x_AddElem( LPCTSTR szName, LPCTSTR szValue, bool bInsert, bool bAddChild );
  185.     bool x_AddSubDoc( LPCTSTR szSubDoc, bool bInsert, bool bAddChild );
  186.     bool x_FindAttrib( TokenPos& token, LPCTSTR szAttrib=NULL ) const;
  187.     bool x_SetAttrib( int iPos, LPCTSTR szAttrib, LPCTSTR szValue );
  188.     bool x_SetAttrib( int iPos, LPCTSTR szAttrib, int nValue );
  189.     bool x_SetAttrib( int iPos, LPCTSTR szAttrib, __int64 nValue );
  190.     bool x_CreateNode( CString& csNode, int nNodeType, LPCTSTR szText );
  191.     void x_LocateNew( int iPosParent, int& iPosRel, int& nOffset, int nLength, int nFlags );
  192.     int x_ParseNode( TokenPos& token );
  193.     bool x_SetData( int iPos, LPCTSTR szData, int nCDATA );
  194.     int x_RemoveElem( int iPos );
  195.     void x_DocChange( int nLeft, int nReplace, const CString& csInsert );
  196.     void x_PosInsert( int iPos, int nInsertLength );
  197.     void x_Adjust( int iPos, int nShift, bool bAfterPos = false );
  198.     CString x_TextToDoc( LPCTSTR szText, bool bAttrib = false ) const;
  199.     CString x_TextFromDoc( int nLeft, int nRight ) const;
  200. };
  201.  
  202. #endif // !defined(AFX_MARKUP_H__948A2705_9E68_11D2_A0BF_00105A27C570__INCLUDED_)
  203.