misc.hpp
1 #ifndef NOOS_MISC
2 #define NOOS_MISC
3 #include "includes.ihh"
4 namespace noos {
11 namespace misc {
12 
15 inline std::string decode64(const std::string &val)
16 {
17  if (val.empty()) {
18  throw std::runtime_error("empty string param");
19  }
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)),
23  It(std::end(val))),
24  [](char c) {return c == '\0';});
25 }
26 
30 inline std::string encode64(const std::string &val)
31 {
32  if (val.empty()) {
33  throw std::runtime_error("empty string param");
34  }
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, '=');
39 }
40 
42 inline std::string random_boundary()
43 {
44  std::string chars("abcdefghijklmnopqrstuvwxyz"
45  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46  "1234567890");
47  std::random_device rng;
48  std::mt19937 gen(rng());
49  std::string uid;
50 
51  // Randomly chose 16 characters
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)]);
55  }
56  return uid;
57 }
58 
61 inline std::string escape_string(const std::string & str)
62 {
63  if (str.empty()) {
64  throw std::runtime_error("empty string param");
65  }
66  std::ostringstream ss;
67  for (auto iter = str.cbegin(); iter != str.cend(); iter++) {
68  switch (*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;
78  }
79  }
80  return ss.str();
81 }
82 
88 template <class F, class... Args>
89 void for_each_arg(F&& f, Args&&... args)
90 {
91  using swallow = int[];
92  (void)swallow{0, (void(f(std::forward<Args>(args))), 0)...};
93 }
94 
98 template<typename T>
99 T get_json_value(const std::string key, const nlohmann::json & json_f)
100 {
101  auto it = json_f.find(key);
102  if (it == json_f.end()) {
103  throw std::runtime_error("key " + key + " not found");
104  }
105  return it->get<T>();
106 }
107 
109 inline bool check_json(
110  nlohmann::json & json,
111  const std::string json_str
112  )
113 {
114  try {
115  json = nlohmann::json::parse(json_str);
116  }
117  catch (std::exception & e) {
118  std::cerr << e.what() << std::endl;
119  return false;
120  }
121  return true;
122 }
123 
125 // the platform
126 inline bool check_error(
127  const nlohmann::json json,
128  const std::string error_str = "error"
129  )
130 {
131  auto error = misc::get_json_value<std::string>(error_str, json);
132  if (!error.empty()) {
133  std::cerr << "error JSON: " << error <<std::endl;
134  return false;
135  }
136  return true;
137 }
138 
142 inline bool save_dec_image(std::string data,
143  std::string filename)
144 {
145  std::string temporary = "tmp";
146  std::ofstream tmp(temporary);
147  tmp << misc::decode64(data);
148  tmp.close();
149  noos::object::picture result(temporary);
150  std::remove(temporary.c_str());
151  return result.save(filename);
152 }
153 
154 }
155 }
156 
157 // c++11 - add `std::make_unique`
158 #if __cplusplus==201103L
159 namespace std {
160 template<typename T, typename... Args>
161 std::unique_ptr<T> make_unique(Args&&... args)
162 {
163  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
164 }
165 }
166 #endif
167 
168 //
169 #endif
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
STL namespace.
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