NSVD Reader  0.0.1
tinyxml.hpp
Go to the documentation of this file.
1 
7 /*
8  * These codes are licensed under CC0.
9  * http://creativecommons.org/publicdomain/zero/1.0/
10  */
11 /*
12  * TinyXML-2 license is zlib license.
13  */
14 #ifndef __NODAMUSHI_SVD_TINYXML_HPP__
15 #define __NODAMUSHI_SVD_TINYXML_HPP__
16 
18 # include <tinyxml2.h>
19 # include <cstdio>
20 
21 namespace nodamushi{
22 namespace svd{
23 
24 
40 {
41 
42 
43  tinyxml_svd_reader(const std::string& filename,bool throw_error=false):
44  doc(),child(nullptr),_ok(false)
45  {
46  if(file_read_exception::check(filename,throw_error)){
47  if(doc.LoadFile(filename.data()) == tinyxml2::XML_SUCCESS){
48  child = doc.FirstChildElement();
49  _ok = true;
50  }else if(throw_error){
51  throw xml_parse_exception::make(filename);
52  }
53  }
54  }
55 
59  bool ok()const {return _ok;}
60 
61 
62  using node = ::tinyxml2::XMLNode;
63  using text = ::tinyxml2::XMLText;
64  using element = ::tinyxml2::XMLElement;
65  using attribute = ::tinyxml2::XMLAttribute;
66  using document = ::tinyxml2::XMLDocument;
67 
68 
69  struct sub_reader:public svd_reader
70  {
71  sub_reader(const element* e):
72  attr(false),name(e->Name()),value(),
73  attrs(e->FirstAttribute()),child(e->FirstChildElement())
74  {
75  for(const node* n = e->FirstChild();n!=nullptr;n = n->NextSibling()){
76  const text* t = n->ToText();
77  if(t){
78  value = t->Value();
80  }
81  }
82  }
83  sub_reader(const attribute* a):
84  attr(true),name(a->Name()),value(a->Value()),
85  attrs(nullptr),child(nullptr)
86  {}
87 
88  //-------------------------------------------------------
89  // svd_reader interface
90  //-------------------------------------------------------
91  bool is_attribute()const{return attr;}
92  string_ref get_name()const{return name;}
93  string_ref get_value()const{return value;}
95  {
96  if(attrs!=nullptr){
97  auto a = attrs;
98  attrs = a->Next();
99  return {a};
100  }
101  auto c= child;
102  child = c->NextSiblingElement();
103  return {c};
104  }
105  operator bool()const{
106  return attrs != nullptr || child != nullptr;
107  }
108 
109  private:
110  bool attr;
111 # if __cplusplus >= 201703
112  std::string_view name;
113  std::string_view value;
114 # else
115  std::string name;
116  std::string value;
117 # endif
118  const attribute* attrs;
119  const element* child;
120  };
121 
122 
123  //-------------------------------------------------------
124  // svd_reader interface
125  //-------------------------------------------------------
126  bool is_attribute()const{return false;}
127 # if __cplusplus >= 201703
128  std::string_view get_name()const{return std::string_view{};}
129  std::string_view get_value()const{return std::string_view{};}
130 # else
131  std::string get_name()const{return std::string{};}
132  std::string get_value()const{return std::string{};}
133 # endif
135  {
136  auto* c = child;
137  child = child->NextSiblingElement();
138  return {c};
139  }
140  operator bool()const{return child != nullptr;}
141 
142 
143 
144  private:
145  document doc;
146  element* child;
147  bool _ok;
148 };
149 
150 }}
151 #endif
std::string get_name() const
Definition: tinyxml.hpp:131
static bool check(const std::string &file_name, bool throw_exception)
Definition: svd_reader.hpp:54
::tinyxml2::XMLElement element
Definition: tinyxml.hpp:64
::tinyxml2::XMLDocument document
Definition: tinyxml.hpp:66
const std::string & string_ref
Definition: string_type.hpp:44
std::string get_value() const
Definition: tinyxml.hpp:132
tinyxml_svd_reader(const std::string &filename, bool throw_error=false)
Definition: tinyxml.hpp:43
::tinyxml2::XMLNode node
Definition: tinyxml.hpp:62
This class reperesents SVD(xml) element / attribute.
Definition: value.hpp:53
::tinyxml2::XMLText text
Definition: tinyxml.hpp:63
::tinyxml2::XMLAttribute attribute
Definition: tinyxml.hpp:65
svd_reader
static xml_parse_exception make(const std::string &file, size_t file_line=UNKNOWN_FILE_LINE)
Definition: svd_reader.hpp:80
static void trim(std::string &str)
remove white spaces
Definition: svd_reader.hpp:125