Publishing R5 content (#72)
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / xml_parse_utils.cpp
1 // Copyright (C) 2018 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
9 int XMLParseUtils::GetIntAttr(const pugi::xml_node &node, const char *str) {
10     auto attr = node.attribute(str);
11     if (attr.empty())
12         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
13                            << node.offset_debug();
14     return atoi(attr.value());
15 }
16
17 uint64_t XMLParseUtils::GetUInt64Attr(const pugi::xml_node &node, const char *str) {
18     auto attr = node.attribute(str);
19     if (attr.empty())
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());
23     if (value < 0)
24         THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
25                            << node.offset_debug();
26     return static_cast<uint64_t>(value);
27 }
28
29 unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *str) {
30     auto attr = node.attribute(str);
31     if (attr.empty())
32         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
33                            << node.offset_debug();
34     int value = atoi(attr.value());
35     if (value < 0)
36         THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
37                            << node.offset_debug();
38     return static_cast<unsigned int>(value);
39 }
40
41 std::string XMLParseUtils::GetStrAttr(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     return attr.value();
47 }
48
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;
52     return attr.value();
53 }
54
55 float XMLParseUtils::GetFloatAttr(const pugi::xml_node &node, const char *str) {
56     auto attr = node.attribute(str);
57     if (attr.empty())
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()));
61 }
62
63 InferenceEngine::Precision XMLParseUtils::GetPrecisionAttr(const pugi::xml_node &node, const char *str) {
64     auto attr = node.attribute(str);
65     if (attr.empty())
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());
69 }
70
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());
76 }
77
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());
82 }
83
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());
88     if (value < 0)
89         THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
90                            << node.offset_debug();
91     return static_cast<uint64_t>(value);
92 }
93
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());
98     if (value < 0)
99         THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
100                            << node.offset_debug();
101     return static_cast<unsigned int>(value);
102 }
103
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()));
108 }
109
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());
114 }
115
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);
123     } else {
124         baseName = baseName.substr(slashPos);
125     }
126     return baseName;
127 }
128