map/das-dn/third_party/AdsLib/AdsDef.cpp

54 lines
1.2 KiB
C++
Raw Normal View History

2024-12-03 10:36:06 +08:00
// SPDX-License-Identifier: MIT
/**
Copyright (c) 2015 - 2022 Beckhoff Automation GmbH & Co. KG
*/
#include "AdsDef.h"
2024-12-05 11:04:47 +08:00
#include <ostream>
2024-12-03 10:36:06 +08:00
#include <sstream>
#include <stdlib.h>
#include <string.h>
bool operator<(const AmsNetId& lhs, const AmsNetId& rhs)
{
for (unsigned int i = 0; i < sizeof(rhs.b); ++i) {
if (lhs.b[i] != rhs.b[i]) {
return lhs.b[i] < rhs.b[i];
}
}
return false;
}
bool operator<(const AmsAddr& lhs, const AmsAddr& rhs)
{
if (memcmp(&lhs.netId, &rhs.netId, sizeof(lhs.netId))) {
return lhs.netId < rhs.netId;
}
return lhs.port < rhs.port;
}
std::ostream& operator<<(std::ostream& os, const AmsNetId& netId)
{
return os << std::dec << (int)netId.b[0] << '.' << (int)netId.b[1] << '.' << (int)netId.b[2] << '.' <<
(int)netId.b[3] << '.' << (int)netId.b[4] << '.' << (int)netId.b[5];
}
2024-12-05 11:04:47 +08:00
AmsNetId make_AmsNetId(const std::string& addr)
{
std::istringstream iss(addr);
std::string s;
size_t i = 0;
AmsNetId id {};
while ((i < sizeof(id.b)) && std::getline(iss, s, '.')) {
id.b[i] = atoi(s.c_str()) % 256;
++i;
}
if ((i != sizeof(id.b)) || std::getline(iss, s, '.')) {
memset(id.b, 0, sizeof(id.b));
}
return id;
2024-12-03 10:36:06 +08:00
}