Publishing R3
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / xml_parse_utils.cpp
1 // Copyright (C) 2018 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5
6 #include "xml_parse_utils.h"
7 #include "details/ie_exception.hpp"
8 #include "ie_precision.hpp"
9
10 int XMLParseUtils::GetIntAttr(const pugi::xml_node &node, const char *str) {
11     auto attr = node.attribute(str);
12     if (attr.empty())
13         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
14                            << node.offset_debug();
15     return atoi(attr.value());
16 }
17
18
19 unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *str) {
20     auto attr = node.attribute(str);
21     if (attr.empty())
22         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
23                            << node.offset_debug();
24     int value = atoi(attr.value());
25     if (value < 0)
26         THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
27                            << node.offset_debug();
28     return static_cast<unsigned int>(value);
29 }
30
31 std::string XMLParseUtils::GetStrAttr(const pugi::xml_node &node, const char *str) {
32     auto attr = node.attribute(str);
33     if (attr.empty())
34         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
35                            << node.offset_debug();
36     return attr.value();
37 }
38
39 std::string XMLParseUtils::GetStrAttr(const pugi::xml_node &node, const char *str, const char *def) {
40     auto attr = node.attribute(str);
41     if (attr.empty()) return def;
42     return attr.value();
43 }
44
45 float XMLParseUtils::GetFloatAttr(const pugi::xml_node &node, const char *str) {
46     auto attr = node.attribute(str);
47     if (attr.empty())
48         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
49                            << node.offset_debug();
50     return static_cast<float>(atof(attr.value()));
51 }
52
53 InferenceEngine::Precision XMLParseUtils::GetPrecisionAttr(const pugi::xml_node &node, const char *str) {
54     auto attr = node.attribute(str);
55     if (attr.empty())
56         THROW_IE_EXCEPTION << "node <" << node.name() << "> is missing mandatory attribute: " << str << " at offset "
57                            << node.offset_debug();
58     return InferenceEngine::Precision::FromStr(attr.value());
59 }
60
61 InferenceEngine::Precision XMLParseUtils::GetPrecisionAttr(const pugi::xml_node &node, const char *str,
62                                                            InferenceEngine::Precision def) {
63     auto attr = node.attribute(str);
64     if (attr.empty()) return InferenceEngine::Precision(def);
65     return InferenceEngine::Precision::FromStr(attr.value());
66 }
67
68 int XMLParseUtils::GetIntAttr(const pugi::xml_node &node, const char *str, int defVal) {
69     auto attr = node.attribute(str);
70     if (attr.empty()) return defVal;
71     return atoi(attr.value());
72 }
73
74 unsigned int XMLParseUtils::GetUIntAttr(const pugi::xml_node &node, const char *str, unsigned int defVal) {
75     auto attr = node.attribute(str);
76     if (attr.empty()) return defVal;
77     int value = atoi(attr.value());
78     if (value < 0)
79         THROW_IE_EXCEPTION << "node <" << node.name() << "> has incorrect parameter: " << str << " at offset "
80                            << node.offset_debug();
81     return static_cast<unsigned int>(value);
82 }
83
84 float XMLParseUtils::GetFloatAttr(const pugi::xml_node &node, const char *str, float defVal) {
85     auto attr = node.attribute(str);
86     if (attr.empty()) return defVal;
87     return static_cast<float>(atof(attr.value()));
88 }
89
90 int XMLParseUtils::GetIntChild(const pugi::xml_node &node, const char *str, int defVal) {
91     auto child = node.child(str);
92     if (child.empty()) return defVal;
93     return atoi(child.child_value());
94 }
95
96 std::string XMLParseUtils::NameFromFilePath(const char *filepath) {
97     std::string baseName = filepath;
98     auto slashPos = baseName.rfind('/');
99     slashPos = slashPos == std::string::npos ? 0 : slashPos + 1;
100     auto dotPos = baseName.rfind('.');
101     if (dotPos != std::string::npos) {
102         baseName = baseName.substr(slashPos, dotPos - slashPos);
103     } else {
104         baseName = baseName.substr(slashPos);
105     }
106     return baseName;
107 }
108