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: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2025 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 "lib/file/vfs/vfs_path.h"
      66             : #include "ps/CStr.h"
      67             : 
      68             : class XMBElement;
      69             : class XMBData;
      70             : class XMLWriter_Element;
      71             : 
      72          18 : class XMLWriter_File
      73             : {
      74             : public:
      75             :     XMLWriter_File();
      76             : 
      77           1 :     void SetPrettyPrint(bool enabled) { m_PrettyPrint = enabled; }
      78             : 
      79             :     void Comment(const char* text);
      80             : 
      81             :     void XMB(const XMBData& xmb);
      82             : 
      83             :     bool StoreVFS(const PIVFS& vfs, const VfsPath& pathname);
      84             :     const CStr8& GetOutput();
      85             : 
      86             : private:
      87             : 
      88             :     friend class XMLWriter_Element;
      89             : 
      90             :     void ElementXMB(const XMBData& xmb, XMBElement el);
      91             : 
      92             :     void ElementStart(XMLWriter_Element* element, const char* name);
      93             :     void ElementText(const char* text, bool cdata);
      94             :     template <typename T> void ElementAttribute(const char* name, const T& value, bool newelement);
      95             :     void ElementClose();
      96             :     void ElementEnd(const char* name, int type);
      97             : 
      98             :     CStr8 Indent();
      99             : 
     100             :     bool m_PrettyPrint;
     101             : 
     102             :     CStr m_Data;
     103             :     int m_Indent;
     104             :     XMLWriter_Element* m_LastElement;
     105             : };
     106             : 
     107             : class XMLWriter_Element
     108             : {
     109             : public:
     110             :     XMLWriter_Element(XMLWriter_File& file, const char* name);
     111             :     ~XMLWriter_Element();
     112             : 
     113             :     template <typename constCharPtr> void Text(constCharPtr text, bool cdata);
     114           4 :     template <typename T> void Attribute(const char* name, T value) { m_File->ElementAttribute(name, value, false); }
     115           1 :     template <typename T> void Setting(const char* name, T value) { m_File->ElementAttribute(name, value, true); }
     116             :     void Close(int type);
     117             : 
     118             : private:
     119             : 
     120             :     friend class XMLWriter_File;
     121             : 
     122             :     XMLWriter_File* m_File;
     123             :     CStr m_Name;
     124             :     int m_Type;
     125             : };
     126             : 
     127             : #endif // INCLUDED_XMLWRITER

Generated by: LCOV version 1.16