forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_parse_utils.h
301 lines (265 loc) · 9.38 KB
/
xml_parse_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief Basic functions to safely extract values from `pugi::xml_node` and open `pugi::xml_document`
* @file xml_parse_utils.h
*/
#pragma once
#include <cstdlib>
#include <fstream>
#include <memory>
#include <pugixml.hpp>
#include <sstream>
#include <string>
#include <utility>
#include "file_utils.h"
#include "ie_api.h"
#include "ie_common.h"
#include "ie_precision.hpp"
IE_SUPPRESS_DEPRECATED_START
/**
* @ingroup ie_dev_api_xml
* @brief Defines convinient for-each based cycle to iterate over node children
*
* @param c Child node name
* @param p Parent node name
* @param tag The tag represented as a string value
*/
#define FOREACH_CHILD(c, p, tag) for (auto c = p.child(tag); !c.empty(); c = c.next_sibling(tag))
/**
* @brief XML helpers function to extract values from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*/
namespace pugixml {
/**
* @brief XML helpers function to extract values from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*/
namespace utils {
/**
* @brief Gets the integer attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string
* @return An integer value
*/
INFERENCE_ENGINE_API_CPP(int) GetIntAttr(const pugi::xml_node& node, const char* str);
/**
* @brief Gets the integer attribute from `pugi::xml_node`
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @param[in] defVal The default value
* @return An integer value
*/
INFERENCE_ENGINE_API_CPP(int) GetIntAttr(const pugi::xml_node& node, const char* str, int defVal);
/**
* @brief Gets the `int64_t` attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @return An `int64_t` value
*/
INFERENCE_ENGINE_API_CPP(int64_t) GetInt64Attr(const pugi::xml_node& node, const char* str);
/**
* @brief Gets the `int64_t` attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @param[in] defVal The default value
* @return An `int64_t` value
*/
INFERENCE_ENGINE_API_CPP(int64_t) GetInt64Attr(const pugi::xml_node& node, const char* str, int64_t defVal);
/**
* @brief Gets the `uint64_t` attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @return An `uint64_t` value
*/
INFERENCE_ENGINE_API_CPP(uint64_t) GetUInt64Attr(const pugi::xml_node& node, const char* str);
/**
* @brief Gets the `uint64_t` attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @param[in] defVal The default value
* @return An `uint64_t` value
*/
INFERENCE_ENGINE_API_CPP(uint64_t) GetUInt64Attr(const pugi::xml_node& node, const char* str, uint64_t defVal);
/**
* @brief Gets the unsigned integer attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @return An unsigned integer value
*/
INFERENCE_ENGINE_API_CPP(unsigned int) GetUIntAttr(const pugi::xml_node& node, const char* str);
/**
* @brief Gets the unsigned integer attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @param[in] defVal The default value
* @return An unsigned integer value
*/
INFERENCE_ENGINE_API_CPP(unsigned int) GetUIntAttr(const pugi::xml_node& node, const char* str, unsigned int defVal);
/**
* @brief Gets the string attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @return A string value
*/
INFERENCE_ENGINE_API_CPP(std::string) GetStrAttr(const pugi::xml_node& node, const char* str);
/**
* @brief Gets the string attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @param[in] def The default value
* @return A string value
*/
INFERENCE_ENGINE_API_CPP(std::string) GetStrAttr(const pugi::xml_node& node, const char* str, const char* def);
/**
* @brief Gets the bool attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @return A boolean value
*/
INFERENCE_ENGINE_API_CPP(bool) GetBoolAttr(const pugi::xml_node& node, const char* str);
/**
* @brief Gets the bool attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @param[in] def The default value
* @return A boolean value
*/
INFERENCE_ENGINE_API_CPP(bool) GetBoolAttr(const pugi::xml_node& node, const char* str, const bool def);
/**
* @brief Gets the float attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @return A single-precision floating point value
*/
INFERENCE_ENGINE_API_CPP(float) GetFloatAttr(const pugi::xml_node& node, const char* str);
/**
* @brief Gets the float attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @param[in] defVal The default value
* @return A single-precision floating point value
*/
INFERENCE_ENGINE_API_CPP(float) GetFloatAttr(const pugi::xml_node& node, const char* str, float defVal);
/**
* @brief Gets the Precision attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @return A Precision value
*/
INFERENCE_ENGINE_API_CPP(InferenceEngine::Precision) GetPrecisionAttr(const pugi::xml_node& node, const char* str);
/**
* @brief Gets the Precision attribute from `pugi::xml_node`
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string identifying value name
* @param[in] def The default value
* @return A Precision value
*/
INFERENCE_ENGINE_API_CPP(InferenceEngine::Precision)
GetPrecisionAttr(const pugi::xml_node& node, const char* str, InferenceEngine::Precision def);
/**
* @brief Gets the integer value located in a child node.
* @ingroup ie_dev_api_xml
*
* @param[in] node The node
* @param[in] str The string value identifying a child node
* @param[in] defVal The default value
* @return An ingeter value located in a child node, @p devVal otherwise.
*/
INFERENCE_ENGINE_API_CPP(int) GetIntChild(const pugi::xml_node& node, const char* str, int defVal);
} // namespace utils
} // namespace pugixml
/**
* @brief A XML parse result structure with an error message and the `pugi::xml_document` document.
* @ingroup ie_dev_api_xml
*/
struct parse_result {
/**
* @brief Constructs parse_result with `pugi::xml_document` and an error message
*
* @param xml The `pugi::xml_document`
* @param[in] error_msg The error message
*/
parse_result(std::unique_ptr<pugi::xml_document>&& xml, std::string error_msg)
: xml(std::move(xml)),
error_msg(std::move(error_msg)) {}
/**
* @brief A XML document.
*/
std::unique_ptr<pugi::xml_document> xml;
/**
* @brief An error message
*/
std::string error_msg{};
};
/**
* @brief Parses a file and returns parse_result
* @ingroup ie_dev_api_xml
*
* @param[in] file_path The file path
*
* @return The parse_result.
*/
inline parse_result ParseXml(const char* file_path) {
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
std::wstring wFilePath = ov::util::string_to_wstring(file_path);
const wchar_t* resolvedFilepath = wFilePath.c_str();
#else
const char* resolvedFilepath = file_path;
#endif
try {
auto xml = std::unique_ptr<pugi::xml_document>{new pugi::xml_document{}};
const auto load_result = xml->load_file(resolvedFilepath);
const auto error_msg = [&]() -> std::string {
if (load_result.status == pugi::status_ok)
return {};
std::ifstream file_stream(file_path);
const auto file =
std::string(std::istreambuf_iterator<char>{file_stream}, std::istreambuf_iterator<char>{});
const auto error_offset = std::next(file.rbegin(), file.size() - load_result.offset);
const auto line_begin = std::find(error_offset, file.rend(), '\n');
const auto line = 1 + std::count(line_begin, file.rend(), '\n');
const auto pos = std::distance(error_offset, line_begin);
std::stringstream ss;
ss << "Error loading XML file: " << file_path << ":" << line << ":" << pos << ": "
<< load_result.description();
return ss.str();
}();
return {std::move(xml), error_msg};
} catch (std::exception& e) {
return {std::move(nullptr), std::string("Error loading XML file: ") + e.what()};
}
}
IE_SUPPRESS_DEPRECATED_END