2015-05-11 00:18:22 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
template <>
|
2015-10-01 22:09:55 +02:00
|
|
|
int getJson(const Json::Value &root, const std::string &key)
|
2015-05-11 00:18:22 +02:00
|
|
|
{
|
|
|
|
auto val = root.get(key, Json::Value());
|
|
|
|
if (val.isInt())
|
|
|
|
return val.asInt();
|
2015-09-25 00:13:30 +02:00
|
|
|
throw std::runtime_error("Json Type error");
|
2015-05-11 00:18:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2015-10-01 22:09:55 +02:00
|
|
|
std::string getJson(const Json::Value &root, const std::string &key)
|
2015-05-11 00:18:22 +02:00
|
|
|
{
|
|
|
|
auto val = root.get(key, Json::Value());
|
|
|
|
if (val.isString())
|
|
|
|
return val.asString();
|
2015-09-25 00:13:30 +02:00
|
|
|
throw std::runtime_error("Json Type error");
|
2015-05-11 00:18:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2015-10-01 22:09:55 +02:00
|
|
|
size_t getJson(const Json::Value &root, const std::string &key)
|
2015-05-11 00:18:22 +02:00
|
|
|
{
|
|
|
|
auto val = root.get(key, Json::Value());
|
|
|
|
if (val.isInt())
|
|
|
|
return val.asUInt64();
|
2015-09-25 00:13:30 +02:00
|
|
|
throw std::runtime_error("Json Type error");
|
2015-05-11 00:18:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2015-10-01 22:09:55 +02:00
|
|
|
bool getJson(const Json::Value &root, const std::string &key)
|
2015-05-11 00:18:22 +02:00
|
|
|
{
|
|
|
|
auto val = root.get(key, Json::Value());
|
|
|
|
if (val.isBool())
|
|
|
|
return val.asBool();
|
2015-09-25 00:13:30 +02:00
|
|
|
throw std::runtime_error("Json Type error");
|
2015-05-11 00:18:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2015-10-01 22:09:55 +02:00
|
|
|
Json::Value getJson(const Json::Value &root, const std::string &key)
|
2015-05-11 00:18:22 +02:00
|
|
|
{
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2016-04-03 17:03:50 +02:00
|
|
|
std::string randHexString(unsigned int len)
|
2015-05-11 00:18:22 +02:00
|
|
|
{
|
2015-10-01 22:09:55 +02:00
|
|
|
std::string retval;
|
2016-04-03 17:03:50 +02:00
|
|
|
while (len--)
|
|
|
|
retval += nibble2hex(rand() & 0xF);
|
2015-05-11 00:18:22 +02:00
|
|
|
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;
|
|
|
|
}
|
2015-09-25 00:13:30 +02:00
|
|
|
throw std::runtime_error("Malformed hexadecimal input");
|
2015-05-11 00:18:22 +02:00
|
|
|
}
|