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