Commit 735cd3b1 authored by aligungr's avatar aligungr

AssertNodeName added to common utils

parent dd943245
...@@ -8,11 +8,11 @@ ...@@ -8,11 +8,11 @@
#include "common.hpp" #include "common.hpp"
#include <atomic> #include <atomic>
#include <cassert>
#include <chrono> #include <chrono>
#include <random> #include <random>
#include <regex> #include <regex>
#include <sstream> #include <sstream>
#include <stdexcept>
#include <thread> #include <thread>
#include <unistd.h> #include <unistd.h>
...@@ -273,3 +273,25 @@ bool utils::IsRoot() ...@@ -273,3 +273,25 @@ bool utils::IsRoot()
{ {
return geteuid() == 0; return geteuid() == 0;
} }
void utils::AssertNodeName(const std::string &str)
{
if (str.length() <= 2)
throw std::runtime_error("Node name assertion failed: string'" + str + "' is too short");
if (str.length() > 1024)
throw std::runtime_error("Node name assertion failed: string'" + str + "' is too long");
for (char c : str)
{
if (c >= '0' && c <= '9')
continue;
if (c >= 'a' && c <= 'z')
continue;
if (c >= 'A' && c <= 'Z')
continue;
if (c == '-' || c == '_')
continue;
throw std::runtime_error("Node name assertion failed: string '" + str +
"' contains illegal character: " + std::string(1, c));
}
}
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "octet.hpp" #include "octet.hpp"
#include "octet_string.hpp" #include "octet_string.hpp"
#include "time_stamp.hpp" #include "time_stamp.hpp"
#include <iomanip>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -31,6 +32,7 @@ int ParseInt(const char *str); ...@@ -31,6 +32,7 @@ int ParseInt(const char *str);
uint64_t Random64(); uint64_t Random64();
void Sleep(int ms); void Sleep(int ms);
bool IsRoot(); bool IsRoot();
void AssertNodeName(const std::string &str);
template <typename T> template <typename T>
inline void ClearAndDelete(std::vector<T *> &vector) inline void ClearAndDelete(std::vector<T *> &vector)
...@@ -40,4 +42,12 @@ inline void ClearAndDelete(std::vector<T *> &vector) ...@@ -40,4 +42,12 @@ inline void ClearAndDelete(std::vector<T *> &vector)
vector.clear(); vector.clear();
} }
template <typename T>
static std::string IntToHex(T i)
{
std::stringstream stream;
stream << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex << i;
return stream.str();
}
} // namespace utils } // namespace utils
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment