-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpoint.cpp
More file actions
52 lines (43 loc) · 1.29 KB
/
Copy pathpoint.cpp
File metadata and controls
52 lines (43 loc) · 1.29 KB
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
/*************************************************************************
> File Name: point.cpp
> Author: Netcan
> Blog: https://netcan.github.io/
> Mail: netcan1996@gmail.com
> Created Time: 2022-01-03 14:57
************************************************************************/
#include <tinyxml2.h>
#include <optional>
#include <iostream>
#include <string_view>
struct Point {
double x;
double y;
};
std::optional<Point> load_point(std::string_view xml_path) {
using namespace tinyxml2;
XMLDocument doc;
if (doc.LoadFile(xml_path.data()) != XML_SUCCESS) {
return std::nullopt;
}
auto root = doc.FirstChildElement("point");
if (! root) { return std::nullopt; }
Point res{};
if (auto x = root->FirstChildElement("x");
x == nullptr || x->QueryDoubleText(&res.x) != XML_SUCCESS) {
return std::nullopt;
}
if (auto y = root->FirstChildElement("y");
y == nullptr || y->QueryDoubleText(&res.y) != XML_SUCCESS) {
return std::nullopt;
}
return res;
}
int main(int argc, char** argv) {
auto p = load_point("point.xml");
if (p.has_value()) {
std::cout << "(" << p->x << ", " << p->y << ")\n";
} else {
std::cout << "load faild point" << std::endl;
}
return 0;
}