Mercurial > thymian
comparison common/common.h @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 #ifndef COMMON_H | |
| 2 #define COMMON_H | |
| 3 | |
| 4 #include <string> | |
| 5 #include <vector> | |
| 6 #include <sstream> | |
| 7 #include <iomanip> | |
| 8 #include <functional> | |
| 9 #include <random> | |
| 10 #include <iterator> | |
| 11 #include <algorithm> | |
| 12 #include <stdlib.h> | |
| 13 | |
| 14 namespace | |
| 15 { | |
| 16 static const std::string SLASH = "/"; | |
| 17 static const std::string HOSTT_WINDOWS = "WINDOWS"; | |
| 18 static const std::string HOSTT_LINUX = "LINUX"; | |
| 19 static const std::string HOSTT_ANDROID = "ANDROID"; | |
| 20 static const std::string HOSTT_AUTHENTICATOR = "AUTHENTICATOR"; | |
| 21 | |
| 22 static const std::string HOSTT_UNKNOWN = "UNKNOWN"; | |
| 23 } | |
| 24 /* Functions and their implementation */ | |
| 25 namespace unafrog { | |
| 26 | |
| 27 std::string server(); | |
| 28 | |
| 29 namespace utils { | |
| 30 | |
| 31 // trim from start | |
| 32 static inline std::string &sltrim(std::string &s) { | |
| 33 s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace)))); | |
| 34 return s; | |
| 35 } | |
| 36 | |
| 37 // trim from end | |
| 38 static inline std::string &srtrim(std::string &s) { | |
| 39 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end()); | |
| 40 return s; | |
| 41 } | |
| 42 | |
| 43 // trim from both ends | |
| 44 static inline std::string &trim(std::string &s) { | |
| 45 return sltrim(srtrim(s)); | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * Converts the given hex string to a number | |
| 50 **/ | |
| 51 template <class T> | |
| 52 T hex_string_to_nr(const std::string& s) | |
| 53 { | |
| 54 T ret = 0; | |
| 55 for (unsigned int i = 0; i < s.length(); i += 2) | |
| 56 { | |
| 57 ret <<= 8; | |
| 58 std::string byteString = s.substr(i, 2); | |
| 59 uint8_t byte = static_cast<uint8_t>(strtol(byteString.c_str(), NULL, 16)); | |
| 60 ret |= byte; | |
| 61 } | |
| 62 return ret; | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Converts the parameter to string. Used since android has no std::to_string | |
| 67 **/ | |
| 68 template<class T1> | |
| 69 std::string to_string(const T1& v) | |
| 70 { | |
| 71 std::stringstream ss; | |
| 72 ss << v; | |
| 73 return ss.str(); | |
| 74 } | |
| 75 | |
| 76 /** | |
| 77 * To get a type which can be used in a stringstream to be used as int if type is too small (char) | |
| 78 */ | |
| 79 template <class T> struct typefinder { using type = T; }; | |
| 80 template<> struct typefinder<char> { using type = int; }; | |
| 81 template<> struct typefinder<unsigned char> { using type = int; }; | |
| 82 | |
| 83 /** | |
| 84 * Converts the given number to a string. The number must be integral type, floating | |
| 85 * point conversion is deleted by default. | |
| 86 **/ | |
| 87 template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type> std::string int_to_hex( T i ) | |
| 88 { | |
| 89 std::stringstream stream; | |
| 90 stream << std::setfill ('0') << std::setw(sizeof(T)*2) | |
| 91 << std::hex << static_cast<typename typefinder<T>::type>(i); | |
| 92 return stream.str(); | |
| 93 } | |
| 94 | |
| 95 /** Converts the given hex string to the vector of bytes */ | |
| 96 std::vector<uint8_t> hex_string_to_vector(const std::string& s); | |
| 97 | |
| 98 /** | |
| 99 * Convrets the given input string to a hex string. | |
| 100 * Return the hex string, lowercase | |
| 101 **/ | |
| 102 std::string hex_to_string(const std::string& input); | |
| 103 | |
| 104 /** | |
| 105 * Converts the given hext string (as generated by hex_to_string) to the original string | |
| 106 */ | |
| 107 std::string string_to_hex(const std::string& input); | |
| 108 | |
| 109 /** | |
| 110 * Makes a URL with the given arguments | |
| 111 **/ | |
| 112 template<class Arg> | |
| 113 std::string make_url(Arg arg) | |
| 114 { | |
| 115 return to_string(arg); | |
| 116 } | |
| 117 | |
| 118 template<class Arg, class... Args> | |
| 119 std::string make_url(Arg a, Args... args) | |
| 120 { | |
| 121 std::string res = to_string(a) + SLASH + make_url(args...); | |
| 122 return res; | |
| 123 } | |
| 124 | |
| 125 /** | |
| 126 * Will remove all HTML tags from the string and transform the HTML special characters into their equivalent | |
| 127 **/ | |
| 128 std::string sanitize_user_input(const std::string& s, bool remove_domains = true); | |
| 129 | |
| 130 /** | |
| 131 * Will sanitize the hostname, ir. remove the - and . characters and replace them with "_" | |
| 132 */ | |
| 133 std::string sanitize_hostname_web(std::string hn); | |
| 134 | |
| 135 /** | |
| 136 * consumes the given number of characters from the strings' beginning, throws if out of range | |
| 137 **/ | |
| 138 std::string consume(std::string& s, int c); | |
| 139 | |
| 140 /** | |
| 141 * Grows the given string to the required length. | |
| 142 */ | |
| 143 std::string grow(const std::string& s, std::size_t required_length); | |
| 144 | |
| 145 /* The b62 namespace contains functions related to B62 encoding/decoding */ | |
| 146 namespace b62 { | |
| 147 | |
| 148 /** | |
| 149 * base62 decodes the given string, returns the number | |
| 150 */ | |
| 151 uint64_t base62_decode (const std::string &str); | |
| 152 | |
| 153 /** | |
| 154 * encodes the given number into base62 | |
| 155 **/ | |
| 156 std::string base62_encode (uint64_t val); | |
| 157 | |
| 158 } //b62 | |
| 159 | |
| 160 /* The random namespace has function for getting random data */ | |
| 161 namespace random { | |
| 162 | |
| 163 /* The class for the characters to be used in the string */ | |
| 164 enum class random_string_class | |
| 165 { | |
| 166 RSC_HEX = 0, | |
| 167 RSC_B64 = 1, | |
| 168 RSC_FULL = 2, | |
| 169 RSC_ASC_DEC = 3, | |
| 170 RSC_DEC = 4, | |
| 171 }; | |
| 172 | |
| 173 | |
| 174 /** | |
| 175 * Will generate a random string with the given charaters from the class | |
| 176 **/ | |
| 177 std::string random_string(size_t length , random_string_class cls = unafrog::utils::random::random_string_class::RSC_ASC_DEC); | |
| 178 | |
| 179 template<typename Iter, typename RandomGenerator> | |
| 180 Iter random_element(Iter start, Iter end, RandomGenerator& g) | |
| 181 { | |
| 182 std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1); | |
| 183 std::advance(start, dis(g)); | |
| 184 return start; | |
| 185 } | |
| 186 | |
| 187 template<typename Iter> | |
| 188 Iter random_element(Iter start, Iter end) | |
| 189 { | |
| 190 static std::random_device rd; | |
| 191 static std::mt19937 gen(rd()); | |
| 192 return random_element(start, end, gen); | |
| 193 } | |
| 194 | |
| 195 template<typename C> | |
| 196 C random_element(const std::vector<C>& v) | |
| 197 { | |
| 198 return *random_element(v.begin(), v.end()); | |
| 199 } | |
| 200 | |
| 201 } // random | |
| 202 | |
| 203 | |
| 204 std::string to_upper(const std::string& s); | |
| 205 | |
| 206 template <typename Arg, typename... Args> | |
| 207 void redirect_stream(std::ostream& out, char sep, | |
| 208 Arg&& arg, Args&&... args) | |
| 209 { | |
| 210 out << std::forward<Arg>(arg); | |
| 211 ((out << sep << std::forward<Args>(args)), ...); | |
| 212 } | |
| 213 | |
| 214 template <typename... Args> | |
| 215 std::string join_string(char sep, Args&&... args) | |
| 216 { | |
| 217 std::stringstream ss; | |
| 218 redirect_stream(ss, sep, std::forward<Args>(args)...); | |
| 219 return ss.str(); | |
| 220 } | |
| 221 | |
| 222 }} // unafrog::utils | |
| 223 | |
| 224 /** | |
| 225 * Will be used to compare string case insensitively | |
| 226 **/ | |
| 227 struct case_insensitive_str_eq | |
| 228 { | |
| 229 case_insensitive_str_eq(std::string key); | |
| 230 bool operator()(const std::string& item) const; | |
| 231 std::string key_; | |
| 232 }; | |
| 233 | |
| 234 /** | |
| 235 * Converts the given enum class to its base type | |
| 236 */ | |
| 237 template <typename E> | |
| 238 constexpr typename std::underlying_type<E>::type basetype(E e) noexcept | |
| 239 { | |
| 240 return static_cast<typename std::underlying_type<E>::type>(e); | |
| 241 } | |
| 242 | |
| 243 const std::string platform(); | |
| 244 | |
| 245 /** will join the strings in the vector with the given delimiter */ | |
| 246 std::string join(const std::vector<std::string>& vec, const char* delim); | |
| 247 | |
| 248 /** will split the string into substrings, delimited by delim */ | |
| 249 std::vector<std::string> split(const std::string&, const char *delim); | |
| 250 | |
| 251 /** will run a piece of code when the scope is left */ | |
| 252 template<class F> | |
| 253 class scope_exit_runner | |
| 254 { | |
| 255 public: | |
| 256 scope_exit_runner(F f) : m_f(f) {}; | |
| 257 scope_exit_runner(F f, bool run) : m_f(f), m_really_run(run) {}; | |
| 258 virtual ~scope_exit_runner() { if(m_really_run) m_f(); } | |
| 259 void invalidate() { m_really_run = false; } | |
| 260 void validate() { m_really_run = true; } | |
| 261 | |
| 262 template<typename T> | |
| 263 void operator()(T s) | |
| 264 { | |
| 265 m_f(s); | |
| 266 } | |
| 267 | |
| 268 private: | |
| 269 F m_f; | |
| 270 bool m_really_run = true; | |
| 271 }; | |
| 272 | |
| 273 template<class F> | |
| 274 class simple_runner | |
| 275 { | |
| 276 public: | |
| 277 simple_runner(F f) : m_f(f) {} | |
| 278 template<typename T> | |
| 279 simple_runner(F f, T t) : m_f(f) | |
| 280 { | |
| 281 m_f(t); | |
| 282 } | |
| 283 | |
| 284 template<typename T> | |
| 285 void operator()(T t) | |
| 286 { | |
| 287 m_f(t); | |
| 288 } | |
| 289 | |
| 290 private: | |
| 291 F m_f; | |
| 292 | |
| 293 }; | |
| 294 | |
| 295 /* just to signal an error */ | |
| 296 unsigned internalServerError(); | |
| 297 | |
| 298 /* remove the duplicates from a string */ | |
| 299 std::string remove_duplicates(std::string s, char to_remove); | |
| 300 | |
| 301 /* removes the quotes from the string */ | |
| 302 void remove_quotes(std::string &s); | |
| 303 | |
| 304 | |
| 305 template<std::size_t I = 0, typename FuncT, typename... Tp> | |
| 306 inline typename std::enable_if< I == sizeof...(Tp), void>::type | |
| 307 for_index(std::size_t, std::tuple<Tp...> &, FuncT) | |
| 308 {} | |
| 309 | |
| 310 template<std::size_t I = 0, typename FuncT, typename... Tp> | |
| 311 inline typename std::enable_if< I < sizeof...(Tp), void>::type | |
| 312 for_index(int index, std::tuple<Tp...>& t, FuncT f) | |
| 313 { | |
| 314 if (index == 0) f(std::get<I>(t)); | |
| 315 for_index<I + 1, FuncT, Tp...>(index-1, t, f); | |
| 316 } | |
| 317 | |
| 318 template <class ...Vals> | |
| 319 std::tuple<Vals...> valset(Vals... v) | |
| 320 { | |
| 321 return std::make_tuple(v...); | |
| 322 } | |
| 323 | |
| 324 #endif | |
| 325 |
