XmlManager.h 3.0 KB

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