Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / xml_parse_utils.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include "xml_parse_utils.h"
6 #include "details/ie_exception.hpp"
7 #include "ie_precision.hpp"
8 #include <string>
9 #include <limits>
10
11 int XMLParseUtils::GetIntAttr(const pugi::xml_node &node, const char *str) {
12     auto attr = node.attribute(str);
13     if (attr.empty())
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());
17     std::size_t idx = 0;
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();
23     return int_value;
24 }
25
26 uint64_t XMLParseUtils::GetUInt64Attr(const pugi::xml_node &node, const char *str) {
27     auto attr = node.attribute(str);
28     if (attr.empty())
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());
32     std::size_t idx = 0;
33     long long int_value = std::stoll(str_value, &idx, 10);
34     if (idx != str_value.length() || int_value < 0 || int_value > (std::numeric_limits<uint64_t>::max)())
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);
39 }
40
41 unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *str) {
42     auto attr = node.attribute(str);
43     if (attr.empty())
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());
47     std::size_t idx = 0;
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);
54 }
55
56 std::string XMLParseUtils::GetStrAttr(const pugi::xml_node &node, const char *str) {
57     auto attr = node.attribute(str);
58     if (attr.empty())
59         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
60                            << node.offset_debug();
61     return attr.value();
62 }
63
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;
67     return attr.value();
68 }
69
70 float XMLParseUtils::GetFloatAttr(const pugi::xml_node &node, const char *str) {
71     auto attr = node.attribute(str);
72     if (attr.empty())
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());
76     std::size_t idx = 0;
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();
82     return float_value;
83 }
84
85 InferenceEngine::Precision XMLParseUtils::GetPrecisionAttr(const pugi::xml_node &node, const char *str) {
86     auto attr = node.attribute(str);
87     if (attr.empty())
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());
91 }
92
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());
98 }
99
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);
104 }
105
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);
110 }
111
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);
116 }
117
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);
122 }
123
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());
128 }
129
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);
137     } else {
138         baseName = baseName.substr(slashPos);
139     }
140     return baseName;
141 }
142