| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | #pragma once |
|---|
| 22 | #include <unicode_char.hpp> |
|---|
| 23 | #include <types.hpp> |
|---|
| 24 | |
|---|
| 25 | #include <string> |
|---|
| 26 | #include <sstream> |
|---|
| 27 | #include <iomanip> |
|---|
| 28 | #include <utility> |
|---|
| 29 | #include <list> |
|---|
| 30 | #include <functional> |
|---|
| 31 | #include <time.h> |
|---|
| 32 | #include <algorithm> |
|---|
| 33 | #include <locale> |
|---|
| 34 | #include <iostream> |
|---|
| 35 | |
|---|
| 36 | #include <format.hpp> |
|---|
| 37 | #include <utf8.hpp> |
|---|
| 38 | |
|---|
| 39 | #include <cctype> |
|---|
| 40 | |
|---|
| 41 | #ifdef __GNUC__ |
|---|
| 42 | #include <iconv.h> |
|---|
| 43 | #include <errno.h> |
|---|
| 44 | #endif |
|---|
| 45 | |
|---|
| 46 | #include <boost/lexical_cast.hpp> |
|---|
| 47 | #include <boost/date_time.hpp> |
|---|
| 48 | #include <boost/algorithm/string.hpp> |
|---|
| 49 | #include <boost/foreach.hpp> |
|---|
| 50 | //#include <boost/date_time/local_time/local_date_time.hpp> |
|---|
| 51 | //#include <boost/date_time/gregorian/conversion.hpp> |
|---|
| 52 | //boost::local_time::local_date_time |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | #ifdef _DEBUG |
|---|
| 56 | #include <iostream> |
|---|
| 57 | #endif |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | #include <string> |
|---|
| 63 | #include <locale> |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | namespace strEx { |
|---|
| 67 | class string_exception : public std::exception { |
|---|
| 68 | std::wstring _what; |
|---|
| 69 | public: |
|---|
| 70 | string_exception(std::wstring what) : _what(what) {} |
|---|
| 71 | std::wstring what() { |
|---|
| 72 | return _what; |
|---|
| 73 | } |
|---|
| 74 | virtual ~string_exception() throw(); |
|---|
| 75 | }; |
|---|
| 76 | namespace s { |
|---|
| 77 | template<class T> |
|---|
| 78 | inline T stox(std::string s) { |
|---|
| 79 | return boost::lexical_cast<T>(s.c_str()); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | template<typename T> |
|---|
| 83 | inline std::string xtos(T i) { |
|---|
| 84 | std::stringstream ss; |
|---|
| 85 | ss << i; |
|---|
| 86 | return ss.str(); |
|---|
| 87 | } |
|---|
| 88 | inline std::string itos_non_sci(double i) { |
|---|
| 89 | std::stringstream ss; |
|---|
| 90 | if (i < 10) |
|---|
| 91 | ss.precision(20); |
|---|
| 92 | ss << std::noshowpoint << std::fixed << i; |
|---|
| 93 | std::string s = ss.str(); |
|---|
| 94 | std::string::size_type pos = s.find('.'); |
|---|
| 95 | if (pos != std::string::npos && (s.length()-pos) > 6) { |
|---|
| 96 | s = s.substr(0, pos+6); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | pos = s.find_last_not_of('0'); |
|---|
| 100 | if (pos == std::wstring::npos) |
|---|
| 101 | return s; |
|---|
| 102 | if (s[pos] != '.') |
|---|
| 103 | pos++; |
|---|
| 104 | return s.substr(0, pos); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | inline void append_list(std::wstring &lst, std::wstring &append, std::wstring sep = _T(", ")) { |
|---|
| 109 | if (append.empty()) |
|---|
| 110 | return; |
|---|
| 111 | if (!lst.empty()) |
|---|
| 112 | lst += sep; |
|---|
| 113 | lst += append; |
|---|
| 114 | } |
|---|
| 115 | inline void append_list_ex(std::wstring &lst, std::wstring append, std::wstring sep = _T(", ")) { |
|---|
| 116 | if (append.empty()) |
|---|
| 117 | return; |
|---|
| 118 | if (!lst.empty()) |
|---|
| 119 | lst += sep; |
|---|
| 120 | lst += append; |
|---|
| 121 | } |
|---|
| 122 | inline std::string wstring_to_string( const std::wstring& str ) { |
|---|
| 123 | return utf8::cvt<std::string>(str); |
|---|
| 124 | } |
|---|
| 125 | inline std::wstring string_to_wstring( const std::string& str ) { |
|---|
| 126 | return utf8::cvt<std::wstring>(str); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | inline std::wstring format_buffer(const wchar_t* buf, unsigned int len) { |
|---|
| 130 | std::wstringstream ss; |
|---|
| 131 | std::wstring chars; |
|---|
| 132 | for (unsigned int i=0;i<len;i++) { |
|---|
| 133 | ss << std::hex << buf[i]; |
|---|
| 134 | ss << _T(", "); |
|---|
| 135 | if (buf[i] >= ' ' && buf[i] <= 'z') |
|---|
| 136 | chars += buf[i]; |
|---|
| 137 | else |
|---|
| 138 | chars += '?'; |
|---|
| 139 | if (i%32==0) { |
|---|
| 140 | ss << chars; |
|---|
| 141 | ss << _T("\n"); |
|---|
| 142 | chars = _T(""); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | ss << chars; |
|---|
| 146 | return ss.str(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | inline std::wstring format_date(boost::posix_time::ptime date, std::wstring format = _T("%Y-%m-%d %H:%M:%S")) { |
|---|
| 150 | std::locale locale_local (""); |
|---|
| 151 | |
|---|
| 152 | boost::gregorian::wdate_facet *date_output = new boost::gregorian::wdate_facet(); |
|---|
| 153 | std::locale locale_adjusted (locale_local, date_output); |
|---|
| 154 | |
|---|
| 155 | std::wstringstream date_ss; |
|---|
| 156 | date_ss.imbue(locale_adjusted); |
|---|
| 157 | |
|---|
| 158 | date_output->format(format.c_str()); |
|---|
| 159 | date_ss << date; |
|---|
| 160 | |
|---|
| 161 | std::wstring ss = date_ss.str(); |
|---|
| 162 | return ss; |
|---|
| 163 | } |
|---|
| 164 | #ifdef WIN32 |
|---|
| 165 | inline std::wstring format_date(const SYSTEMTIME &time, std::wstring format = _T("%Y-%m-%d %H:%M:%S")) { |
|---|
| 166 | TCHAR buf[51]; |
|---|
| 167 | |
|---|
| 168 | struct tm tmTime; |
|---|
| 169 | memset(&tmTime, 0, sizeof(tmTime)); |
|---|
| 170 | |
|---|
| 171 | tmTime.tm_sec = time.wSecond; // seconds after the minute - [0,59] |
|---|
| 172 | tmTime.tm_min = time.wMinute; // minutes after the hour - [0,59] |
|---|
| 173 | tmTime.tm_hour = time.wHour; // hours since midnight - [0,23] |
|---|
| 174 | tmTime.tm_mday = time.wDay; // day of the month - [1,31] |
|---|
| 175 | tmTime.tm_mon = time.wMonth-1; // months since January - [0,11] |
|---|
| 176 | tmTime.tm_year = time.wYear-1900; // years since 1900 |
|---|
| 177 | tmTime.tm_wday = time.wDayOfWeek; // days since Sunday - [0,6] |
|---|
| 178 | |
|---|
| 179 | size_t l = wcsftime(buf, 50, format.c_str(), &tmTime); |
|---|
| 180 | if (l <= 0 || l >= 50) |
|---|
| 181 | return _T(""); |
|---|
| 182 | buf[l] = 0; |
|---|
| 183 | return buf; |
|---|
| 184 | } |
|---|
| 185 | #endif |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | inline std::wstring format_date(std::time_t time, std::wstring format = _T("%Y-%m-%d %H:%M:%S")) { |
|---|
| 189 | return format_date(boost::posix_time::from_time_t(time), format); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | static const long long SECS_BETWEEN_EPOCHS = 11644473600; |
|---|
| 194 | static const long long SECS_TO_100NS = 10000000; |
|---|
| 195 | inline unsigned long long filetime_to_time(unsigned long long filetime) { |
|---|
| 196 | return (filetime - (SECS_BETWEEN_EPOCHS * SECS_TO_100NS)) / SECS_TO_100NS; |
|---|
| 197 | } |
|---|
| 198 | inline std::wstring format_filetime(unsigned long long filetime, std::wstring format = _T("%Y-%m-%d %H:%M:%S")) { |
|---|
| 199 | if (filetime == 0) |
|---|
| 200 | return _T("ZERO"); |
|---|
| 201 | return format_date(static_cast<time_t>(filetime_to_time(filetime)), format); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | inline void replace(std::wstring &string, const std::wstring replace, const std::wstring with) { |
|---|
| 205 | std::wstring::size_type pos = string.find(replace); |
|---|
| 206 | std::wstring::size_type len = replace.length(); |
|---|
| 207 | while (pos != std::wstring::npos) { |
|---|
| 208 | string = string.substr(0,pos)+with+string.substr(pos+len); |
|---|
| 209 | if (with.find(replace) != std::wstring::npos) // If the replace containes the key look after the replace! |
|---|
| 210 | pos = string.find(replace, pos+with.length()); |
|---|
| 211 | else |
|---|
| 212 | pos = string.find(replace, pos+1); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | inline void replace(std::string &string, const std::string replace, const std::string with) { |
|---|
| 216 | std::string::size_type pos = string.find(replace); |
|---|
| 217 | std::string::size_type len = replace.length(); |
|---|
| 218 | while (pos != std::string::npos) { |
|---|
| 219 | string = string.substr(0,pos)+with+string.substr(pos+len); |
|---|
| 220 | if (with.find(replace) != std::string::npos) // If the replace containes the key look after the replace! |
|---|
| 221 | pos = string.find(replace, pos+with.length()); |
|---|
| 222 | else |
|---|
| 223 | pos = string.find(replace, pos+1); |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | inline std::wstring ctos(wchar_t c) { |
|---|
| 227 | return std::wstring(1, c); |
|---|
| 228 | } |
|---|
| 229 | inline wchar_t stoc(std::wstring str) { |
|---|
| 230 | if (str.length() == 0) |
|---|
| 231 | return L' '; |
|---|
| 232 | return str[0]; |
|---|
| 233 | } |
|---|
| 234 | template<class T> |
|---|
| 235 | inline std::wstring itos(T i) { |
|---|
| 236 | std::wstringstream ss; |
|---|
| 237 | ss << i; |
|---|
| 238 | return ss.str(); |
|---|
| 239 | } |
|---|
| 240 | inline std::wstring itos_non_sci(double i) { |
|---|
| 241 | std::wstringstream ss; |
|---|
| 242 | if (i < 10) |
|---|
| 243 | ss.precision(20); |
|---|
| 244 | ss << std::noshowpoint << std::fixed << i; |
|---|
| 245 | std::wstring s = ss.str(); |
|---|
| 246 | std::wstring::size_type pos = s.find_last_not_of('0'); |
|---|
| 247 | if (pos == std::wstring::npos) |
|---|
| 248 | return s; |
|---|
| 249 | if (s[pos] != '.') |
|---|
| 250 | pos++; |
|---|
| 251 | return s.substr(0, pos); |
|---|
| 252 | } |
|---|
| 253 | inline std::wstring ihextos(unsigned int i) { |
|---|
| 254 | std::wstringstream ss; |
|---|
| 255 | ss << std::hex << i; |
|---|
| 256 | return ss.str(); |
|---|
| 257 | } |
|---|
| 258 | inline int stoi(std::wstring s) { |
|---|
| 259 | return boost::lexical_cast<int>(s.c_str()); |
|---|
| 260 | } |
|---|
| 261 | template<class T> |
|---|
| 262 | inline double stod(T s) { |
|---|
| 263 | return boost::lexical_cast<double>(s.c_str()); |
|---|
| 264 | } |
|---|
| 265 | inline long long stoi64(std::wstring s) { |
|---|
| 266 | return boost::lexical_cast<long long>(s.c_str()); |
|---|
| 267 | } |
|---|
| 268 | inline unsigned stoui_as_time(std::wstring time, unsigned int smallest_unit = 1000) { |
|---|
| 269 | std::wstring::size_type p = time.find_first_of(_T("sSmMhHdDwW")); |
|---|
| 270 | std::wstring::size_type pend = time.find_first_not_of(_T("0123456789")); |
|---|
| 271 | unsigned int value = boost::lexical_cast<unsigned int>(pend==std::wstring::npos?time:time.substr(0,pend).c_str()); |
|---|
| 272 | if (p == std::wstring::npos) |
|---|
| 273 | return value * smallest_unit; |
|---|
| 274 | else if ( (time[p] == 's') || (time[p] == 'S') ) |
|---|
| 275 | return value * 1000; |
|---|
| 276 | else if ( (time[p] == 'm') || (time[p] == 'M') ) |
|---|
| 277 | return value * 60 * 1000; |
|---|
| 278 | else if ( (time[p] == 'h') || (time[p] == 'H') ) |
|---|
| 279 | return value * 60 * 60 * 1000; |
|---|
| 280 | else if ( (time[p] == 'd') || (time[p] == 'D') ) |
|---|
| 281 | return value * 24 * 60 * 60 * 1000; |
|---|
| 282 | else if ( (time[p] == 'w') || (time[p] == 'W') ) |
|---|
| 283 | return value * 7 * 24 * 60 * 60 * 1000; |
|---|
| 284 | return value * smallest_unit; |
|---|
| 285 | } |
|---|
| 286 | inline unsigned stoui_as_time_sec(std::wstring time, unsigned int smallest_unit = 1) { |
|---|
| 287 | std::wstring::size_type p = time.find_first_of(_T("sSmMhHdDwW")); |
|---|
| 288 | std::wstring::size_type pend = time.find_first_not_of(_T("0123456789")); |
|---|
| 289 | unsigned int value = boost::lexical_cast<unsigned int>(pend==std::wstring::npos?time:time.substr(0,pend).c_str()); |
|---|
| 290 | if (p == std::wstring::npos) |
|---|
| 291 | return value * smallest_unit; |
|---|
| 292 | else if ( (time[p] == L's') || (time[p] == L'S') ) |
|---|
| 293 | return value; |
|---|
| 294 | else if ( (time[p] == L'm') || (time[p] == L'M') ) |
|---|
| 295 | return value * 60; |
|---|
| 296 | else if ( (time[p] == L'h') || (time[p] == L'H') ) |
|---|
| 297 | return value * 60 * 60; |
|---|
| 298 | else if ( (time[p] == L'd') || (time[p] == L'D') ) |
|---|
| 299 | return value * 24 * 60 * 60; |
|---|
| 300 | else if ( (time[p] == L'w') || (time[p] == L'W') ) |
|---|
| 301 | return value * 7 * 24 * 60 * 60; |
|---|
| 302 | return value * smallest_unit; |
|---|
| 303 | } |
|---|
| 304 | inline unsigned stoui_as_time_sec(std::string time, unsigned int smallest_unit = 1) { |
|---|
| 305 | std::string::size_type p = time.find_first_of("sSmMhHdDwW"); |
|---|
| 306 | std::string::size_type pend = time.find_first_not_of("0123456789"); |
|---|
| 307 | unsigned int value = boost::lexical_cast<unsigned int>(pend==std::string::npos?time:time.substr(0,pend).c_str()); |
|---|
| 308 | if (p == std::string::npos) |
|---|
| 309 | return value * smallest_unit; |
|---|
| 310 | else if ( (time[p |
|---|
| 311 | ] == 's') || (time[p] == 'S') ) |
|---|
| 312 | return value; |
|---|
| 313 | else if ( (time[p] == 'm') || (time[p] == 'M') ) |
|---|
| 314 | return value * 60; |
|---|
| 315 | else if ( (time[p] == 'h') || (time[p] == 'H') ) |
|---|
| 316 | return value * 60 * 60; |
|---|
| 317 | else if ( (time[p] == 'd') || (time[p] == 'D') ) |
|---|
| 318 | return value * 24 * 60 * 60; |
|---|
| 319 | else if ( (time[p] == 'w') || (time[p] == 'W') ) |
|---|
| 320 | return value * 7 * 24 * 60 * 60; |
|---|
| 321 | return value * smallest_unit; |
|---|
| 322 | } |
|---|
| 323 | inline long stol_as_time_sec(std::wstring time, unsigned int smallest_unit = 1) { |
|---|
| 324 | if (time.length() > 1 && time[0] == L'-') |
|---|
| 325 | return -(long)stoui_as_time_sec(time.substr(1), smallest_unit); |
|---|
| 326 | return stoui_as_time_sec(time, smallest_unit); |
|---|
| 327 | } |
|---|
| 328 | inline long stol_as_time_sec(std::string time, unsigned int smallest_unit = 1) { |
|---|
| 329 | if (time.length() > 1 && time[0] == '-') |
|---|
| 330 | return -(long)stoui_as_time_sec(time.substr(1), smallest_unit); |
|---|
| 331 | return stoui_as_time_sec(time, smallest_unit); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | inline unsigned long long stoi64_as_time(std::wstring time, unsigned int smallest_unit = 1000) { |
|---|
| 335 | std::wstring::size_type p = time.find_first_of(_T("sSmMhHdDwW")); |
|---|
| 336 | if (p == std::wstring::npos) |
|---|
| 337 | return boost::lexical_cast<long long>(time) * smallest_unit; |
|---|
| 338 | unsigned long long value = boost::lexical_cast<long long>(time.substr(0, p)); |
|---|
| 339 | if ( (time[p] == 's') || (time[p] == 'S') ) |
|---|
| 340 | return value * 1000; |
|---|
| 341 | else if ( (time[p] == 'm') || (time[p] == 'M') ) |
|---|
| 342 | return value * 60 * 1000; |
|---|
| 343 | else if ( (time[p] == 'h') || (time[p] == 'H') ) |
|---|
| 344 | return value * 60 * 60 * 1000; |
|---|
| 345 | else if ( (time[p] == 'd') || (time[p] == 'D') ) |
|---|
| 346 | return value * 24 * 60 * 60 * 1000; |
|---|
| 347 | else if ( (time[p] == 'w') || (time[p] == 'W') ) |
|---|
| 348 | return value * 7 * 24 * 60 * 60 * 1000; |
|---|
| 349 | return value * smallest_unit; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | #define WEEK (7 * 24 * 60 * 60 * 1000) |
|---|
| 354 | #define DAY (24 * 60 * 60 * 1000) |
|---|
| 355 | #define HOUR (60 * 60 * 1000) |
|---|
| 356 | #define MIN (60 * 1000) |
|---|
| 357 | #define SEC (1000) |
|---|
| 358 | inline std::wstring itos_as_time(unsigned long long time) { |
|---|
| 359 | if (time > WEEK) { |
|---|
| 360 | unsigned int w = static_cast<unsigned int>(time/WEEK); |
|---|
| 361 | unsigned int d = static_cast<unsigned int>((time-(w*WEEK))/DAY); |
|---|
| 362 | unsigned int h = static_cast<unsigned int>((time-(w*WEEK)-(d*DAY))/HOUR); |
|---|
| 363 | unsigned int m = static_cast<unsigned int>((time-(w*WEEK)-(d*DAY)-(h*HOUR))/MIN); |
|---|
| 364 | return itos(w) + _T("w ") + itos(d) + _T("d ") + itos(h) + _T(":") + itos(m); |
|---|
| 365 | } |
|---|
| 366 | else if (time > DAY) { |
|---|
| 367 | unsigned int d = static_cast<unsigned int>((time)/DAY); |
|---|
| 368 | unsigned int h = static_cast<unsigned int>((time-(d*DAY))/HOUR); |
|---|
| 369 | unsigned int m = static_cast<unsigned int>((time-(d*DAY)-(h*HOUR))/MIN); |
|---|
| 370 | return itos(d) + _T("d ") + itos(h) + _T(":") + itos(m); |
|---|
| 371 | } |
|---|
| 372 | else if (time > HOUR) { |
|---|
| 373 | unsigned int h = static_cast<unsigned int>((time)/HOUR); |
|---|
| 374 | unsigned int m = static_cast<unsigned int>((time-(h*HOUR))/MIN); |
|---|
| 375 | return itos(h) + _T(":") + itos(m); |
|---|
| 376 | } else if (time > MIN) { |
|---|
| 377 | return _T("0:") + itos(static_cast<unsigned int>(time/(60 * 1000))); |
|---|
| 378 | } else if (time > SEC) |
|---|
| 379 | return itos(static_cast<unsigned int>(time/(1000))) + _T("s"); |
|---|
| 380 | return itos(static_cast<unsigned int>(time)); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | typedef std::list<std::wstring> splitList; |
|---|
| 385 | inline splitList splitEx(const std::wstring str, const std::wstring key) { |
|---|
| 386 | splitList ret; |
|---|
| 387 | std::wstring::size_type pos = 0, lpos = 0; |
|---|
| 388 | while ((pos = str.find(key, pos)) != std::wstring::npos) { |
|---|
| 389 | ret.push_back(str.substr(lpos, pos-lpos)); |
|---|
| 390 | lpos = ++pos; |
|---|
| 391 | } |
|---|
| 392 | if (lpos < str.size()) |
|---|
| 393 | ret.push_back(str.substr(lpos)); |
|---|
| 394 | return ret; |
|---|
| 395 | } |
|---|
| 396 | typedef std::vector<std::wstring> splitVector; |
|---|
| 397 | template<class T> |
|---|
| 398 | inline std::vector<T> splitV(const T str, const T key) { |
|---|
| 399 | std::vector<T> ret; |
|---|
| 400 | typename T::size_type pos = 0, lpos = 0; |
|---|
| 401 | while ((pos = str.find(key, pos)) != T::npos) { |
|---|
| 402 | ret.push_back(str.substr(lpos, pos-lpos)); |
|---|
| 403 | lpos = ++pos; |
|---|
| 404 | } |
|---|
| 405 | if (lpos < str.size()) |
|---|
| 406 | ret.push_back(str.substr(lpos)); |
|---|
| 407 | return ret; |
|---|
| 408 | } |
|---|
| 409 | inline std::wstring joinEx(splitList lst, std::wstring key) { |
|---|
| 410 | std::wstring ret; |
|---|
| 411 | for (splitList::const_iterator it = lst.begin(); it != lst.end(); ++it) { |
|---|
| 412 | if (!ret.empty()) |
|---|
| 413 | ret += key; |
|---|
| 414 | ret += *it; |
|---|
| 415 | } |
|---|
| 416 | return ret; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | inline std::wstring trim_right(const std::wstring &source , const std::wstring& t = _T(" ")) |
|---|
| 422 | { |
|---|
| 423 | std::wstring str = source; |
|---|
| 424 | return str.erase( str.find_last_not_of(t) + 1); |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | inline std::wstring trim_left( const std::wstring& source, const std::wstring& t = _T(" ")) |
|---|
| 428 | { |
|---|
| 429 | std::wstring str = source; |
|---|
| 430 | return str.erase(0 , source.find_first_not_of(t) ); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | inline std::wstring trim(const std::wstring& source, const std::wstring& t = _T(" ")) |
|---|
| 434 | { |
|---|
| 435 | std::wstring str = source; |
|---|
| 436 | return trim_left( trim_right( str , t) , t ); |
|---|
| 437 | } |
|---|
| 438 | template<class T> |
|---|
| 439 | inline std::pair<T,T> split(T str, T key) { |
|---|
| 440 | typename T::size_type pos = str.find(key); |
|---|
| 441 | if (pos == T::npos) |
|---|
| 442 | return std::pair<T,T>(str, T()); |
|---|
| 443 | return std::pair<T,T>(str.substr(0, pos), str.substr(pos+key.length())); |
|---|
| 444 | } |
|---|
| 445 | typedef std::pair<std::wstring,std::wstring> token; |
|---|
| 446 | // foo bar "foo \" bar" foo -> foo, bar "foo \" bar" foo -> bar, "foo \" bar" foo -> |
|---|
| 447 | // |
|---|
| 448 | inline token getToken(std::wstring buffer, char split, bool escape = false) { |
|---|
| 449 | std::wstring::size_type pos = std::wstring::npos; |
|---|
| 450 | if ((escape) && (buffer[0] == '\"')) { |
|---|
| 451 | do { |
|---|
| 452 | pos = buffer.find('\"'); |
|---|
| 453 | } |
|---|
| 454 | while (((pos != std::wstring::npos)&&(pos > 1))&&(buffer[pos-1] == '\\')); |
|---|
| 455 | } else |
|---|
| 456 | pos = buffer.find(split); |
|---|
| 457 | if (pos == std::wstring::npos) |
|---|
| 458 | return token(buffer, _T("")); |
|---|
| 459 | if (pos == buffer.length()-1) |
|---|
| 460 | return token(buffer.substr(0, pos), _T("")); |
|---|
| 461 | return token(buffer.substr(0, pos-1), buffer.substr(pos-1)); |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | #define MK_FORMAT_FTD(min, key, val) \ |
|---|
| 465 | if (mtm->tm_year > min) \ |
|---|
| 466 | strEx::replace(format, key, strEx::itos(val)); \ |
|---|
| 467 | else \ |
|---|
| 468 | strEx::replace(format, key, _T("0")); |
|---|
| 469 | #ifdef WIN32 |
|---|
| 470 | inline std::wstring format_time_delta(struct tm *mtm, std::wstring format = _T("%Y years %m months %d days %H hours %M minutes")) { |
|---|
| 471 | // "Date: %Y-%m-%d %H:%M:%S" |
|---|
| 472 | MK_FORMAT_FTD(70, _T("%Y"), mtm->tm_year); |
|---|
| 473 | MK_FORMAT_FTD(0, _T("%m"), mtm->tm_mon); |
|---|
| 474 | MK_FORMAT_FTD(0, _T("%d"), mtm->tm_mday-1); |
|---|
| 475 | MK_FORMAT_FTD(0, _T("%H"), mtm->tm_hour); |
|---|
| 476 | MK_FORMAT_FTD(0, _T("%M"), mtm->tm_min); |
|---|
| 477 | MK_FORMAT_FTD(0, _T("%S"), mtm->tm_sec); |
|---|
| 478 | return format; |
|---|
| 479 | } |
|---|
| 480 | inline std::wstring format_time_delta(time_t time, std::wstring format = _T("%Y years %m months %d days %H hours %M minutes")) { |
|---|
| 481 | struct tm nt; // = new struct tm; |
|---|
| 482 | #if (_MSC_VER > 1300) // 1300 == VC++ 7.0 |
|---|
| 483 | if (gmtime_s(&nt, &time) != 0) |
|---|
| 484 | return _T(""); |
|---|
| 485 | #else |
|---|
| 486 | nt = gmtime(&time); |
|---|
| 487 | if (nt == NULL) |
|---|
| 488 | return ""; |
|---|
| 489 | #endif |
|---|
| 490 | return format_time_delta(&nt, format); |
|---|
| 491 | } |
|---|
| 492 | inline std::wstring format_filetime_delta(unsigned long long filetime, std::wstring format = _T("%Y-%m-%d %H:%M:%S")) { |
|---|
| 493 | if (filetime == 0) |
|---|
| 494 | return _T("ZERO"); |
|---|
| 495 | //filetime -= (SECS_BETWEEN_EPOCHS * SECS_TO_100NS); |
|---|
| 496 | filetime /= SECS_TO_100NS; |
|---|
| 497 | return format_time_delta(static_cast<time_t>(filetime), format); |
|---|
| 498 | } |
|---|
| 499 | #endif |
|---|
| 500 | |
|---|
| 501 | #ifdef _DEBUG |
|---|
| 502 | inline void test_getToken(std::wstring in1, char in2, std::wstring out1, std::wstring out2) { |
|---|
| 503 | token t = getToken(in1, in2); |
|---|
| 504 | std::wcout << _T("strEx::test_getToken(") << in1 << _T(", ") << in2 << _T(") : "); |
|---|
| 505 | if ((t.first == out1) && (t.second == out2)) |
|---|
| 506 | std::wcout << _T("Succeeded") << std::endl; |
|---|
| 507 | else |
|---|
| 508 | std::wcout << _T("Failed [") << out1 << _T("=") << t.first << _T(", ") << out2 << _T("=") << t.second << _T("]") << std::endl; |
|---|
| 509 | } |
|---|
| 510 | inline void run_test_getToken() { |
|---|
| 511 | test_getToken(_T(""), '&', _T(""), _T("")); |
|---|
| 512 | test_getToken(_T("&"), '&', _T(""), _T("")); |
|---|
| 513 | test_getToken(_T("&&"), '&', _T(""), _T("&")); |
|---|
| 514 | test_getToken(_T("foo"), '&', _T("foo"), _T("")); |
|---|
| 515 | test_getToken(_T("foo&"), '&', _T("foo"), _T("")); |
|---|
| 516 | test_getToken(_T("foo&bar"), '&', _T("foo"), _T("bar")); |
|---|
| 517 | test_getToken(_T("foo&bar&test"), '&', _T("foo"), _T("bar&test")); |
|---|
| 518 | } |
|---|
| 519 | |
|---|
| 520 | inline void test_replace(std::wstring source, std::wstring replace, std::wstring with, std::wstring out) { |
|---|
| 521 | std::wcout << _T("strEx::test_replace(") << source << _T(", ") << replace << _T(", ") << with << _T(") : "); |
|---|
| 522 | std::wstring s = source; |
|---|
| 523 | strEx::replace(s, replace, with); |
|---|
| 524 | if (s == out) |
|---|
| 525 | std::wcout << _T("Succeeded") << std::endl; |
|---|
| 526 | else |
|---|
| 527 | std::wcout << _T("Failed [") << s << _T("=") << out << _T("]") << std::endl; |
|---|
| 528 | } |
|---|
| 529 | inline void run_test_replace() { |
|---|
| 530 | test_replace(_T(""), _T(""), _T(""), _T("")); |
|---|
| 531 | test_replace(_T("foo"), _T(""), _T(""), _T("foo")); |
|---|
| 532 | test_replace(_T("foobar"), _T("foo"), _T(""), _T("bar")); |
|---|
| 533 | test_replace(_T("foobar"), _T("foo"), _T("bar"), _T("barbar")); |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | #endif |
|---|
| 537 | |
|---|
| 538 | template<typename T> |
|---|
| 539 | inline void parse_command(T &cmd_line, std::list<T> &args) { |
|---|
| 540 | boost::tokenizer<boost::escaped_list_separator<wchar_t>, typename T::const_iterator, T > tok(cmd_line, boost::escaped_list_separator<wchar_t>(L'\\', L' ', L'\"')); |
|---|
| 541 | BOOST_FOREACH(T s, tok) |
|---|
| 542 | args.push_back(s); |
|---|
| 543 | } |
|---|
| 544 | template<typename T> |
|---|
| 545 | inline void parse_command(T cmd_line, T &cmd, std::list<T> &args) { |
|---|
| 546 | boost::tokenizer<boost::escaped_list_separator<wchar_t>, typename T::const_iterator, T > tok(cmd_line, boost::escaped_list_separator<wchar_t>(L'\\', L' ', L'\"')); |
|---|
| 547 | bool first = true; |
|---|
| 548 | BOOST_FOREACH(T s, tok) |
|---|
| 549 | { |
|---|
| 550 | if (first) { |
|---|
| 551 | cmd = s; |
|---|
| 552 | first = false; |
|---|
| 553 | } else { |
|---|
| 554 | args.push_back(s); |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | } |
|---|
| 558 | } |
|---|
| 559 | /* |
|---|
| 560 | template<typename T = std::string> |
|---|
| 561 | inline void parse_command(T &string, std::list<T> &list) { |
|---|
| 562 | boost::tokenizer<boost::escaped_list_separator<char>, T::const_iterator, T > tok(string, boost::escaped_list_separator<char>('\\', ' ', '\"')); |
|---|
| 563 | BOOST_FOREACH(T s, tok) |
|---|
| 564 | list.push_back(s); |
|---|
| 565 | } |
|---|
| 566 | */ |
|---|
| 567 | |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | namespace nscp { |
|---|
| 571 | namespace helpers { |
|---|
| 572 | template <typename T> std::string to_string(const T& arg) { |
|---|
| 573 | try { |
|---|
| 574 | return boost::lexical_cast<std::string>(arg) ; |
|---|
| 575 | } |
|---|
| 576 | catch(...) { |
|---|
| 577 | return ""; |
|---|
| 578 | } |
|---|
| 579 | } |
|---|
| 580 | template <typename T> std::string to_string(const std::string& arg) { |
|---|
| 581 | return arg; |
|---|
| 582 | } |
|---|
| 583 | template <typename T> std::string to_string(const std::wstring& arg) { |
|---|
| 584 | return utf8::cvt<std::string>(arg); |
|---|
| 585 | } |
|---|
| 586 | template <typename T> std::string to_string(const wchar_t* arg) { |
|---|
| 587 | return utf8::cvt<std::string>(std::wstring(arg)); |
|---|
| 588 | } |
|---|
| 589 | template <typename T> std::wstring to_wstring(const T& arg) { |
|---|
| 590 | try { |
|---|
| 591 | return boost::lexical_cast<std::wstring>(arg) ; |
|---|
| 592 | } |
|---|
| 593 | catch(...) { |
|---|
| 594 | return _T(""); |
|---|
| 595 | } |
|---|
| 596 | } |
|---|
| 597 | template <typename T> std::wstring to_wstring(const std::wstring& arg) { |
|---|
| 598 | return arg; |
|---|
| 599 | } |
|---|
| 600 | template <typename T> std::wstring to_wstring(const std::string& arg) { |
|---|
| 601 | return utf8::cvt<std::wstring>(arg); |
|---|
| 602 | } |
|---|
| 603 | } |
|---|
| 604 | } |
|---|
| 605 | |
|---|