LCOV - code coverage report
Current view: top level - source/ps/XMB - XMBData.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 31 33 93.9 %
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             : /*
      19             :     Xeromyces - XMB reading library
      20             : 
      21             : Brief outline:
      22             : 
      23             : XMB originated as a binary representation of XML, with some limitations
      24             : but much more efficiency (particularly for loading simple data
      25             : classes that don't need much initialisation).
      26             : 
      27             : Theoretical file structure:
      28             : 
      29             : XMB_File {
      30             :     char Header[4]; // because everyone has one; currently "XMB0"
      31             :     std::uint32_t Version;
      32             : 
      33             :     int OffsetFromStartToElementNames;
      34             :     int ElementNameCount;
      35             : 
      36             :     int OffsetFromStartToAttributeNames;
      37             :     int AttributeNameCount;
      38             : 
      39             :     XMB_Node Root;
      40             : 
      41             :     ZStr8 ElementNames[];
      42             :     ZStr8 AttributeNames[];
      43             : 
      44             : }
      45             : 
      46             : XMB_Node {
      47             : 0)  int Length; // of entire struct, so it can be skipped over
      48             : 
      49             : 4)  int ElementName;
      50             : 
      51             : 8)  int AttributeCount;
      52             : 12) int ChildCount;
      53             : 
      54             : 16) int ChildrenOffset; // == sizeof(Text)+sizeof(Attributes)
      55             : 20) XMB_Text Text;
      56             :     XMB_Attribute Attributes[];
      57             :     XMB_Node Children[];
      58             : 
      59             : }
      60             : 
      61             : XMB_Attribute {
      62             :     int Name;
      63             :     ZStr8 Value;
      64             : }
      65             : 
      66             : ZStr8 {
      67             :     int Length; // in bytes
      68             :     char* Text; // null-terminated UTF8
      69             : }
      70             : 
      71             : XMB_Text {
      72             : 20) int Length; // 0 if there's no text, else 4+sizeof(Text) in bytes including terminator
      73             :     // If Length != 0:
      74             : 24) int LineNumber; // for e.g. debugging scripts
      75             : 28) char* Text; // null-terminated UTF8
      76             : }
      77             : 
      78             : */
      79             : 
      80             : #ifndef INCLUDED_XEROXMB
      81             : #define INCLUDED_XEROXMB
      82             : 
      83             : #include "ps/CStr.h"
      84             : 
      85             : #include <cstddef>
      86             : #include <iterator>
      87             : #include <string_view>
      88             : 
      89             : class XMBAttributeList;
      90             : class XMBElement;
      91             : class XMBElementList;
      92             : class XMBStorage;
      93             : 
      94             : class XMBData
      95             : {
      96             : public:
      97             : 
      98       14778 :     XMBData() : m_Pointer(nullptr) {}
      99             : 
     100             :     /*
     101             :     * Initialise from the contents of an XMBStorage.
     102             :     * @param doc must remain allocated and unchanged while
     103             :     * the XMBData is being used.
     104             :     * @return indication of success; main cause for failure is attempting to
     105             :     * load a partially valid XMB file (e.g. if the game was interrupted
     106             :     * while writing it), which we detect by checking the magic string.
     107             :     * It also fails when trying to load an XMB file with a different version.
     108             :     */
     109             :     bool Initialise(const XMBStorage& doc);
     110             : 
     111             :     // Returns the root element
     112             :     XMBElement GetRoot() const;
     113             : 
     114             :     // Returns internal ID for a given element/attribute string.
     115             :     int GetElementID(const char* Name) const;
     116             :     int GetAttributeID(const char* Name) const;
     117             : 
     118             :     // Returns element/attribute string for a given internal ID.
     119             :     const char* GetElementString(const int ID) const;
     120             :     const char* GetAttributeString(const int ID) const;
     121             : 
     122             :     std::string_view GetElementStringView(const int ID) const;
     123             :     std::string_view GetAttributeStringView(const int ID) const;
     124             : 
     125             : private:
     126             :     const char* m_Pointer;
     127             : 
     128             :     int m_ElementNameCount;
     129             :     int m_AttributeNameCount;
     130             :     const char* m_ElementPointer;
     131             :     const char* m_AttributePointer;
     132             : };
     133             : 
     134             : class XMBElement
     135             : {
     136             : public:
     137           3 :     XMBElement()
     138           0 :         : m_Pointer(0) {}
     139             : 
     140      428191 :     XMBElement(const char* offset)
     141             :         : m_Pointer(offset) {}
     142             : 
     143             :     int GetNodeName() const;
     144             :     XMBElementList GetChildNodes() const;
     145             :     XMBAttributeList GetAttributes() const;
     146             :     CStr8 GetText() const;
     147             :     // Returns the line number of the text within this element,
     148             :     // or -1 if there is no text
     149             :     int GetLineNumber() const;
     150             : 
     151             : private:
     152             :     // Pointer to the start of the node
     153             :     const char* m_Pointer;
     154             : };
     155             : 
     156             : class XMBElementList
     157             : {
     158             : public:
     159      440773 :     XMBElementList(const char* offset, size_t count, const char* endoffset)
     160      440773 :         : m_Size(count), m_Pointer(offset), m_CurItemID(0), m_CurPointer(offset), m_EndPointer(endoffset) {}
     161             : 
     162             :     // Get first element in list with the given name.
     163             :     // Performance is linear in the number of elements in the list.
     164             :     XMBElement GetFirstNamedItem(const int ElementName) const;
     165             : 
     166             :     // Linear in the number of elements in the list
     167             :     XMBElement operator[](size_t id); // returns Children[id]
     168             : 
     169             :     class iterator
     170             :     {
     171             :     public:
     172             :         typedef ptrdiff_t difference_type;
     173             :         typedef XMBElement value_type;
     174             :         typedef XMBElement reference; // Because we need to construct the object
     175             :         typedef XMBElement pointer; // Because we need to construct the object
     176             :         typedef std::forward_iterator_tag iterator_category;
     177             : 
     178      440723 :         iterator(size_t size, const char* ptr, const char* endptr = NULL)
     179      440723 :             : m_Size(size), m_CurItemID(endptr ? size : 0), m_CurPointer(endptr ? endptr : ptr) {}
     180      428182 :         XMBElement operator*() const { return XMBElement(m_CurPointer); }
     181             :         XMBElement operator->() const { return **this; }
     182             :         iterator& operator++();
     183             : 
     184      868906 :         bool operator==(const iterator& rhs) const
     185             :         {
     186     1737812 :             return m_Size == rhs.m_Size &&
     187      868906 :                    m_CurItemID == rhs.m_CurItemID &&
     188      440724 :                    m_CurPointer == rhs.m_CurPointer;
     189             :         }
     190     1737809 :         bool operator!=(const iterator& rhs) const { return !(*this == rhs); }
     191             :     private:
     192             :         size_t m_Size;
     193             :         size_t m_CurItemID;
     194             :         const char* m_CurPointer;
     195             :     };
     196      440723 :     iterator begin() { return iterator(m_Size, m_Pointer); }
     197      881446 :     iterator end() { return iterator(m_Size, m_Pointer, m_EndPointer); }
     198             : 
     199          31 :     size_t size() const { return m_Size; }
     200           0 :     bool empty() const { return m_Size == 0; }
     201             : 
     202             : private:
     203             :     size_t m_Size;
     204             : 
     205             :     const char* m_Pointer;
     206             : 
     207             :     // For optimised sequential access:
     208             :     size_t m_CurItemID;
     209             :     const char* m_CurPointer;
     210             : 
     211             :     const char* m_EndPointer;
     212             : };
     213             : 
     214             : 
     215      142930 : struct XMBAttribute
     216             : {
     217             :     XMBAttribute() {}
     218      127159 :     XMBAttribute(int name, const CStr8& value)
     219      127159 :         : Name(name), Value(value) {};
     220             : 
     221             :     int Name;
     222             :     CStr8 Value; // UTF-8 encoded
     223             : };
     224             : 
     225             : class XMBAttributeList
     226             : {
     227             : public:
     228     1332430 :     XMBAttributeList(const char* offset, size_t count, const char* endoffset)
     229     1332430 :         : m_Size(count), m_Pointer(offset), m_CurItemID(0), m_CurPointer(offset), m_EndPointer(endoffset) {}
     230             : 
     231             :     // Get the attribute value directly
     232             :     CStr8 GetNamedItem(const int AttributeName) const;
     233             : 
     234             :     // Linear in the number of elements in the list
     235             :     XMBAttribute operator[](size_t id); // returns Children[id]
     236             : 
     237             :     class iterator
     238             :     {
     239             :     public:
     240             :         typedef ptrdiff_t difference_type;
     241             :         typedef XMBAttribute value_type;
     242             :         typedef XMBAttribute reference; // Because we need to construct the object
     243             :         typedef XMBAttribute pointer; // Because we need to construct the object
     244             :         typedef std::forward_iterator_tag iterator_category;
     245             : 
     246     1323546 :         iterator(size_t size, const char* ptr, const char* endptr = NULL)
     247     1323546 :             : m_Size(size), m_CurItemID(endptr ? size : 0), m_CurPointer(endptr ? endptr : ptr) {}
     248             :         XMBAttribute operator*() const;
     249             :         XMBAttribute operator->() const { return **this; }
     250             :         iterator& operator++();
     251             : 
     252     1433639 :         bool operator==(const iterator& rhs) const
     253             :         {
     254     2867278 :             return m_Size == rhs.m_Size &&
     255     1433639 :                    m_CurItemID == rhs.m_CurItemID &&
     256     1306485 :                    m_CurPointer == rhs.m_CurPointer;
     257             :         }
     258     2882732 :         bool operator!=(const iterator& rhs) const { return !(*this == rhs); }
     259             :     private:
     260             :         size_t m_Size;
     261             :         size_t m_CurItemID;
     262             :         const char* m_CurPointer;
     263             :     };
     264     1323546 :     iterator begin() const { return iterator(m_Size, m_Pointer); }
     265     2647092 :     iterator end() const { return iterator(m_Size, m_Pointer, m_EndPointer); }
     266             : 
     267           6 :     size_t size() const { return m_Size; }
     268             :     bool empty() const { return m_Size == 0; }
     269             : 
     270             : private:
     271             :     size_t m_Size;
     272             : 
     273             :     // Pointer to start of attribute list
     274             :     const char* m_Pointer;
     275             : 
     276             :     // For optimised sequential access:
     277             :     size_t m_CurItemID;
     278             :     const char* m_CurPointer;
     279             : 
     280             :     const char* m_EndPointer;
     281             : };
     282             : 
     283             : #endif // INCLUDED_XEROXMB

Generated by: LCOV version 1.16