| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_UTIL_HH |
|---|
| 21 | #define GUARD_UTIL_HH |
|---|
| 22 | |
|---|
| 23 | #include <boost/asio/streambuf.hpp> |
|---|
| 24 | #include <stdexcept> |
|---|
| 25 | #include <string> |
|---|
| 26 | |
|---|
| 27 | namespace util |
|---|
| 28 | { |
|---|
| 29 | inline std::string to_lower(const std::string s) |
|---|
| 30 | { |
|---|
| 31 | std::string rv = s; |
|---|
| 32 | for (size_t i = 0; i < s.length(); i++) |
|---|
| 33 | { |
|---|
| 34 | if ('A' <= rv[i] && rv[i] <= 'Z') rv[i] += 'a' - 'A'; |
|---|
| 35 | } |
|---|
| 36 | return rv; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | inline void strip_crlf(std::string &s) |
|---|
| 41 | { |
|---|
| 42 | if (*(s.rbegin()) != '\n') return; |
|---|
| 43 | s.erase(s.length()-1); |
|---|
| 44 | if (*(s.rbegin()) != '\r') return; |
|---|
| 45 | s.erase(s.length()-1); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | inline void strip(std::string &s) |
|---|
| 49 | { |
|---|
| 50 | while (!s.empty() && |
|---|
| 51 | (s[0] == ' ' || s[0] == '\t' || |
|---|
| 52 | s[0] == '\r' || s[0] == '\n')) |
|---|
| 53 | s.erase(0,1); |
|---|
| 54 | while (!s.empty() && |
|---|
| 55 | (s[s.length()-1] == ' ' || s[s.length()-1] == '\t' || |
|---|
| 56 | s[s.length()-1] == '\r' || s[s.length()-1] == '\n')) |
|---|
| 57 | s.erase(s.length()-1); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | class parse_exception : public std::exception { |
|---|
| 61 | public: |
|---|
| 62 | const char *what() const throw() { |
|---|
| 63 | return "parse error"; |
|---|
| 64 | } |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | template <typename Int> |
|---|
| 70 | Int parse_nonnegative_integer(std::string s) |
|---|
| 71 | throw (parse_exception) |
|---|
| 72 | { |
|---|
| 73 | Int rv = 0; |
|---|
| 74 | |
|---|
| 75 | for (size_t i = 0; i < s.length();i++) { |
|---|
| 76 | if (!('0' <= s[i] && s[i] <= '9')) { |
|---|
| 77 | throw parse_exception(); |
|---|
| 78 | } |
|---|
| 79 | rv = 10*rv + (s[i] - '0'); |
|---|
| 80 | } |
|---|
| 81 | return rv; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | std::string split(std::string &s, const char *sep = " \t\r\n"); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | std::string split1R(std::string &s, const char *delim); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | std::string split1L(std::string &s, const char *delim); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | bool is_number(std::string s); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | inline bool is_digit(char c) |
|---|
| 106 | { |
|---|
| 107 | return '0' <= c && c <= '9'; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | inline void advance_utf8(std::string s, size_t &i) |
|---|
| 113 | { |
|---|
| 114 | unsigned char c = s[i]; |
|---|
| 115 | if (c < 0xc0) i += 1; |
|---|
| 116 | else if (c < 0xd0) i += 2; |
|---|
| 117 | else if (c < 0xe0) i += 3; |
|---|
| 118 | else i += 4; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | std::string current_imf_date(); |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | inline char b64_char(unsigned val) |
|---|
| 126 | { |
|---|
| 127 | if (val < 26) return val + 'A'; |
|---|
| 128 | if (val < 52) return val - 26 + 'a'; |
|---|
| 129 | if (val < 62) return val - 52 + '0'; |
|---|
| 130 | if (val == 62) return '-'; |
|---|
| 131 | if (val == 63) return '.'; |
|---|
| 132 | throw std::logic_error("b64_char"); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | std::string streambuf_to_string(boost::asio::streambuf &sbuf, size_t n); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | #endif |
|---|