ferencd@0: #ifndef COMMON_H ferencd@0: #define COMMON_H ferencd@0: ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: #include ferencd@0: ferencd@0: namespace ferencd@0: { ferencd@0: static const std::string SLASH = "/"; ferencd@0: static const std::string HOSTT_WINDOWS = "WINDOWS"; ferencd@0: static const std::string HOSTT_LINUX = "LINUX"; ferencd@0: static const std::string HOSTT_ANDROID = "ANDROID"; ferencd@0: static const std::string HOSTT_AUTHENTICATOR = "AUTHENTICATOR"; ferencd@0: ferencd@0: static const std::string HOSTT_UNKNOWN = "UNKNOWN"; ferencd@0: } ferencd@0: /* Functions and their implementation */ ferencd@0: namespace unafrog { ferencd@0: ferencd@0: std::string server(); ferencd@0: ferencd@0: namespace utils { ferencd@0: ferencd@0: // trim from start ferencd@0: static inline std::string &sltrim(std::string &s) { ferencd@0: s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(std::isspace)))); ferencd@0: return s; ferencd@0: } ferencd@0: ferencd@0: // trim from end ferencd@0: static inline std::string &srtrim(std::string &s) { ferencd@0: s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(std::isspace))).base(), s.end()); ferencd@0: return s; ferencd@0: } ferencd@0: ferencd@0: // trim from both ends ferencd@0: static inline std::string &trim(std::string &s) { ferencd@0: return sltrim(srtrim(s)); ferencd@0: } ferencd@0: ferencd@0: /** ferencd@0: * Converts the given hex string to a number ferencd@0: **/ ferencd@0: template ferencd@0: T hex_string_to_nr(const std::string& s) ferencd@0: { ferencd@0: T ret = 0; ferencd@0: for (unsigned int i = 0; i < s.length(); i += 2) ferencd@0: { ferencd@0: ret <<= 8; ferencd@0: std::string byteString = s.substr(i, 2); ferencd@0: uint8_t byte = static_cast(strtol(byteString.c_str(), NULL, 16)); ferencd@0: ret |= byte; ferencd@0: } ferencd@0: return ret; ferencd@0: } ferencd@0: ferencd@0: /** ferencd@0: * Converts the parameter to string. Used since android has no std::to_string ferencd@0: **/ ferencd@0: template ferencd@0: std::string to_string(const T1& v) ferencd@0: { ferencd@0: std::stringstream ss; ferencd@0: ss << v; ferencd@0: return ss.str(); ferencd@0: } ferencd@0: ferencd@0: /** ferencd@0: * To get a type which can be used in a stringstream to be used as int if type is too small (char) ferencd@0: */ ferencd@0: template struct typefinder { using type = T; }; ferencd@0: template<> struct typefinder { using type = int; }; ferencd@0: template<> struct typefinder { using type = int; }; ferencd@0: ferencd@0: /** ferencd@0: * Converts the given number to a string. The number must be integral type, floating ferencd@0: * point conversion is deleted by default. ferencd@0: **/ ferencd@0: template::value>::type> std::string int_to_hex( T i ) ferencd@0: { ferencd@0: std::stringstream stream; ferencd@0: stream << std::setfill ('0') << std::setw(sizeof(T)*2) ferencd@0: << std::hex << static_cast::type>(i); ferencd@0: return stream.str(); ferencd@0: } ferencd@0: ferencd@0: /** Converts the given hex string to the vector of bytes */ ferencd@0: std::vector hex_string_to_vector(const std::string& s); ferencd@0: ferencd@0: /** ferencd@0: * Convrets the given input string to a hex string. ferencd@0: * Return the hex string, lowercase ferencd@0: **/ ferencd@0: std::string hex_to_string(const std::string& input); ferencd@0: ferencd@0: /** ferencd@0: * Converts the given hext string (as generated by hex_to_string) to the original string ferencd@0: */ ferencd@0: std::string string_to_hex(const std::string& input); ferencd@0: ferencd@0: /** ferencd@0: * Makes a URL with the given arguments ferencd@0: **/ ferencd@0: template ferencd@0: std::string make_url(Arg arg) ferencd@0: { ferencd@0: return to_string(arg); ferencd@0: } ferencd@0: ferencd@0: template ferencd@0: std::string make_url(Arg a, Args... args) ferencd@0: { ferencd@0: std::string res = to_string(a) + SLASH + make_url(args...); ferencd@0: return res; ferencd@0: } ferencd@0: ferencd@0: /** ferencd@0: * Will remove all HTML tags from the string and transform the HTML special characters into their equivalent ferencd@0: **/ ferencd@0: std::string sanitize_user_input(const std::string& s, bool remove_domains = true); ferencd@0: ferencd@0: /** ferencd@0: * Will sanitize the hostname, ir. remove the - and . characters and replace them with "_" ferencd@0: */ ferencd@0: std::string sanitize_hostname_web(std::string hn); ferencd@0: ferencd@0: /** ferencd@0: * consumes the given number of characters from the strings' beginning, throws if out of range ferencd@0: **/ ferencd@0: std::string consume(std::string& s, int c); ferencd@0: ferencd@0: /** ferencd@0: * Grows the given string to the required length. ferencd@0: */ ferencd@0: std::string grow(const std::string& s, std::size_t required_length); ferencd@0: ferencd@0: /* The b62 namespace contains functions related to B62 encoding/decoding */ ferencd@0: namespace b62 { ferencd@0: ferencd@0: /** ferencd@0: * base62 decodes the given string, returns the number ferencd@0: */ ferencd@0: uint64_t base62_decode (const std::string &str); ferencd@0: ferencd@0: /** ferencd@0: * encodes the given number into base62 ferencd@0: **/ ferencd@0: std::string base62_encode (uint64_t val); ferencd@0: ferencd@0: } //b62 ferencd@0: ferencd@0: /* The random namespace has function for getting random data */ ferencd@0: namespace random { ferencd@0: ferencd@0: /* The class for the characters to be used in the string */ ferencd@0: enum class random_string_class ferencd@0: { ferencd@0: RSC_HEX = 0, ferencd@0: RSC_B64 = 1, ferencd@0: RSC_FULL = 2, ferencd@0: RSC_ASC_DEC = 3, ferencd@0: RSC_DEC = 4, ferencd@0: }; ferencd@0: ferencd@0: ferencd@0: /** ferencd@0: * Will generate a random string with the given charaters from the class ferencd@0: **/ ferencd@0: std::string random_string(size_t length , random_string_class cls = unafrog::utils::random::random_string_class::RSC_ASC_DEC); ferencd@0: ferencd@0: template ferencd@0: Iter random_element(Iter start, Iter end, RandomGenerator& g) ferencd@0: { ferencd@0: std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1); ferencd@0: std::advance(start, dis(g)); ferencd@0: return start; ferencd@0: } ferencd@0: ferencd@0: template ferencd@0: Iter random_element(Iter start, Iter end) ferencd@0: { ferencd@0: static std::random_device rd; ferencd@0: static std::mt19937 gen(rd()); ferencd@0: return random_element(start, end, gen); ferencd@0: } ferencd@0: ferencd@0: template ferencd@0: C random_element(const std::vector& v) ferencd@0: { ferencd@0: return *random_element(v.begin(), v.end()); ferencd@0: } ferencd@0: ferencd@0: } // random ferencd@0: ferencd@0: ferencd@0: std::string to_upper(const std::string& s); ferencd@0: ferencd@0: template ferencd@0: void redirect_stream(std::ostream& out, char sep, ferencd@0: Arg&& arg, Args&&... args) ferencd@0: { ferencd@0: out << std::forward(arg); ferencd@0: ((out << sep << std::forward(args)), ...); ferencd@0: } ferencd@0: ferencd@0: template ferencd@0: std::string join_string(char sep, Args&&... args) ferencd@0: { ferencd@0: std::stringstream ss; ferencd@0: redirect_stream(ss, sep, std::forward(args)...); ferencd@0: return ss.str(); ferencd@0: } ferencd@0: ferencd@0: }} // unafrog::utils ferencd@0: ferencd@0: /** ferencd@0: * Will be used to compare string case insensitively ferencd@0: **/ ferencd@0: struct case_insensitive_str_eq ferencd@0: { ferencd@0: case_insensitive_str_eq(std::string key); ferencd@0: bool operator()(const std::string& item) const; ferencd@0: std::string key_; ferencd@0: }; ferencd@0: ferencd@0: /** ferencd@0: * Converts the given enum class to its base type ferencd@0: */ ferencd@0: template ferencd@0: constexpr typename std::underlying_type::type basetype(E e) noexcept ferencd@0: { ferencd@0: return static_cast::type>(e); ferencd@0: } ferencd@0: ferencd@0: const std::string platform(); ferencd@0: ferencd@0: /** will join the strings in the vector with the given delimiter */ ferencd@0: std::string join(const std::vector& vec, const char* delim); ferencd@0: ferencd@0: /** will split the string into substrings, delimited by delim */ ferencd@0: std::vector split(const std::string&, const char *delim); ferencd@0: ferencd@0: /** will run a piece of code when the scope is left */ ferencd@0: template ferencd@0: class scope_exit_runner ferencd@0: { ferencd@0: public: ferencd@0: scope_exit_runner(F f) : m_f(f) {}; ferencd@0: scope_exit_runner(F f, bool run) : m_f(f), m_really_run(run) {}; ferencd@0: virtual ~scope_exit_runner() { if(m_really_run) m_f(); } ferencd@0: void invalidate() { m_really_run = false; } ferencd@0: void validate() { m_really_run = true; } ferencd@0: ferencd@0: template ferencd@0: void operator()(T s) ferencd@0: { ferencd@0: m_f(s); ferencd@0: } ferencd@0: ferencd@0: private: ferencd@0: F m_f; ferencd@0: bool m_really_run = true; ferencd@0: }; ferencd@0: ferencd@0: template ferencd@0: class simple_runner ferencd@0: { ferencd@0: public: ferencd@0: simple_runner(F f) : m_f(f) {} ferencd@0: template ferencd@0: simple_runner(F f, T t) : m_f(f) ferencd@0: { ferencd@0: m_f(t); ferencd@0: } ferencd@0: ferencd@0: template ferencd@0: void operator()(T t) ferencd@0: { ferencd@0: m_f(t); ferencd@0: } ferencd@0: ferencd@0: private: ferencd@0: F m_f; ferencd@0: ferencd@0: }; ferencd@0: ferencd@0: /* just to signal an error */ ferencd@0: unsigned internalServerError(); ferencd@0: ferencd@0: /* remove the duplicates from a string */ ferencd@0: std::string remove_duplicates(std::string s, char to_remove); ferencd@0: ferencd@0: /* removes the quotes from the string */ ferencd@0: void remove_quotes(std::string &s); ferencd@0: ferencd@0: ferencd@0: template ferencd@0: inline typename std::enable_if< I == sizeof...(Tp), void>::type ferencd@0: for_index(std::size_t, std::tuple &, FuncT) ferencd@0: {} ferencd@0: ferencd@0: template ferencd@0: inline typename std::enable_if< I < sizeof...(Tp), void>::type ferencd@0: for_index(int index, std::tuple& t, FuncT f) ferencd@0: { ferencd@0: if (index == 0) f(std::get(t)); ferencd@0: for_index(index-1, t, f); ferencd@0: } ferencd@0: ferencd@0: template ferencd@0: std::tuple valset(Vals... v) ferencd@0: { ferencd@0: return std::make_tuple(v...); ferencd@0: } ferencd@0: ferencd@0: #endif ferencd@0: