31 #include <system_error>
35 #include "pplx/pplxtasks.h"
36 #include "cpprest/details/basic_types.h"
38 #if !defined(_WIN32) || (_MSC_VER >= 1700)
43 #include <boost/algorithm/string.hpp>
44 #if !defined(ANDROID) && !defined(__ANDROID__) // CodePlex 269
54 typedef std::chrono::seconds seconds;
80 _ASYNCRTIMP std::string __cdecl
utf16_to_utf8(
const utf16string &w);
87 _ASYNCRTIMP utf16string __cdecl
utf8_to_utf16(
const std::string &s);
108 _ASYNCRTIMP utf8string __cdecl
latin1_to_utf8(
const std::string &s);
115 _ASYNCRTIMP utility::string_t __cdecl
to_string_t(std::string &&s);
122 _ASYNCRTIMP utility::string_t __cdecl
to_string_t(utf16string &&s);
129 _ASYNCRTIMP utility::string_t __cdecl
to_string_t(
const std::string &s);
136 _ASYNCRTIMP utility::string_t __cdecl
to_string_t(
const utf16string &s);
143 _ASYNCRTIMP utf16string __cdecl
to_utf16string(
const std::string &value);
150 _ASYNCRTIMP utf16string __cdecl
to_utf16string(utf16string value);
157 _ASYNCRTIMP std::string __cdecl
to_utf8string(std::string value);
164 _ASYNCRTIMP std::string __cdecl
to_utf8string(
const utf16string &value);
169 _ASYNCRTIMP utility::string_t __cdecl
to_base64(
const std::vector<unsigned char>& data);
174 _ASYNCRTIMP utility::string_t __cdecl
to_base64(uint64_t data);
179 _ASYNCRTIMP std::vector<unsigned char> __cdecl
from_base64(
const utility::string_t& str);
181 template <
typename Source>
182 utility::string_t print_string(
const Source &val,
const std::locale &loc)
184 utility::ostringstream_t oss;
189 throw std::bad_cast();
194 template <
typename Source>
195 utility::string_t print_string(
const Source &val)
197 return print_string(val, std::locale());
200 template <
typename Target>
201 Target scan_string(
const utility::string_t &str,
const std::locale &loc)
204 utility::istringstream_t iss(str);
209 throw std::bad_cast();
214 template <
typename Target>
215 Target scan_string(
const utility::string_t &str)
217 return scan_string<Target>(str, std::locale());
230 _ASYNCRTIMP ~scoped_c_thread_locale();
232 #if !defined(ANDROID) && !defined(__ANDROID__) // CodePlex 269
234 typedef _locale_t xplat_locale;
236 typedef locale_t xplat_locale;
239 static _ASYNCRTIMP xplat_locale __cdecl c_locale();
243 std::string m_prevLocale;
244 int m_prevThreadSetting;
245 #elif !(defined(ANDROID) || defined(__ANDROID__))
246 locale_t m_prevLocale;
248 scoped_c_thread_locale(
const scoped_c_thread_locale &);
249 scoped_c_thread_locale & operator=(
const scoped_c_thread_locale &);
256 inline bool __cdecl is_alnum(
char ch)
258 return (ch >=
'0' && ch <=
'9')
259 || (ch >=
'A' && ch <=
'Z')
260 || (ch >=
'a' && ch <=
'z');
267 template <
typename _Type>
268 std::unique_ptr<_Type> make_unique() {
269 return std::unique_ptr<_Type>(
new _Type());
272 template <
typename _Type,
typename _Arg1>
273 std::unique_ptr<_Type> make_unique(_Arg1&& arg1) {
274 return std::unique_ptr<_Type>(
new _Type(std::forward<_Arg1>(arg1)));
277 template <
typename _Type,
typename _Arg1,
typename _Arg2>
278 std::unique_ptr<_Type> make_unique(_Arg1&& arg1, _Arg2&& arg2) {
279 return std::unique_ptr<_Type>(
new _Type(std::forward<_Arg1>(arg1), std::forward<_Arg2>(arg2)));
282 template <
typename _Type,
typename _Arg1,
typename _Arg2,
typename _Arg3>
283 std::unique_ptr<_Type> make_unique(_Arg1&& arg1, _Arg2&& arg2, _Arg3&& arg3) {
284 return std::unique_ptr<_Type>(
new _Type(std::forward<_Arg1>(arg1), std::forward<_Arg2>(arg2), std::forward<_Arg3>(arg3)));
287 template <
typename _Type,
typename _Arg1,
typename _Arg2,
typename _Arg3,
typename _Arg4>
288 std::unique_ptr<_Type> make_unique(_Arg1&& arg1, _Arg2&& arg2, _Arg3&& arg3, _Arg4&& arg4) {
289 return std::unique_ptr<_Type>(
new _Type(std::forward<_Arg1>(arg1), std::forward<_Arg2>(arg2), std::forward<_Arg3>(arg3), std::forward<_Arg4>(arg4)));
298 inline bool str_icmp(
const utility::string_t &left,
const utility::string_t &right)
301 return _wcsicmp(left.c_str(), right.c_str()) == 0;
303 return boost::iequals(left, right);
312 class windows_category_impl :
public std::error_category
315 virtual const char *name() const CPPREST_NOEXCEPT {
return "windows"; }
317 _ASYNCRTIMP
virtual std::string message(
int errorCode)
const CPPREST_NOEXCEPT;
319 _ASYNCRTIMP
virtual std::error_condition default_error_condition(
int errorCode)
const CPPREST_NOEXCEPT;
326 _ASYNCRTIMP
const std::error_category & __cdecl windows_category();
334 _ASYNCRTIMP
const std::error_category & __cdecl linux_category();
341 _ASYNCRTIMP
const std::error_category & __cdecl platform_category();
346 inline std::system_error __cdecl create_system_error(
unsigned long errorCode)
348 std::error_code code((
int)errorCode, platform_category());
349 return std::system_error(code, code.message());
355 inline std::error_code __cdecl create_error_code(
unsigned long errorCode)
357 return std::error_code((
int)errorCode, platform_category());
363 inline utility::string_t __cdecl create_error_message(
unsigned long errorCode)
373 typedef uint64_t interval_type;
388 enum:interval_type { utc_timestamp_invalid =
static_cast<interval_type
>(-1) };
397 if (seconds >= 11644473600LL)
399 return seconds - 11644473600LL;
403 return utc_timestamp_invalid;
415 static _ASYNCRTIMP datetime __cdecl
from_string(
const utility::string_t& timestring,
date_format format = RFC_1123);
430 datetime operator- (interval_type value)
const
432 return datetime(m_interval - value);
435 datetime operator+ (interval_type value)
const
437 return datetime(m_interval + value);
440 bool operator== (datetime dt)
const
442 return m_interval == dt.m_interval;
445 bool operator!= (
const datetime& dt)
const
447 return !(*
this == dt);
450 static interval_type from_milliseconds(
unsigned int milliseconds)
452 return milliseconds*_msTicks;
455 static interval_type from_seconds(
unsigned int seconds)
457 return seconds*_secondTicks;
460 static interval_type from_minutes(
unsigned int minutes)
462 return minutes*_minuteTicks;
465 static interval_type from_hours(
unsigned int hours)
467 return hours*_hourTicks;
470 static interval_type from_days(
unsigned int days)
472 return days*_dayTicks;
475 bool is_initialized()
const
477 return m_interval != 0;
482 friend int operator- (datetime t1, datetime t2);
484 static const interval_type _msTicks =
static_cast<interval_type
>(10000);
485 static const interval_type _secondTicks = 1000*_msTicks;
486 static const interval_type _minuteTicks = 60*_secondTicks;
487 static const interval_type _hourTicks = 60*60*_secondTicks;
488 static const interval_type _dayTicks = 24*60*60*_secondTicks;
493 static _ASYNCRTIMP
bool __cdecl datetime::system_type_to_datetime(
void* psysTime, uint64_t seconds, datetime * pdt);
495 static datetime timeval_to_datetime(
const timeval &time);
499 datetime(interval_type interval) : m_interval(interval)
504 interval_type m_interval;
515 static int icmp(std::string left, std::string right)
518 for (i = 0; i < left.size(); ++i)
520 if (i == right.size())
return 1;
522 auto l = cmp::tolower(left[i]);
523 auto r = cmp::tolower(right[i]);
525 if (l < r)
return -1;
527 if (i < right.size())
return -1;
532 static char tolower(
char c)
534 if (c >=
'A' && c <=
'Z')
535 return static_cast<char>(c -
'A' +
'a');
544 auto diff = (t1.m_interval - t2.m_interval);
547 diff /= 10 * 1000 * 1000;
549 return static_cast<int>(diff);
562 enum { default_length = 32 };
578 _ASYNCRTIMP utility::string_t generate();
593 static const utility::string_t c_allowed_chars;
594 std::mt19937 m_random;
_ASYNCRTIMP std::string __cdecl utf16_to_utf8(const utf16string &w)
Converts a UTF-16 string to a UTF-8 string.
_ASYNCRTIMP utility::string_t __cdecl seconds_to_xml_duration(utility::seconds numSecs)
Converts a timespan/interval in seconds to xml duration string as specified by http://www.w3.org/TR/xmlschema-2/#duration
Definition: asyncrt_utils.h:511
static _ASYNCRTIMP datetime __cdecl utc_now()
Returns the current UTC time.
Cross platform RAII container for setting thread local locale.
Definition: asyncrt_utils.h:226
_ASYNCRTIMP utility::string_t to_string(date_format format=RFC_1123) const
Returns a string representation of the datetime.
static interval_type utc_timestamp()
Returns seconds since Unix/POSIX time epoch at 01-01-1970 00:00:00. If time is before epoch...
Definition: asyncrt_utils.h:394
nonce_generator(int length=default_length)
Nonce generator constructor.
Definition: asyncrt_utils.h:568
Definition: asyncrt_utils.h:370
_ASYNCRTIMP utility::string_t __cdecl to_base64(const std::vector< unsigned char > &data)
Encode the given byte array into a base64 string
interval_type to_interval() const
Returns the integral time value.
Definition: asyncrt_utils.h:425
void set_length(int length)
Set length of the generated nonce string.
Definition: asyncrt_utils.h:590
_ASYNCRTIMP utf16string __cdecl utf8_to_utf16(const std::string &s)
Converts a UTF-8 string to a UTF-16
static _ASYNCRTIMP datetime __cdecl from_string(const utility::string_t ×tring, date_format format=RFC_1123)
Creates datetime from a string representing time in UTC in RFC 1123 format.
int length() const
Get length of generated nonce string.
Definition: asyncrt_utils.h:584
_ASYNCRTIMP utility::string_t __cdecl to_string_t(std::string &&s)
Converts to a platform dependent Unicode string type.
date_format
Defines the supported date and time string formats.
Definition: asyncrt_utils.h:378
_ASYNCRTIMP utf16string __cdecl to_utf16string(const std::string &value)
Converts to a UTF-16 from string.
_ASYNCRTIMP utility::seconds __cdecl xml_duration_to_seconds(const utility::string_t ×panString)
Converts an xml duration to timespan/interval in seconds http://www.w3.org/TR/xmlschema-2/#duration ...
_ASYNCRTIMP utf16string __cdecl usascii_to_utf16(const std::string &s)
Converts a ASCII (us-ascii) string to a UTF-16 string.
Nonce string generator class.
Definition: asyncrt_utils.h:555
_ASYNCRTIMP std::vector< unsigned char > __cdecl from_base64(const utility::string_t &str)
Decode the given base64 string to a byte array
_ASYNCRTIMP utf16string __cdecl latin1_to_utf16(const std::string &s)
Converts a Latin1 (iso-8859-1) string to a UTF-16 string.
_ASYNCRTIMP utf8string __cdecl latin1_to_utf8(const std::string &s)
Converts a Latin1 (iso-8859-1) string to a UTF-8 string.
_ASYNCRTIMP std::string __cdecl to_utf8string(std::string value)
Converts to a UTF-8 string.
Various utilities for string conversions and date and time manipulation.
Definition: asyncrt_utils.h:50