forked from open62541pp/open62541pp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.cpp
151 lines (126 loc) · 4.53 KB
/
types.cpp
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
#include "open62541pp/types.hpp"
#include <ctime> // gmtime, localtime
#include <iomanip> // put_time
#include <ostream>
#include <sstream>
#include "open62541pp/config.hpp"
namespace opcua {
/* ------------------------------------------- String ------------------------------------------- */
std::ostream& operator<<(std::ostream& os, const String& str) {
os << static_cast<std::string_view>(str);
return os;
}
/* -------------------------------------------- Guid -------------------------------------------- */
std::string Guid::toString() const {
// <Data1>-<Data2>-<Data3>-<Data4[0:2]>-<Data4[2:8]>
// each value is formatted as a hexadecimal number with padded zeros
std::ostringstream ss;
ss << std::hex << std::uppercase << std::setfill('0');
ss << std::setw(8) << handle()->data1 << "-";
ss << std::setw(4) << handle()->data2 << "-";
ss << std::setw(4) << handle()->data3 << "-";
for (size_t i = 0; i < 8; ++i) {
ss << std::setw(2) << static_cast<int>(handle()->data4[i]); // NOLINT
if (i == 1) {
ss << "-";
}
}
return ss.str();
}
std::ostream& operator<<(std::ostream& os, const Guid& guid) {
os << guid.toString();
return os;
}
/* ------------------------------------------ DateTime ------------------------------------------ */
std::string DateTime::format(std::string_view format, bool localtime) const {
const std::time_t unixTime = toUnixTime();
std::ostringstream ss;
const auto* timeinfo = localtime ? std::localtime(&unixTime) : std::gmtime(&unixTime);
if (timeinfo != nullptr) {
ss << std::put_time(timeinfo, std::string(format).c_str());
}
return ss.str();
}
/* ----------------------------------------- ByteString ----------------------------------------- */
ByteString ByteString::fromBase64([[maybe_unused]] std::string_view encoded) {
#if UAPP_OPEN62541_VER_GE(1, 1)
ByteString output;
UA_ByteString_fromBase64(output.handle(), String(encoded).handle());
return output;
#else
return {};
#endif
}
std::string ByteString::toBase64() const {
#if UAPP_OPEN62541_VER_GE(1, 1)
String output;
UA_ByteString_toBase64(handle(), output.handle());
return std::string(output);
#else
return {};
#endif
}
/* ----------------------------------------- XmlElement ----------------------------------------- */
std::ostream& operator<<(std::ostream& os, const XmlElement& xmlElement) {
os << static_cast<std::string_view>(xmlElement);
return os;
}
/* ---------------------------------------- NumericRange ---------------------------------------- */
NumericRange::NumericRange(std::string_view encodedRange) {
const auto encodedRangeNative = detail::toNativeString(encodedRange);
#if UAPP_OPEN62541_VER_GE(1, 1)
throwIfBad(UA_NumericRange_parse(handle(), encodedRangeNative));
#else
throwIfBad(UA_NumericRange_parseFromString(handle(), &encodedRangeNative));
#endif
}
std::string NumericRange::toString() const {
std::ostringstream ss;
for (const auto& dimension : dimensions()) {
ss << dimension.min;
if (dimension.min != dimension.max) {
ss << ':' << dimension.max;
}
ss << ',';
}
auto str = ss.str();
str.pop_back(); // remove last comma
return str;
}
/* ------------------------------------------- NodeId ------------------------------------------- */
std::string NodeId::toString() const {
std::string result;
if (const auto ns = namespaceIndex(); ns > 0) {
result.append("ns=").append(std::to_string(ns)).append(";");
}
switch (identifierType()) {
case NodeIdType::Numeric:
result.append("i=").append(std::to_string(identifier<uint32_t>()));
break;
case NodeIdType::String:
result.append("s=").append(identifier<String>());
break;
case NodeIdType::Guid:
result.append("g=").append(identifier<Guid>().toString());
break;
case NodeIdType::ByteString:
result.append("b=").append(identifier<ByteString>().toBase64());
break;
}
return result;
}
/* --------------------------------------- ExpandedNodeId --------------------------------------- */
std::string ExpandedNodeId::toString() const {
std::string result;
const auto svr = serverIndex();
if (svr > 0) {
result.append("svr=").append(std::to_string(svr)).append(";");
}
const auto nsu = namespaceUri();
if (!nsu.empty()) {
result.append("nsu=").append(nsu).append(";");
}
result.append(nodeId().toString());
return result;
}
} // namespace opcua