7#ifndef _CRT_SECURE_NO_WARNINGS
8#define _CRT_SECURE_NO_WARNINGS
29 std::string token =
"%i%";
31 while ((pos = str.find(token, pos)) != std::string::npos)
33 str.replace(pos, token.length(), replace);
34 pos += replace.length();
42void Diagnostics::ignoredNode(
const pugi::xml_node &node,
const std::string &msg)
44 warnings.push_back(std::string(
"XML Warning: ") + msg +
" [" + node.path() +
"]");
46void Diagnostics::unexpectedNode(
const pugi::xml_node &node,
const std::string &msg)
48 errors.push_back(std::string(
"XML Error: ") + msg +
" [" + node.path() +
"]");
50void Diagnostics::print(std::ostream &out)
const
52 for (std::list<std::string>::const_iterator iter = warnings.begin(); iter != warnings.end(); ++iter)
54 out << *iter << std::endl;
56 for (std::list<std::string>::const_iterator iter = errors.begin(); iter != errors.end(); ++iter)
58 out << *iter << std::endl;
63bool parse<std::string>(pugi::xml_node node, std::string &dst, Diagnostics &diag)
65 if (node.type() == pugi::node_null)
67 diag.unexpectedNode(node,
"cannot get string from null node");
70 pugi::xml_node tnode = node.first_child();
71 if (tnode.type() != pugi::node_pcdata)
73 diag.unexpectedNode(tnode,
"cannot get string from non pcdata node");
76 if (tnode.next_sibling().type() != pugi::node_null)
78 diag.unexpectedNode(node,
"node conatains more than just one text node");
85bool write<std::string>(pugi::xml_node node,
const std::string &src, Diagnostics &diag)
87 node.append_child(pugi::node_pcdata).set_value(src.c_str());
92bool parse<uint64_t>(pugi::xml_node node,
uint64_t &dst, Diagnostics &diag)
95 if (!parse<std::string>(node, ret, diag))
99 return fromString(ret, dst);
102bool parse_hex(pugi::xml_node node,
uint64_t &dst, Diagnostics &diag)
105 if (!parse<std::string>(node, ret, diag))
110 val = std::stoll(ret,
nullptr, 16);
115bool write<uint64_t>(pugi::xml_node node,
const uint64_t &src, Diagnostics &diag)
117 return write<std::string>(node, std::to_string(src), diag);
121bool parse<unsigned>(pugi::xml_node node,
unsigned &dst, Diagnostics &diag)
124 if (!parse<std::string>(node, ret, diag))
128 return fromString(ret, dst);
131bool write<unsigned>(pugi::xml_node node,
const unsigned &src, Diagnostics &diag)
133 return write<std::string>(node, std::to_string(src), diag);
137bool parse_attr<std::string>(pugi::xml_node node,
const std::string &attr_name, std::string &dst, Diagnostics &diag)
140 for (pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute())
142 if (hasName(attr, attr_name))
150 diag.unexpectedNode(node, std::string(
"Duplicated attribute: ") + attr_name);
159bool write_attr<std::string>(pugi::xml_node node,
const std::string &attr_name,
const std::string &src,
162 for (pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute())
164 if (hasName(attr, attr_name))
166 attr.set_value(src.c_str());
170 node.append_attribute(attr_name.c_str()).set_value(src.c_str());
175bool fromString<uint64_t>(
const std::string &
s,
uint64_t &val)
177#if CXX0X_UP_SUPPORTED
178 static_assert(
sizeof(
uint64_t) <=
sizeof(
unsigned long long),
"cannot convert string to uint64_t with stoull");
181 val = std::stoull(
s);
189 unsigned long long tmp;
190 if (!sscanf(
s.c_str(),
"%llu", &tmp))
197bool fromString<unsigned>(
const std::string &
s,
unsigned &val)
199#if CXX0X_UP_SUPPORTED
202 val = (unsigned)std::stoull(
s);
210 if (!sscanf(
s.c_str(),
"%iu", &val))
216bool hasName(
const pugi::xml_node &node,
const std::string &name)
218 const char *cname = node.name();
221 return name == cname;
223bool hasName(
const pugi::xml_attribute &attr,
const std::string &name)
225 const char *cname = attr.name();
228 return name == cname;
231pugi::xml_node findSingleNode(pugi::xml_node node,
const std::string &name, Diagnostics &diag)
235 for (pugi::xml_node cnode = node.first_child(); cnode; cnode = cnode.next_sibling())
237 if (hasName(cnode, name))
241 ret = pugi::xml_node();
242 diag.unexpectedNode(cnode,
"only one node with this name may exist");
257bool parseXML(pugi::xml_document &doc, std::istream &input, std::ostream &diagnostics_out)
259 pugi::xml_parse_result pr = doc.load(input);
263 diagnostics_out <<
"failed to load xml from stream: " << pr.description() << std::endl;
contains XML related functions.
__device__ __2f16 float bool s
static __inline__ uint64_t
void setCoreName(std::string &str)
bool parseXML(pugi::xml_document &doc, std::istream &input, std::ostream &diagnostics_out=std::cout)
parse a XML document held in input stream and return as doc