| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include "util.hh" |
|---|
| 21 | |
|---|
| 22 | #include <boost/date_time/posix_time/posix_time.hpp> |
|---|
| 23 | #include <boost/asio/streambuf.hpp> |
|---|
| 24 | |
|---|
| 25 | namespace util |
|---|
| 26 | { |
|---|
| 27 | std::string split(std::string &s, const char *sep) |
|---|
| 28 | { |
|---|
| 29 | if (s.length() == 0) return s; |
|---|
| 30 | std::string::size_type k1 = s.find_first_not_of(sep); |
|---|
| 31 | if (k1 == std::string::npos) { s = ""; return ""; } |
|---|
| 32 | std::string::size_type k2 = s.find_first_of(sep, k1); |
|---|
| 33 | std::string rv = s.substr(k1, k2-k1); |
|---|
| 34 | s.erase(0, k2); |
|---|
| 35 | return rv; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | bool is_number(std::string s) |
|---|
| 39 | { |
|---|
| 40 | if (s.empty()) return false; |
|---|
| 41 | for (size_t i = 0; i < s.length(); i++) { |
|---|
| 42 | if (!is_digit(s[i])) return false; |
|---|
| 43 | } |
|---|
| 44 | return true; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | std::string split1R(std::string &s, const char *delim) |
|---|
| 48 | { |
|---|
| 49 | size_t dl = s.find_first_of(delim); |
|---|
| 50 | std::string rv = s.substr(0, dl); |
|---|
| 51 | if (dl+1 == 0) s.clear(); else s.erase(0, dl+1); |
|---|
| 52 | return rv; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | std::string split1L(std::string &s, const char *delim) |
|---|
| 56 | { |
|---|
| 57 | size_t dl = s.find_first_of(delim); |
|---|
| 58 | if (dl == std::string::npos) return ""; |
|---|
| 59 | std::string rv = s.substr(0, dl); |
|---|
| 60 | s.erase(0, dl+1); |
|---|
| 61 | return rv; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | std::string current_imf_date() |
|---|
| 65 | { |
|---|
| 66 | using namespace boost::posix_time; |
|---|
| 67 | std::ostringstream ss; |
|---|
| 68 | time_facet *tf = new time_facet |
|---|
| 69 | ("%a, %d %b %Y %H:%M:%S -0000"); |
|---|
| 70 | ss.imbue(std::locale(std::locale::classic(), tf)); |
|---|
| 71 | ss << second_clock::universal_time(); |
|---|
| 72 | return ss.str(); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | std::string streambuf_to_string(boost::asio::streambuf &sbuf, size_t n) |
|---|
| 76 | { |
|---|
| 77 | std::string recv; |
|---|
| 78 | recv.reserve(n); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | sbuf.commit(n); |
|---|
| 84 | typedef boost::asio::streambuf::const_buffers_type |
|---|
| 85 | const_buffers_type; |
|---|
| 86 | const_buffers_type bufs = sbuf.data(); |
|---|
| 87 | for (const_buffers_type::const_iterator it = bufs.begin(); |
|---|
| 88 | it != bufs.end(); it++) |
|---|
| 89 | { |
|---|
| 90 | using boost::asio::buffer_cast; |
|---|
| 91 | using boost::asio::buffer_size; |
|---|
| 92 | recv += std::string(buffer_cast<const char *>(*it), |
|---|
| 93 | buffer_size(*it)); |
|---|
| 94 | } |
|---|
| 95 | sbuf.consume(recv.length()); |
|---|
| 96 | return recv; |
|---|
| 97 | } |
|---|
| 98 | } |
|---|