ETISS 0.8.0
Extendable Translating Instruction Set Simulator (version 0.8.0)
XML.cpp
Go to the documentation of this file.
1 /*
2 
3  @copyright
4 
5  <pre>
6 
7  Copyright 2018 Infineon Technologies AG
8 
9  This file is part of ETISS tool, see <https://github.com/tum-ei-eda/etiss>.
10 
11  The initial version of this software has been created with the funding support by the German Federal
12  Ministry of Education and Research (BMBF) in the project EffektiV under grant 01IS13022.
13 
14  Redistribution and use in source and binary forms, with or without modification, are permitted
15  provided that the following conditions are met:
16 
17  1. Redistributions of source code must retain the above copyright notice, this list of conditions and
18  the following disclaimer.
19 
20  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
21  and the following disclaimer in the documentation and/or other materials provided with the distribution.
22 
23  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
24  or promote products derived from this software without specific prior written permission.
25 
26  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
27  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
28  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
29  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  POSSIBILITY OF SUCH DAMAGE.
34 
35  </pre>
36 
37  @author Chair of Electronic Design Automation, TUM
38 
39  @version 0.1
40 
41 */
42 
43 #ifndef _CRT_SECURE_NO_WARNINGS
44 #define _CRT_SECURE_NO_WARNINGS
45 #endif
46 #ifndef NO_ETISS
47 #include "etiss/fault/XML.h"
48 #else
49 #include "fault/XML.h"
50 #endif
51 
52 #include <sstream>
53 
54 namespace etiss
55 {
56 
57 namespace fault
58 {
59 
60 // some helper for changing Core Names
62 void setCoreName(std::string &str)
63 {
64  size_t pos = 0;
65  std::string token = "%i%";
66  std::string replace = std::to_string(coreIDActuallXML);
67  while ((pos = str.find(token, pos)) != std::string::npos)
68  {
69  str.replace(pos, token.length(), replace);
70  pos += replace.length();
71  }
72 }
73 
74 #if ETISS_FAULT_XML
75 namespace xml
76 {
77 
78 void Diagnostics::ignoredNode(const pugi::xml_node &node, const std::string &msg)
79 {
80  warnings.push_back(std::string("XML Warning: ") + msg + " [" + node.path() + "]");
81 }
82 void Diagnostics::unexpectedNode(const pugi::xml_node &node, const std::string &msg)
83 {
84  errors.push_back(std::string("XML Error: ") + msg + " [" + node.path() + "]");
85 }
86 void Diagnostics::print(std::ostream &out) const
87 {
88  for (std::list<std::string>::const_iterator iter = warnings.begin(); iter != warnings.end(); ++iter)
89  {
90  out << *iter << std::endl;
91  }
92  for (std::list<std::string>::const_iterator iter = errors.begin(); iter != errors.end(); ++iter)
93  {
94  out << *iter << std::endl;
95  }
96 }
97 
98 template <>
99 bool parse<std::string>(pugi::xml_node node, std::string &dst, Diagnostics &diag)
100 {
101  if (node.type() == pugi::node_null)
102  {
103  diag.unexpectedNode(node, "cannot get string from null node");
104  return false;
105  }
106  pugi::xml_node tnode = node.first_child();
107  if (tnode.type() != pugi::node_pcdata)
108  {
109  diag.unexpectedNode(tnode, "cannot get string from non pcdata node");
110  return false;
111  }
112  if (tnode.next_sibling().type() != pugi::node_null)
113  {
114  diag.unexpectedNode(node, "node conatains more than just one text node");
115  return false;
116  }
117  dst = tnode.value();
118  return true;
119 }
120 template <>
121 bool write<std::string>(pugi::xml_node node, const std::string &src, Diagnostics &diag)
122 {
123  node.append_child(pugi::node_pcdata).set_value(src.c_str());
124  return true;
125 }
126 
127 template <>
128 bool parse<uint64_t>(pugi::xml_node node, uint64_t &dst, Diagnostics &diag)
129 {
130  std::string ret;
131  if (!parse<std::string>(node, ret, diag))
132  {
133  return false;
134  }
135  return fromString(ret, dst);
136 }
137 
138 bool parse_hex(pugi::xml_node node, uint64_t &dst, Diagnostics &diag)
139 {
140  std::string ret;
141  if (!parse<std::string>(node, ret, diag))
142  {
143  return false;
144  }
145  uint64_t val;
146  std::stringstream ss;
147  ss << std::hex << ret;
148  ss >> val;
149  dst = val;
150  return true;
151 }
152 template <>
153 bool write<uint64_t>(pugi::xml_node node, const uint64_t &src, Diagnostics &diag)
154 {
155  std::stringstream ss;
156  ss << src;
157  return write<std::string>(node, ss.str(), diag);
158 }
159 
160 template <>
161 bool parse<unsigned>(pugi::xml_node node, unsigned &dst, Diagnostics &diag)
162 {
163  std::string ret;
164  if (!parse<std::string>(node, ret, diag))
165  {
166  return false;
167  }
168  return fromString(ret, dst);
169 }
170 template <>
171 bool write<unsigned>(pugi::xml_node node, const unsigned &src, Diagnostics &diag)
172 {
173  std::stringstream ss;
174  ss << src;
175  return write<std::string>(node, ss.str(), diag);
176 }
177 
178 template <>
179 bool parse_attr<std::string>(pugi::xml_node node, const std::string &attr_name, std::string &dst, Diagnostics &diag)
180 {
181  unsigned count = 0;
182  for (pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute())
183  {
184  if (hasName(attr, attr_name))
185  {
186  if (count == 0)
187  {
188  dst = attr.value();
189  }
190  else
191  {
192  diag.unexpectedNode(node, std::string("Duplicated attribute: ") + attr_name);
193  }
194  count++;
195  }
196  }
197  return count == 1;
198 }
199 
200 template <>
201 bool write_attr<std::string>(pugi::xml_node node, const std::string &attr_name, const std::string &src,
202  Diagnostics &diag)
203 {
204  for (pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute())
205  {
206  if (hasName(attr, attr_name))
207  {
208  attr.set_value(src.c_str());
209  return true;
210  }
211  }
212  node.append_attribute(attr_name.c_str()).set_value(src.c_str());
213  return true;
214 }
215 
216 template <>
217 bool fromString<uint64_t>(const std::string &s, uint64_t &val)
218 {
219 #if CXX0X_UP_SUPPORTED
220  static_assert(sizeof(uint64_t) <= sizeof(unsigned long long), "cannot convert string to uint64_t with stoull");
221  try
222  {
223  val = std::stoull(s);
224  return true;
225  }
226  catch (...)
227  {
228  return false;
229  }
230 #else
231  unsigned long long tmp;
232  if (!sscanf(s.c_str(), "%llu", &tmp))
233  return false;
234  val = (uint64_t)tmp;
235  return true;
236 #endif
237 }
238 template <>
239 bool fromString<unsigned>(const std::string &s, unsigned &val)
240 {
241 #if CXX0X_UP_SUPPORTED
242  try
243  {
244  val = (unsigned)std::stoull(s);
245  return true;
246  }
247  catch (...)
248  {
249  return false;
250  }
251 #else
252  if (!sscanf(s.c_str(), "%iu", &val))
253  return false;
254  return true;
255 #endif
256 }
257 
258 bool hasName(const pugi::xml_node &node, const std::string &name)
259 {
260  const char *cname = node.name();
261  if (cname == 0)
262  return false;
263  return name == cname;
264 }
265 bool hasName(const pugi::xml_attribute &attr, const std::string &name)
266 {
267  const char *cname = attr.name();
268  if (cname == 0)
269  return false;
270  return name == cname;
271 }
272 
273 pugi::xml_node findSingleNode(pugi::xml_node node, const std::string &name, Diagnostics &diag)
274 {
275  pugi::xml_node ret;
276  bool retset = false;
277  for (pugi::xml_node cnode = node.first_child(); cnode; cnode = cnode.next_sibling())
278  {
279  if (hasName(cnode, name))
280  {
281  if (retset)
282  {
283  ret = pugi::xml_node();
284  diag.unexpectedNode(cnode, "only one node with this name may exist");
285  }
286  else
287  {
288  retset = true;
289  ret = cnode;
290  }
291  }
292  }
293  return ret;
294 }
295 
296 } // namespace xml
297 #endif
298 
299 } // namespace fault
300 
301 } // namespace etiss
#define static_assert(x, y)
Definition: Fault.cpp:60
contains XML related functions.
__device__ __2f16 float bool s
static __inline__ uint64_t
Definition: arm_cde.h:31
const char_t * name() const
Definition: pugixml.cpp:4435
xml_attribute next_attribute() const
Definition: pugixml.cpp:4378
bool set_value(const char_t *rhs)
Definition: pugixml.cpp:4507
string_t path(char_t delimiter='/') const
Definition: pugixml.cpp:5291
bool set_value(const char_t *rhs)
Definition: pugixml.cpp:4821
xml_node_type type() const
Definition: pugixml.cpp:4669
xml_node append_child(xml_node_type type=node_element)
Definition: pugixml.cpp:4983
xml_node first_child() const
Definition: pugixml.cpp:4797
xml_attribute append_attribute(const char_t *name)
Definition: pugixml.cpp:4837
xml_node next_sibling() const
Definition: pugixml.cpp:4715
const char_t * value() const
Definition: pugixml.cpp:4674
xml_attribute first_attribute() const
Definition: pugixml.cpp:4787
const char_t * name() const
Definition: pugixml.cpp:4664
void setCoreName(std::string &str)
Definition: XML.cpp:62
int coreIDActuallXML
Definition: XML.cpp:61
Page Table Entry (PTE) defines the composition of Page Frame Number (PFN) and relavant flags.
Definition: Benchmark.h:53
@ node_pcdata
Definition: pugixml.hpp:108
@ node_null
Definition: pugixml.hpp:105