File: xml_parser.h

package info (click to toggle)
embree 3.13.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,924 kB
  • sloc: cpp: 180,815; xml: 3,877; ansic: 2,957; python: 1,466; sh: 502; makefile: 229; csh: 42
file content (127 lines) | stat: -rw-r--r-- 4,211 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "../default.h"

namespace embree
{
  /* an XML node */
  class XML : public RefCount
  {
  public:
    XML (const std::string& name = "") : name(name) {}

    /*! returns number of children of XML node */
    size_t size() const { return children.size(); }

    /*! checks if child with specified node tag exists */
    bool hasChild(const std::string& childID) const 
    {
      for (size_t i=0; i<children.size(); i++)
        if (children[i]->name == childID) return true;
      return false;
    }

    /*! returns a parameter of the XML node */
    std::string parm(const std::string& parmID) const {
      std::map<std::string,std::string>::const_iterator i = parms.find(parmID);
      if (i == parms.end()) return ""; else return i->second;
    }

    Vec2f parm_Vec2f(const std::string& parmID) const {
      std::map<std::string,std::string>::const_iterator i = parms.find(parmID);
      if (i == parms.end()) THROW_RUNTIME_ERROR (loc.str()+": XML node has no parameter \"" + parmID + "\"");
      return string_to_Vec2f(i->second);
    }

    Vec3fa parm_Vec3fa(const std::string& parmID) const {
      std::map<std::string,std::string>::const_iterator i = parms.find(parmID);
      if (i == parms.end()) THROW_RUNTIME_ERROR (loc.str()+": XML node has no parameter \"" + parmID + "\"");
      return Vec3fa(string_to_Vec3f(i->second));
    }

    float parm_float(const std::string& parmID) const {
      std::map<std::string,std::string>::const_iterator i = parms.find(parmID);
      if (i == parms.end()) THROW_RUNTIME_ERROR (loc.str()+": XML node has no parameter \"" + parmID + "\"");
      return std::stof(i->second);
    }

    /*! returns the nth child */
    const Ref<XML> child(const size_t id) const 
    {
      if (id >= children.size()) 
        THROW_RUNTIME_ERROR (loc.str()+": XML node has no child \"" + toString(id) + "\"");
      return children[id];
    }

    /*! returns child by node tag */
    const Ref<XML> child(const std::string& childID) const 
    {
      for (size_t i=0; i<children.size(); i++)
        if (children[i]->name == childID) return children[i];
      THROW_RUNTIME_ERROR (loc.str()+": XML node has no child \"" + childID + "\"");
    }

    /*! returns child by node tag without failing */
    const Ref<XML> childOpt(const std::string& childID) const 
    {
      for (size_t i=0; i<children.size(); i++)
        if (children[i]->name == childID) return children[i];
      return null;
    }

    /*! adds a new parameter to the node */
    Ref<XML> add(const std::string& name, const std::string& val) {
      parms[name] = val; 
      return this; 
    }

    /*! adds a new child */
    Ref<XML> add(const Ref<XML>& xml) { 
      children.push_back(xml); 
      return this; 
    }

    /*! adds new data tokens to the body of the node */
    Ref<XML> add(const Token& tok) {
      body.push_back(tok);     
      return this; 
    }

    /*! compares two XML nodes */
    friend bool operator ==( const Ref<XML>& a, const Ref<XML>& b ) {
      return a->name == b->name && a->parms == b->parms && a->children == b->children && a->body == b->body;
    }

    /*! orders two XML nodes */
    friend bool operator <( const Ref<XML>& a, const Ref<XML>& b ) {
      if (a->name     != b->name    ) return a->name     < b->name;
      if (a->parms    != b->parms   ) return a->parms    < b->parms;
      if (a->children != b->children) return a->children < b->children;
      if (a->body     != b->body    ) return a->body     < b->body;
      return false;
    }

  public:
    ParseLocation loc;
    std::string name;
    std::map<std::string,std::string> parms;
    std::vector<Ref<XML> > children;
    std::vector<Token> body;
  };

  /*! load XML file from stream */
  std::istream& operator>>(std::istream& cin, Ref<XML>& xml);

  /*! load XML file from disk */
  Ref<XML> parseXML(const FileName& fileName, std::string id = "", bool hasHeader = true);

  /* store XML to stream */
  std::ostream& operator<<(std::ostream& cout, const Ref<XML>& xml);

  /*! store XML to disk */
  void emitXML(const FileName& fileName, const Ref<XML>& xml);
}