w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RethTools.cpp
Go to the documentation of this file.
1// $Id: RethTools.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2017- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2017-04-15 875 1.0 Initial version
8// 2017-02-04 849 0.1 First draft
9// ---------------------------------------------------------------------------
10
16#include "librtools/Rtools.hpp"
17
18#include "RethTools.hpp"
19
20using namespace std;
21
27// all method definitions in namespace Retro
28namespace Retro {
29namespace RethTools {
30
31//------------------------------------------+-----------------------------------
33
34std::string Mac2String(uint64_t mac)
35{
36 const char* codes = "0123456789abcdef";
37 string rval;
38 rval.reserve(3*6-1); // reserve expected length
39 for (int ib=0; ib<6; ib++) {
40 uint64_t byte = mac & 0xff;
41 mac = mac >> 8;
42 if (ib > 0) rval += ':';
43 rval += codes[(byte>>4) & 0x0f];
44 rval += codes[ byte & 0x0f];
45 }
46
47 return rval;
48}
49
50//------------------------------------------+-----------------------------------
52bool String2Mac(const std::string& str, uint64_t& mac, RerrMsg& emsg)
53{
54 if (str.length() != 17) {
55 emsg.Init("RethTools::String2Mac",
56 string("invalid string size for '") + str +"'");
57 return false;
58 }
59 mac = 0;
60 for (int ib=0; ib<6; ib++) {
61 unsigned long byte;
62 if (!Rtools::String2Long(str.substr(3*ib,2), byte, emsg, 16)) return false;
63 mac |= uint64_t(byte&0xff)<<(8*ib);
64 if (ib > 0) {
65 char delim = str[3*ib-1];
66 if (delim != ':' && delim != '-') {
67 emsg.Init("RethTools::String2Mac",
68 string("invalid delimiter '") + delim +"'");
69 return false;
70 }
71 }
72 }
73 return true;
74}
75
76//------------------------------------------+-----------------------------------
78uint64_t String2Mac(const std::string& str)
79{
80 uint64_t mac;
81 RerrMsg emsg;
82 if (!String2Mac(str, mac, emsg)) throw Rexception(emsg);
83 return mac;
84}
85
86} // end namespace RethTools
87} // end namespace Retro
FIXME_docs.
Definition: RerrMsg.hpp:25
void Init(const std::string &meth, const std::string &text)
FIXME_docs.
Definition: RerrMsg.cpp:74
FIXME_docs.
Definition: Rexception.hpp:29
bool String2Mac(const std::string &str, uint64_t &mac, RerrMsg &emsg)
FIXME_docs.
Definition: RethTools.cpp:52
std::string Mac2String(uint64_t mac)
FIXME_docs.
Definition: RethTools.cpp:34
bool String2Long(const std::string &str, long &res, RerrMsg &emsg, int base)
FIXME_docs.
Definition: Rtools.cpp:67
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47