1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include "xml_parse_utils.h"
6 #include "details/ie_exception.hpp"
7 #include "ie_precision.hpp"
11 int XMLParseUtils::GetIntAttr(const pugi::xml_node &node, const char *str) {
12 auto attr = node.attribute(str);
14 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
15 << node.offset_debug();
16 std::string str_value = std::string(attr.value());
18 int int_value = std::stoi(str_value, &idx, 10);
19 if (idx != str_value.length())
20 THROW_IE_EXCEPTION << "node <" << node.name() << "> has attribute \"" << str << "\" = \"" << str_value
21 << "\" which is not an integer" << " at offset "
22 << node.offset_debug();
26 uint64_t XMLParseUtils::GetUInt64Attr(const pugi::xml_node &node, const char *str) {
27 auto attr = node.attribute(str);
29 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
30 << node.offset_debug();
31 std::string str_value = std::string(attr.value());
33 long long int_value = std::stoll(str_value, &idx, 10);
34 if (idx != str_value.length() || int_value < 0)
35 THROW_IE_EXCEPTION << "node <" << node.name() << "> has attribute \"" << str << "\" = \"" << str_value
36 << "\" which is not an unsigned 64 bit integer" << " at offset "
37 << node.offset_debug();
38 return static_cast<uint64_t>(int_value);
41 unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *str) {
42 auto attr = node.attribute(str);
44 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
45 << node.offset_debug();
46 std::string str_value = std::string(attr.value());
48 long long int_value = std::stoll(str_value, &idx, 10);
49 if (idx != str_value.length() || int_value < 0 || int_value > (std::numeric_limits<unsigned int>::max)())
50 THROW_IE_EXCEPTION << "node <" << node.name() << "> has attribute \"" << str << "\" = \"" << str_value
51 << "\" which is not an unsigned integer" << " at offset "
52 << node.offset_debug();
53 return static_cast<unsigned int>(int_value);
56 std::string XMLParseUtils::GetStrAttr(const pugi::xml_node &node, const char *str) {
57 auto attr = node.attribute(str);
59 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
60 << node.offset_debug();
64 std::string XMLParseUtils::GetStrAttr(const pugi::xml_node &node, const char *str, const char *def) {
65 auto attr = node.attribute(str);
66 if (attr.empty()) return def;
70 float XMLParseUtils::GetFloatAttr(const pugi::xml_node &node, const char *str) {
71 auto attr = node.attribute(str);
73 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
74 << node.offset_debug();
75 std::string str_value = std::string(attr.value());
77 float float_value = std::stof(str_value, &idx);
78 if (idx != str_value.length())
79 THROW_IE_EXCEPTION << "node <" << node.name() << "> has attribute \"" << str << "\" = \"" << str_value
80 << "\" which is not a floating point" << " at offset "
81 << node.offset_debug();
85 InferenceEngine::Precision XMLParseUtils::GetPrecisionAttr(const pugi::xml_node &node, const char *str) {
86 auto attr = node.attribute(str);
88 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
89 << node.offset_debug();
90 return InferenceEngine::Precision::FromStr(attr.value());
93 InferenceEngine::Precision XMLParseUtils::GetPrecisionAttr(const pugi::xml_node &node, const char *str,
94 InferenceEngine::Precision def) {
95 auto attr = node.attribute(str);
96 if (attr.empty()) return InferenceEngine::Precision(def);
97 return InferenceEngine::Precision::FromStr(attr.value());
100 int XMLParseUtils::GetIntAttr(const pugi::xml_node &node, const char *str, int defVal) {
101 auto attr = node.attribute(str);
102 if (attr.empty()) return defVal;
103 return GetIntAttr(node, str);
106 uint64_t XMLParseUtils::GetUInt64Attr(const pugi::xml_node &node, const char *str, uint64_t defVal) {
107 auto attr = node.attribute(str);
108 if (attr.empty()) return defVal;
109 return GetUInt64Attr(node, str);
112 unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *str, unsigned int defVal) {
113 auto attr = node.attribute(str);
114 if (attr.empty()) return defVal;
115 return GetUIntAttr(node, str);
118 float XMLParseUtils::GetFloatAttr(const pugi::xml_node &node, const char *str, float defVal) {
119 auto attr = node.attribute(str);
120 if (attr.empty()) return defVal;
121 return GetFloatAttr(node, str);
124 int XMLParseUtils::GetIntChild(const pugi::xml_node &node, const char *str, int defVal) {
125 auto child = node.child(str);
126 if (child.empty()) return defVal;
127 return atoi(child.child_value());
130 std::string XMLParseUtils::NameFromFilePath(const char *filepath) {
131 std::string baseName = filepath;
132 auto slashPos = baseName.rfind('/');
133 slashPos = slashPos == std::string::npos ? 0 : slashPos + 1;
134 auto dotPos = baseName.rfind('.');
135 if (dotPos != std::string::npos) {
136 baseName = baseName.substr(slashPos, dotPos - slashPos);
138 baseName = baseName.substr(slashPos);