/xbmc/visualizations/Vortex/VortexVis/Core/XmlDocument.h

http://github.com/xbmc/xbmc · C Header · 128 lines · 87 code · 20 blank · 21 comment · 12 complexity · a57b2a46006e08c7abb2e228d171d2ad MD5 · raw file

  1. /*
  2. * Copyright Š 2010-2013 Team XBMC
  3. * http://xbmc.org
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. // XmlDocument.h: interface for the CXmlDocument class.
  20. //
  21. //////////////////////////////////////////////////////////////////////
  22. #if !defined(AFX_XMLDOCUMENT_H__D68461F7_E0CE_4FA0_B1C9_0541610164E9__INCLUDED_)
  23. #define AFX_XMLDOCUMENT_H__D68461F7_E0CE_4FA0_B1C9_0541610164E9__INCLUDED_
  24. #if _MSC_VER > 1000
  25. #pragma once
  26. #endif // _MSC_VER > 1000
  27. #include <d3d9.h>
  28. #include <stdio.h>
  29. #define XML_ROOT_NODE 0
  30. #define XML_MAX_TAGNAME_SIZE 32
  31. #define XML_MAX_INNERTEXT_SIZE 1024
  32. typedef int XmlNode;
  33. typedef void (*XmlNodeCallback) (char* szTag, XmlNode node);
  34. class CXmlDocument
  35. {
  36. public:
  37. CXmlDocument();
  38. virtual ~CXmlDocument();
  39. void Create(char* szString);
  40. int Load(char* szFile);
  41. void Close();
  42. int GetNodeCount(char* tag);
  43. void EnumerateNodes(char* szTag, XmlNodeCallback pFunc);
  44. XmlNode GetChildNode(XmlNode node, char* szTag);
  45. XmlNode GetNextNode(XmlNode node);
  46. char* GetNodeText(XmlNode node);
  47. char* GetNodeTag(XmlNode node);
  48. private:
  49. char* m_doc;
  50. int m_size;
  51. int m_nodes;
  52. char m_szTag[XML_MAX_TAGNAME_SIZE];
  53. char m_szText[XML_MAX_INNERTEXT_SIZE];
  54. };
  55. class WriteXML
  56. {
  57. public:
  58. WriteXML() { m_file = NULL; m_rootTag = NULL; };
  59. ~WriteXML() { Close(); };
  60. bool Open(const char *szFile, const char *szOpeningTag)
  61. {
  62. remove(szFile);
  63. if (!szFile || !szOpeningTag) return false;
  64. m_file = fopen(szFile, "w");
  65. if (!m_file) return false;
  66. m_rootTag = new char[strlen(szOpeningTag) + 1];
  67. strcpy(m_rootTag, szOpeningTag);
  68. fprintf(m_file, "<%s>\n", m_rootTag);
  69. return true;
  70. };
  71. void Close()
  72. {
  73. if (m_file)
  74. {
  75. if (m_rootTag)
  76. fprintf(m_file, "</%s>\n", m_rootTag);
  77. fclose(m_file);
  78. }
  79. delete[] m_rootTag;
  80. m_rootTag = NULL;
  81. m_file = NULL;
  82. };
  83. void WriteTag(const char *szTag, const char *data)
  84. {
  85. if (!m_file || !szTag || !data) return;
  86. fprintf(m_file, "\t<%s>%s</%s>\n", szTag, data, szTag);
  87. };
  88. void WriteTag(const char *szTag, int data, const char *format = "%i")
  89. {
  90. char temp[10];
  91. sprintf(temp, format, data);
  92. WriteTag(szTag, temp);
  93. };
  94. void WriteTag(const char *szTag, float data)
  95. {
  96. if (!m_file || !szTag) return;
  97. fprintf(m_file, "\t<%s>%f</%s>\n", szTag, data, szTag);
  98. };
  99. void WriteTag(const char *szTag, bool data)
  100. {
  101. if (!m_file || !szTag) return;
  102. fprintf(m_file, "\t<%s>%s</%s>\n", szTag, data ? "true" : "false", szTag);
  103. };
  104. private:
  105. char *m_rootTag;
  106. FILE *m_file;
  107. };
  108. #endif // !defined(AFX_XMLDOCUMENT_H__D68461F7_E0CE_4FA0_B1C9_0541610164E9__INCLUDED_)