mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-12-22 18:34:25 +01:00
49a5b88f6c
- Data type Token changed from uint64_t to std::string - Added new class "Request" that describes a JSON TCP request - Classes may now throw Responses for proper error handling - Removed JSON parsing from Logic - proper Error handling everywhere - Many small fixes - removed unnecessary includes - removed using namespace std everywhere
93 lines
2.3 KiB
C++
93 lines
2.3 KiB
C++
#include "util.h"
|
|
|
|
template <>
|
|
int getJson(const Json::Value &root, const std::string &key)
|
|
{
|
|
auto val = root.get(key, Json::Value());
|
|
if (val.isInt())
|
|
return val.asInt();
|
|
throw std::runtime_error("Json Type error");
|
|
}
|
|
|
|
template <>
|
|
std::string getJson(const Json::Value &root, const std::string &key)
|
|
{
|
|
auto val = root.get(key, Json::Value());
|
|
if (val.isString())
|
|
return val.asString();
|
|
throw std::runtime_error("Json Type error");
|
|
}
|
|
|
|
template <>
|
|
size_t getJson(const Json::Value &root, const std::string &key)
|
|
{
|
|
auto val = root.get(key, Json::Value());
|
|
if (val.isInt())
|
|
return val.asUInt64();
|
|
throw std::runtime_error("Json Type error");
|
|
}
|
|
|
|
template <>
|
|
bool getJson(const Json::Value &root, const std::string &key)
|
|
{
|
|
auto val = root.get(key, Json::Value());
|
|
if (val.isBool())
|
|
return val.asBool();
|
|
throw std::runtime_error("Json Type error");
|
|
}
|
|
|
|
template <>
|
|
Json::Value getJson(const Json::Value &root, const std::string &key)
|
|
{
|
|
auto val = root.get(key, Json::Value());
|
|
return val;
|
|
}
|
|
|
|
static char nibble2hex(unsigned char input)
|
|
{
|
|
input &= 0xf;
|
|
|
|
if(input <= 9)
|
|
{
|
|
return input + '0';
|
|
}
|
|
|
|
return input - 0xA + 'A';
|
|
}
|
|
|
|
std::string toHexString(const uint64_t c)
|
|
{
|
|
std::string retval;
|
|
|
|
retval = nibble2hex((c>>60) & 0xF);
|
|
retval += nibble2hex((c>>56) & 0xF);
|
|
retval += nibble2hex((c>>52) & 0xF);
|
|
retval += nibble2hex((c>>48) & 0xF);
|
|
retval += nibble2hex((c>>44) & 0xF);
|
|
retval += nibble2hex((c>>40) & 0xF);
|
|
retval += nibble2hex((c>>36) & 0xF);
|
|
retval += nibble2hex((c>>32) & 0xF);
|
|
retval += nibble2hex((c>>28) & 0xF);
|
|
retval += nibble2hex((c>>24) & 0xF);
|
|
retval += nibble2hex((c>>20) & 0xF);
|
|
retval += nibble2hex((c>>16) & 0xF);
|
|
retval += nibble2hex((c>>12) & 0xF);
|
|
retval += nibble2hex((c>> 8) & 0xF);
|
|
retval += nibble2hex((c>> 4) & 0xF);
|
|
retval += nibble2hex((c ) & 0xF);
|
|
|
|
return retval;
|
|
}
|
|
|
|
unsigned char hex2uchar(const char input)
|
|
{
|
|
if(input >= '0' && input <= '9') {
|
|
return input - '0';
|
|
} else if(input >= 'A' && input <= 'F') {
|
|
return input - 'A' + 10;
|
|
} else if(input >= 'a' && input <= 'f') {
|
|
return input - 'a' + 10;
|
|
}
|
|
throw std::runtime_error("Malformed hexadecimal input");
|
|
}
|