UIMarkup.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef __UIMARKUP_H__
  2. #define __UIMARKUP_H__
  3. #pragma once
  4. namespace DuiLib {
  5. enum
  6. {
  7. XMLFILE_ENCODING_UTF8 = 0,
  8. XMLFILE_ENCODING_UNICODE = 1,
  9. XMLFILE_ENCODING_ASNI = 2,
  10. };
  11. class CMarkup;
  12. class CMarkupNode;
  13. class UILIB_API CMarkup
  14. {
  15. friend class CMarkupNode;
  16. public:
  17. CMarkup(LPCTSTR pstrXML = NULL);
  18. ~CMarkup();
  19. bool Load(LPCTSTR pstrXML);
  20. bool LoadFromMem(BYTE* pByte, DWORD dwSize, int encoding = XMLFILE_ENCODING_UTF8);
  21. bool LoadFromFile(LPCTSTR pstrFilename, int encoding = XMLFILE_ENCODING_UTF8);
  22. void Release();
  23. bool IsValid() const;
  24. void SetPreserveWhitespace(bool bPreserve = true);
  25. void GetLastErrorMessage(LPTSTR pstrMessage, SIZE_T cchMax) const;
  26. void GetLastErrorLocation(LPTSTR pstrSource, SIZE_T cchMax) const;
  27. CMarkupNode GetRoot();
  28. private:
  29. typedef struct tagXMLELEMENT
  30. {
  31. ULONG iStart;
  32. ULONG iChild;
  33. ULONG iNext;
  34. ULONG iParent;
  35. ULONG iData;
  36. } XMLELEMENT;
  37. LPTSTR m_pstrXML;
  38. XMLELEMENT* m_pElements;
  39. ULONG m_nElements;
  40. ULONG m_nReservedElements;
  41. TCHAR m_szErrorMsg[100];
  42. TCHAR m_szErrorXML[50];
  43. bool m_bPreserveWhitespace;
  44. private:
  45. bool _Parse();
  46. bool _Parse(LPTSTR& pstrText, ULONG iParent);
  47. XMLELEMENT* _ReserveElement();
  48. inline void _SkipWhitespace(LPTSTR& pstr) const;
  49. inline void _SkipWhitespace(LPCTSTR& pstr) const;
  50. inline void _SkipIdentifier(LPTSTR& pstr) const;
  51. inline void _SkipIdentifier(LPCTSTR& pstr) const;
  52. bool _ParseData(LPTSTR& pstrText, LPTSTR& pstrData, char cEnd);
  53. void _ParseMetaChar(LPTSTR& pstrText, LPTSTR& pstrDest);
  54. bool _ParseAttributes(LPTSTR& pstrText);
  55. bool _Failed(LPCTSTR pstrError, LPCTSTR pstrLocation = NULL);
  56. };
  57. class UILIB_API CMarkupNode
  58. {
  59. friend class CMarkup;
  60. private:
  61. CMarkupNode();
  62. CMarkupNode(CMarkup* pOwner, int iPos);
  63. public:
  64. bool IsValid() const;
  65. CMarkupNode GetParent();
  66. CMarkupNode GetSibling();
  67. CMarkupNode GetChild();
  68. CMarkupNode GetChild(LPCTSTR pstrName);
  69. bool HasSiblings() const;
  70. bool HasChildren() const;
  71. LPCTSTR GetName() const;
  72. LPCTSTR GetValue() const;
  73. bool HasAttributes();
  74. bool HasAttribute(LPCTSTR pstrName);
  75. int GetAttributeCount();
  76. LPCTSTR GetAttributeName(int iIndex);
  77. LPCTSTR GetAttributeValue(int iIndex);
  78. LPCTSTR GetAttributeValue(LPCTSTR pstrName);
  79. bool GetAttributeValue(int iIndex, LPTSTR pstrValue, SIZE_T cchMax);
  80. bool GetAttributeValue(LPCTSTR pstrName, LPTSTR pstrValue, SIZE_T cchMax);
  81. private:
  82. void _MapAttributes();
  83. enum { MAX_XML_ATTRIBUTES = 64 };
  84. typedef struct
  85. {
  86. ULONG iName;
  87. ULONG iValue;
  88. } XMLATTRIBUTE;
  89. int m_iPos;
  90. int m_nAttributes;
  91. XMLATTRIBUTE m_aAttributes[MAX_XML_ATTRIBUTES];
  92. CMarkup* m_pOwner;
  93. };
  94. } // namespace DuiLib
  95. #endif // __UIMARKUP_H__