1 // Copyright (C) 2018 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"
9 int XMLParseUtils::GetIntAttr(const pugi::xml_node &node, const char *str) {
10 auto attr = node.attribute(str);
12 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
13 << node.offset_debug();
14 return atoi(attr.value());
17 uint64_t XMLParseUtils::GetUInt64Attr(const pugi::xml_node &node, const char *str) {
18 auto attr = node.attribute(str);
20 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
21 << node.offset_debug();
22 int64_t value = atoll(attr.value());
24 THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
25 << node.offset_debug();
26 return static_cast<uint64_t>(value);
29 unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *str) {
30 auto attr = node.attribute(str);
32 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
33 << node.offset_debug();
34 int value = atoi(attr.value());
36 THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
37 << node.offset_debug();
38 return static_cast<unsigned int>(value);
41 std::string XMLParseUtils::GetStrAttr(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();
49 std::string XMLParseUtils::GetStrAttr(const pugi::xml_node &node, const char *str, const char *def) {
50 auto attr = node.attribute(str);
51 if (attr.empty()) return def;
55 float XMLParseUtils::GetFloatAttr(const pugi::xml_node &node, const char *str) {
56 auto attr = node.attribute(str);
58 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
59 << node.offset_debug();
60 return static_cast<float>(atof(attr.value()));
63 InferenceEngine::Precision XMLParseUtils::GetPrecisionAttr(const pugi::xml_node &node, const char *str) {
64 auto attr = node.attribute(str);
66 THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
67 << node.offset_debug();
68 return InferenceEngine::Precision::FromStr(attr.value());
71 InferenceEngine::Precision XMLParseUtils::GetPrecisionAttr(const pugi::xml_node &node, const char *str,
72 InferenceEngine::Precision def) {
73 auto attr = node.attribute(str);
74 if (attr.empty()) return InferenceEngine::Precision(def);
75 return InferenceEngine::Precision::FromStr(attr.value());
78 int XMLParseUtils::GetIntAttr(const pugi::xml_node &node, const char *str, int defVal) {
79 auto attr = node.attribute(str);
80 if (attr.empty()) return defVal;
81 return atoi(attr.value());
84 uint64_t XMLParseUtils::GetUInt64Attr(const pugi::xml_node &node, const char *str, uint64_t defVal) {
85 auto attr = node.attribute(str);
86 if (attr.empty()) return defVal;
87 int64_t value = atoll(attr.value());
89 THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
90 << node.offset_debug();
91 return static_cast<uint64_t>(value);
94 unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *str, unsigned int defVal) {
95 auto attr = node.attribute(str);
96 if (attr.empty()) return defVal;
97 int value = atoi(attr.value());
99 THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
100 << node.offset_debug();
101 return static_cast<unsigned int>(value);
104 float XMLParseUtils::GetFloatAttr(const pugi::xml_node &node, const char *str, float defVal) {
105 auto attr = node.attribute(str);
106 if (attr.empty()) return defVal;
107 return static_cast<float>(atof(attr.value()));
110 int XMLParseUtils::GetIntChild(const pugi::xml_node &node, const char *str, int defVal) {
111 auto child = node.child(str);
112 if (child.empty()) return defVal;
113 return atoi(child.child_value());
116 std::string XMLParseUtils::NameFromFilePath(const char *filepath) {
117 std::string baseName = filepath;
118 auto slashPos = baseName.rfind('/');
119 slashPos = slashPos == std::string::npos ? 0 : slashPos + 1;
120 auto dotPos = baseName.rfind('.');
121 if (dotPos != std::string::npos) {
122 baseName = baseName.substr(slashPos, dotPos - slashPos);
124 baseName = baseName.substr(slashPos);