3 #include "includes.ihh" 15 inline std::string
decode64(
const std::string &val)
18 throw std::runtime_error(
"empty string param");
20 using namespace boost::archive::iterators;
21 using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
22 return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)),
24 [](
char c) {
return c ==
'\0';});
30 inline std::string
encode64(
const std::string &val)
33 throw std::runtime_error(
"empty string param");
35 using namespace boost::archive::iterators;
36 using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
37 auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
38 return tmp.append((3 - val.size() % 3) % 3,
'=');
44 std::string chars(
"abcdefghijklmnopqrstuvwxyz" 45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 47 std::random_device rng;
48 std::mt19937 gen(rng());
52 std::uniform_int_distribution<> index_dist(0, (chars.size() - 1));
53 for (
int i = 0; i < 16; ++i){
54 uid.push_back(chars[index_dist(rng)]);
64 throw std::runtime_error(
"empty string param");
66 std::ostringstream ss;
67 for (
auto iter = str.cbegin(); iter != str.cend(); iter++) {
69 case '\\': ss <<
"\\\\";
break;
70 case '"': ss <<
"\\\"";
break;
71 case '/': ss <<
"\\/";
break;
72 case '\b': ss <<
"\\b";
break;
73 case '\f': ss <<
"\\f";
break;
74 case '\n': ss <<
"\\n";
break;
75 case '\r': ss <<
"\\r";
break;
76 case '\t': ss <<
"\\t";
break;
77 default: ss << *iter;
break;
88 template <
class F,
class... Args>
91 using swallow =
int[];
92 (void)swallow{0, (void(f(std::forward<Args>(args))), 0)...};
101 auto it = json_f.find(key);
102 if (it == json_f.end()) {
103 throw std::runtime_error(
"key " + key +
" not found");
110 nlohmann::json & json,
111 const std::string json_str
115 json = nlohmann::json::parse(json_str);
117 catch (std::exception & e) {
118 std::cerr << e.what() << std::endl;
127 const nlohmann::json json,
128 const std::string error_str =
"error" 131 auto error = misc::get_json_value<std::string>(error_str, json);
132 if (!error.empty()) {
133 std::cerr <<
"error JSON: " << error <<std::endl;
143 std::string filename)
145 std::string temporary =
"tmp";
146 std::ofstream tmp(temporary);
150 std::remove(temporary.c_str());
151 return result.
save(filename);
158 #if __cplusplus==201103L 160 template<
typename T,
typename... Args>
161 std::unique_ptr<T> make_unique(Args&&... args)
163 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
bool check_json(nlohmann::json &json, const std::string json_str)
method to check if JSON data is correct
Definition: misc.hpp:109
T get_json_value(const std::string key, const nlohmann::json &json_f)
method to check if the string exists in the actual json
Definition: misc.hpp:99
bool save_dec_image(std::string data, std::string filename)
take decoded data and save it into an image
Definition: misc.hpp:142
Definition: asio_handler.hpp:14
void for_each_arg(F &&f, Args &&...args)
expand variadic arguments using a fold expression
Definition: misc.hpp:89
bool check_error(const nlohmann::json json, const std::string error_str="error")
function to check if an error has been received from
Definition: misc.hpp:126
class which wraps around raw bytes of a picture
Definition: picture.hpp:17
bool save(const std::string filepath)
Save picture to filepath.
std::string escape_string(const std::string &str)
escape JSON strings when sending them over the socket
Definition: misc.hpp:61
std::string decode64(const std::string &val)
decode base64
Definition: misc.hpp:15
std::string encode64(const std::string &val)
encode base64
Definition: misc.hpp:30
std::string random_boundary()
Create a random boundary for the multipart/form in HTTP.
Definition: misc.hpp:42