PageRenderTime 41ms CodeModel.GetById 14ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 20// XmlDocument.h: interface for the CXmlDocument class.
 21//
 22//////////////////////////////////////////////////////////////////////
 23
 24#if !defined(AFX_XMLDOCUMENT_H__D68461F7_E0CE_4FA0_B1C9_0541610164E9__INCLUDED_)
 25#define AFX_XMLDOCUMENT_H__D68461F7_E0CE_4FA0_B1C9_0541610164E9__INCLUDED_
 26
 27#if _MSC_VER > 1000
 28#pragma once
 29#endif // _MSC_VER > 1000
 30
 31#include <d3d9.h>
 32
 33#include <stdio.h>
 34
 35#define XML_ROOT_NODE 0
 36#define XML_MAX_TAGNAME_SIZE	32
 37#define XML_MAX_INNERTEXT_SIZE	1024
 38
 39typedef int    XmlNode;
 40typedef void (*XmlNodeCallback) (char* szTag, XmlNode node);      
 41
 42
 43
 44class CXmlDocument  
 45{
 46public:
 47	CXmlDocument();
 48	virtual ~CXmlDocument();
 49
 50	void	Create(char* szString);
 51	int     Load(char* szFile);
 52	void    Close();
 53
 54	int		GetNodeCount(char* tag);
 55
 56	void    EnumerateNodes(char* szTag, XmlNodeCallback pFunc);
 57
 58	XmlNode GetChildNode(XmlNode node, char* szTag);
 59	XmlNode GetNextNode(XmlNode node);
 60	char*   GetNodeText(XmlNode node);
 61	char*   GetNodeTag(XmlNode node);
 62
 63private:
 64
 65	char*	m_doc;
 66	int		m_size;
 67	int		m_nodes;
 68	char	m_szTag[XML_MAX_TAGNAME_SIZE];
 69	char	m_szText[XML_MAX_INNERTEXT_SIZE];
 70};
 71
 72class WriteXML
 73{
 74public:
 75  WriteXML() { m_file = NULL; m_rootTag = NULL; };
 76  ~WriteXML() { Close(); };
 77
 78  bool Open(const char *szFile, const char *szOpeningTag)
 79  {
 80    remove(szFile);
 81    if (!szFile || !szOpeningTag) return false;
 82    m_file = fopen(szFile, "w");
 83    if (!m_file) return false;
 84    m_rootTag = new char[strlen(szOpeningTag) + 1];
 85    strcpy(m_rootTag, szOpeningTag);
 86    fprintf(m_file, "<%s>\n", m_rootTag);
 87    return true;
 88  };
 89  void Close()
 90  {
 91    if (m_file)
 92    {
 93      if (m_rootTag)
 94        fprintf(m_file, "</%s>\n", m_rootTag);
 95      fclose(m_file);
 96    }
 97    delete[] m_rootTag;
 98    m_rootTag = NULL;
 99    m_file = NULL;
100  };
101  void WriteTag(const char *szTag, const char *data)
102  {
103    if (!m_file || !szTag || !data) return;
104    fprintf(m_file, "\t<%s>%s</%s>\n", szTag, data, szTag);
105  };
106  void WriteTag(const char *szTag, int data, const char *format = "%i")
107  {
108    char temp[10];
109    sprintf(temp, format, data);
110    WriteTag(szTag, temp);
111  };
112  void WriteTag(const char *szTag, float data)
113  {
114    if (!m_file || !szTag) return;
115    fprintf(m_file, "\t<%s>%f</%s>\n", szTag, data, szTag);
116  };
117  void WriteTag(const char *szTag, bool data)
118  {
119    if (!m_file || !szTag) return;
120    fprintf(m_file, "\t<%s>%s</%s>\n", szTag, data ? "true" : "false", szTag);
121  };
122
123private:
124  char *m_rootTag;
125  FILE *m_file;
126};
127
128#endif // !defined(AFX_XMLDOCUMENT_H__D68461F7_E0CE_4FA0_B1C9_0541610164E9__INCLUDED_)