LCOV - code coverage report
Current view: top level - source/ps/XML - XMLWriter.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 4 4 100.0 %
Date: 2023-01-19 00:18:29 Functions: 6 18 33.3 %

          Line data    Source code
       1             : /* Copyright (C) 2021 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_XMLWRITER
      19             : #define INCLUDED_XMLWRITER
      20             : 
      21             : /*
      22             :  *
      23             :  *System for writing simple XML files, with human-readable formatting.
      24             :  *
      25             :  *Example usage:
      26             :  *
      27             :  *  XMLWriter_File exampleFile;
      28             :  *  {
      29             :  *      XMLWriter_Element scenarioTag (exampleFile,"Scenario");
      30             :  *      {
      31             :  *          XMLWriter_Element entitiesTag (exampleFile,"Entities");
      32             :  *          for (...)
      33             :  *          {
      34             :  *              XMLWriter_Element entityTag (exampleFile,"Entity");
      35             :  *              {
      36             :  *                  XMLWriter_Element templateTag (exampleFile,"Template");
      37             :  *                  templateTag.Text(entity.name);
      38             :  *              }
      39             :  *              // Or equivalently:
      40             :  *              templateTag.Setting("Template", entity.name);
      41             :  *              {
      42             :  *                  XMLWriter_Element positionTag (exampleFile,"Position");
      43             :  *                  positionTag.Attribute("x", entity.x);
      44             :  *                  positionTag.Attribute("y", entity.y);
      45             :  *                  positionTag.Attribute("z", entity.z);
      46             :  *              }
      47             :  *              {
      48             :  *                  XMLWriter_Element orientationTag (exampleFile,"Orientation");
      49             :  *                  orientationTag.Attribute("angle", entity.angle);
      50             :  *              }
      51             :  *          }
      52             :  *      }
      53             :  *  }
      54             :  *  exampleFile.StoreVFS(g_VFS, "/test.xml");
      55             :  *
      56             :  *  In general, "{ XML_Element(name); ... }" means "<name> ... </name>" -- the
      57             :  *  scoping braces are important to indicate where an element ends. If you don't put
      58             :  *  them the tag won't be closed until the object's destructor is called, usually
      59             :  *  when it goes out of scope.
      60             :  *  xml_element_.Attribute/xml_element_.Setting are templated. To support more types, alter the
      61             :  *  end of XMLWriter.cpp.
      62             :  */
      63             : 
      64             : #include "lib/file/vfs/vfs.h"
      65             : #include "ps/CStr.h"
      66             : 
      67             : class XMBElement;
      68             : class XMBData;
      69             : class XMLWriter_Element;
      70             : 
      71           9 : class XMLWriter_File
      72             : {
      73             : public:
      74             :     XMLWriter_File();
      75             : 
      76           1 :     void SetPrettyPrint(bool enabled) { m_PrettyPrint = enabled; }
      77             : 
      78             :     void Comment(const char* text);
      79             : 
      80             :     void XMB(const XMBData& xmb);
      81             : 
      82             :     bool StoreVFS(const PIVFS& vfs, const VfsPath& pathname);
      83             :     const CStr8& GetOutput();
      84             : 
      85             : private:
      86             : 
      87             :     friend class XMLWriter_Element;
      88             : 
      89             :     void ElementXMB(const XMBData& xmb, XMBElement el);
      90             : 
      91             :     void ElementStart(XMLWriter_Element* element, const char* name);
      92             :     void ElementText(const char* text, bool cdata);
      93             :     template <typename T> void ElementAttribute(const char* name, const T& value, bool newelement);
      94             :     void ElementClose();
      95             :     void ElementEnd(const char* name, int type);
      96             : 
      97             :     CStr8 Indent();
      98             : 
      99             :     bool m_PrettyPrint;
     100             : 
     101             :     CStr m_Data;
     102             :     int m_Indent;
     103             :     XMLWriter_Element* m_LastElement;
     104             : };
     105             : 
     106             : class XMLWriter_Element
     107             : {
     108             : public:
     109             :     XMLWriter_Element(XMLWriter_File& file, const char* name);
     110             :     ~XMLWriter_Element();
     111             : 
     112             :     template <typename constCharPtr> void Text(constCharPtr text, bool cdata);
     113           5 :     template <typename T> void Attribute(const char* name, T value) { m_File->ElementAttribute(name, value, false); }
     114           1 :     template <typename T> void Setting(const char* name, T value) { m_File->ElementAttribute(name, value, true); }
     115             :     void Close(int type);
     116             : 
     117             : private:
     118             : 
     119             :     friend class XMLWriter_File;
     120             : 
     121             :     XMLWriter_File* m_File;
     122             :     CStr m_Name;
     123             :     int m_Type;
     124             : };
     125             : 
     126             : #endif // INCLUDED_XMLWRITER

Generated by: LCOV version 1.13