@@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include <assert.h>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
@@ -14,6 +14,35 @@
|
||||
|
||||
namespace stdex
|
||||
{
|
||||
/// \cond internal
|
||||
static const char base64_enc_lookup[64] = {
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
|
||||
};
|
||||
|
||||
static const uint8_t base64_dec_lookup[256] = {
|
||||
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
||||
/* 0 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* 1 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* 2 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
|
||||
/* 3 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 64, 255, 255,
|
||||
/* 4 */ 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||
/* 5 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
|
||||
/* 6 */ 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
/* 7 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255,
|
||||
/* 8 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* 9 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* A */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* B */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* C */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* D */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* E */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* F */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
|
||||
};
|
||||
/// \endcond
|
||||
|
||||
///
|
||||
/// Base64 encoding session
|
||||
///
|
||||
@@ -140,17 +169,6 @@ namespace stdex
|
||||
size_t num; ///< Number of bytes used in `buf`
|
||||
};
|
||||
|
||||
|
||||
/// \cond internal
|
||||
static const char base64_enc_lookup[64] = {
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
|
||||
};
|
||||
/// \endcond
|
||||
|
||||
|
||||
///
|
||||
/// Base64 decoding session
|
||||
///
|
||||
@@ -256,27 +274,4 @@ namespace stdex
|
||||
uint8_t buf[4]; ///< Internal buffer
|
||||
size_t num; ///< Number of bytes used in `buf`
|
||||
};
|
||||
|
||||
|
||||
/// \cond internal
|
||||
static const uint8_t base64_dec_lookup[256] = {
|
||||
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
||||
/* 0 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* 1 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* 2 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
|
||||
/* 3 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 64, 255, 255,
|
||||
/* 4 */ 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||
/* 5 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
|
||||
/* 6 */ 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
/* 7 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255,
|
||||
/* 8 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* 9 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* A */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* B */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* C */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* D */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* E */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* F */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
|
||||
};
|
||||
/// \endcond
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include "system.hpp"
|
||||
#include "string.hpp"
|
||||
#include <stdint.h>
|
||||
|
@@ -1,13 +1,15 @@
|
||||
/*
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2022-2023 Amebis
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#ifdef _WIN32
|
||||
#include <sal.h>
|
||||
#endif
|
||||
#include <type_traits>
|
||||
|
||||
#ifndef _In_
|
||||
#define _In_
|
||||
@@ -30,15 +32,24 @@
|
||||
#ifndef _In_z_
|
||||
#define _In_z_
|
||||
#endif
|
||||
#ifndef _In_opt_z_
|
||||
#define _In_opt_z_
|
||||
#endif
|
||||
#ifndef _In_z_count_
|
||||
#define _In_z_count_(p)
|
||||
#endif
|
||||
#ifndef _In_reads_
|
||||
#define _In_reads_(p)
|
||||
#endif
|
||||
#ifndef _In_reads_or_z_
|
||||
#define _In_reads_or_z_(p)
|
||||
#endif
|
||||
#ifndef _In_reads_or_z_opt_
|
||||
#define _In_reads_or_z_opt_(p)
|
||||
#endif
|
||||
#ifndef _In_reads_bytes_opt_
|
||||
#define _In_reads_bytes_opt_(p)
|
||||
#endif
|
||||
#ifndef _Printf_format_string_params_
|
||||
#define _Printf_format_string_params_(n)
|
||||
#endif
|
||||
@@ -46,6 +57,12 @@
|
||||
#ifndef _Inout_
|
||||
#define _Inout_
|
||||
#endif
|
||||
#ifndef _Inout_opt_
|
||||
#define _Inout_opt_
|
||||
#endif
|
||||
#ifndef _Inout_cap_
|
||||
#define _Inout_cap_(p)
|
||||
#endif
|
||||
|
||||
#ifndef _Use_decl_annotations_
|
||||
#define _Use_decl_annotations_
|
||||
@@ -57,19 +74,47 @@
|
||||
#ifndef _Out_opt_
|
||||
#define _Out_opt_
|
||||
#endif
|
||||
#ifndef _Out_z_cap_
|
||||
#define _Out_z_cap_(p)
|
||||
#endif
|
||||
#ifndef _Out_writes_
|
||||
#define _Out_writes_(p)
|
||||
#endif
|
||||
#ifndef _Out_writes_bytes_
|
||||
#define _Out_writes_bytes_(p)
|
||||
#endif
|
||||
#ifndef _Out_writes_z_
|
||||
#define _Out_writes_z_(p)
|
||||
#endif
|
||||
#ifndef _Out_writes_bytes_to_opt_
|
||||
#define _Out_writes_bytes_to_opt_(p, q)
|
||||
#endif
|
||||
|
||||
#ifndef _Success_
|
||||
#define _Success_(p)
|
||||
#endif
|
||||
#ifndef _Ret_maybenull_z_
|
||||
#define _Ret_maybenull_z_
|
||||
#endif
|
||||
#ifndef _Ret_notnull_
|
||||
#define _Ret_notnull_
|
||||
#endif
|
||||
#ifndef _Ret_z_
|
||||
#define _Ret_z_
|
||||
#endif
|
||||
#ifndef _Must_inspect_result_
|
||||
#define _Must_inspect_result_
|
||||
#endif
|
||||
#ifndef _Check_return_
|
||||
#define _Check_return_
|
||||
#endif
|
||||
#ifndef _Post_maybez_
|
||||
#define _Post_maybez_
|
||||
#endif
|
||||
|
||||
#ifndef _Analysis_assume_
|
||||
#define _Analysis_assume_(p)
|
||||
#endif
|
||||
|
||||
#ifndef _Likely_
|
||||
#if _HAS_CXX20
|
||||
@@ -98,3 +143,19 @@
|
||||
#else
|
||||
#define _Unreferenced_(x)
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
template <typename T, size_t N>
|
||||
size_t _countof(T (&arr)[N])
|
||||
{
|
||||
return std::extent<T[N]>::value;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
#define off64_t off_t
|
||||
#define lseek64 lseek
|
||||
#define lockf64 lockf
|
||||
#define ftruncate64 ftruncate
|
||||
#endif
|
||||
|
@@ -1,67 +1,87 @@
|
||||
/*
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2023 Amebis
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include "system.hpp"
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef LITTLE_ENDIAN
|
||||
#define LITTLE_ENDIAN 1234
|
||||
#endif
|
||||
#ifndef BIG_ENDIAN
|
||||
#define BIG_ENDIAN 4321
|
||||
#endif
|
||||
#ifndef BYTE_ORDER
|
||||
#if defined(_WIN32)
|
||||
#if REG_DWORD == REG_DWORD_LITTLE_ENDIAN
|
||||
#define BYTE_ORDER LITTLE_ENDIAN
|
||||
#elif REG_DWORD == REG_DWORD_BIG_ENDIAN
|
||||
#define BIG_ENDIAN
|
||||
#else
|
||||
#error Unknown endian
|
||||
#define BYTE_ORDER BIG_ENDIAN
|
||||
#endif
|
||||
#elif defined(__APPLE__)
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
#define BYTE_ORDER LITTLE_ENDIAN
|
||||
#elif __BYTE_ORDER == __ORDER_BIG_ENDIAN__
|
||||
#define BYTE_ORDER BIG_ENDIAN
|
||||
#endif
|
||||
#else
|
||||
#include <endian.h>
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#define BYTE_ORDER LITTLE_ENDIAN
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define BIG_ENDIAN
|
||||
#else
|
||||
#define BYTE_ORDER BIG_ENDIAN
|
||||
#endif
|
||||
#endif
|
||||
#ifndef BYTE_ORDER
|
||||
#error Unknown endian
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace stdex
|
||||
{
|
||||
inline uint8_t byteswap(_In_ const uint8_t value)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
inline uint16_t byteswap(_In_ const uint16_t value)
|
||||
{
|
||||
#if _MSC_VER >= 1300
|
||||
#if _MSC_VER >= 1300
|
||||
return _byteswap_ushort(value);
|
||||
#elif defined(_MSC_VER)
|
||||
#elif defined(_MSC_VER)
|
||||
uint16_t t = (value & 0x00ff) << 8;
|
||||
t |= (value) >> 8;
|
||||
return t;
|
||||
#else
|
||||
#else
|
||||
return __builtin_bswap16(value);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
inline uint32_t byteswap(_In_ const uint32_t value)
|
||||
{
|
||||
#if _MSC_VER >= 1300
|
||||
#if _MSC_VER >= 1300
|
||||
return _byteswap_ulong(value);
|
||||
#elif defined(_MSC_VER)
|
||||
#elif defined(_MSC_VER)
|
||||
uint32_t t = (value & 0x000000ff) << 24;
|
||||
t |= (value & 0x0000ff00) << 8;
|
||||
t |= (value & 0x00ff0000) >> 8;
|
||||
t |= (value) >> 24;
|
||||
return t;
|
||||
#else
|
||||
#else
|
||||
return __builtin_bswap32(value);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
inline uint64_t byteswap(_In_ const uint64_t value)
|
||||
{
|
||||
#if _MSC_VER >= 1300
|
||||
#if _MSC_VER >= 1300
|
||||
return _byteswap_uint64(value);
|
||||
#elif defined(_MSC_VER)
|
||||
#elif defined(_MSC_VER)
|
||||
uint64_t t = (value & 0x00000000000000ff) << 56;
|
||||
t |= (value & 0x000000000000ff00) << 40;
|
||||
t |= (value & 0x0000000000ff0000) << 24;
|
||||
@@ -71,25 +91,34 @@ namespace stdex
|
||||
t |= (value & 0x00ff000000000000) >> 40;
|
||||
t |= (value) >> 56;
|
||||
return t;
|
||||
#else
|
||||
#else
|
||||
return __builtin_bswap64(value);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
inline int16_t byteswap(_In_ const int16_t value) { return byteswap((uint16_t)value); }
|
||||
inline int32_t byteswap(_In_ const int32_t value) { return byteswap((uint32_t)value); }
|
||||
inline int64_t byteswap(_In_ const int64_t value) { return byteswap((uint64_t)value); }
|
||||
inline int8_t byteswap(_In_ const char value) { return byteswap(static_cast<uint8_t>(value)); }
|
||||
inline int8_t byteswap(_In_ const int8_t value) { return byteswap(static_cast<uint8_t>(value)); }
|
||||
inline int16_t byteswap(_In_ const int16_t value) { return byteswap(static_cast<uint16_t>(value)); }
|
||||
inline int32_t byteswap(_In_ const int32_t value) { return byteswap(static_cast<uint32_t>(value)); }
|
||||
inline int64_t byteswap(_In_ const int64_t value) { return byteswap(static_cast<uint64_t>(value)); }
|
||||
inline float byteswap(_In_ const float value) { return byteswap(*reinterpret_cast<const uint32_t*>(&value)); }
|
||||
inline double byteswap(_In_ const double value) { return byteswap(*reinterpret_cast<const uint64_t*>(&value)); }
|
||||
|
||||
inline void byteswap(_Inout_ uint8_t* value) { assert(value); *value = byteswap(*value); }
|
||||
inline void byteswap(_Inout_ uint16_t* value) { assert(value); *value = byteswap(*value); }
|
||||
inline void byteswap(_Inout_ uint32_t* value) { assert(value); *value = byteswap(*value); }
|
||||
inline void byteswap(_Inout_ uint64_t* value) { assert(value); *value = byteswap(*value); }
|
||||
|
||||
inline void byteswap(_Inout_ int16_t* value) { byteswap((uint16_t*)value); }
|
||||
inline void byteswap(_Inout_ int32_t* value) { byteswap((uint32_t*)value); }
|
||||
inline void byteswap(_Inout_ int64_t* value) { byteswap((uint64_t*)value); }
|
||||
inline void byteswap(_Inout_ char* value) { byteswap(reinterpret_cast<uint8_t*>(value)); }
|
||||
inline void byteswap(_Inout_ int8_t* value) { byteswap(reinterpret_cast<uint8_t*>(value)); }
|
||||
inline void byteswap(_Inout_ int16_t* value) { byteswap(reinterpret_cast<uint16_t*>(value)); }
|
||||
inline void byteswap(_Inout_ int32_t* value) { byteswap(reinterpret_cast<uint32_t*>(value)); }
|
||||
inline void byteswap(_Inout_ int64_t* value) { byteswap(reinterpret_cast<uint64_t*>(value)); }
|
||||
inline void byteswap(_Inout_ float* value) { byteswap(reinterpret_cast<uint32_t*>(value)); }
|
||||
inline void byteswap(_Inout_ double* value) { byteswap(reinterpret_cast<uint64_t*>(value)); }
|
||||
}
|
||||
|
||||
#ifdef BIG_ENDIAN
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
#define LE2HE(x) stdex::byteswap(x)
|
||||
#define BE2HE(x) (x)
|
||||
#define HE2LE(x) stdex::byteswap(x)
|
||||
|
@@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
|
||||
|
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2023 Amebis
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include <exception>
|
||||
|
||||
namespace stdex
|
||||
@@ -13,7 +13,7 @@ namespace stdex
|
||||
///
|
||||
/// User cancelled exception
|
||||
///
|
||||
class user_cancelled : public std::exception
|
||||
class user_cancelled : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
///
|
||||
@@ -21,8 +21,8 @@ namespace stdex
|
||||
///
|
||||
/// \param[in] msg Error message
|
||||
///
|
||||
user_cancelled(_In_opt_z_ const char *msg = nullptr) : exception(msg)
|
||||
user_cancelled(_In_opt_z_ const char *msg = nullptr) : runtime_error(msg)
|
||||
{
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include <assert.h>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
@@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include "stream.hpp"
|
||||
#include <ios>
|
||||
#include <istream>
|
||||
|
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2023 Amebis
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace stdex
|
||||
@@ -78,7 +78,7 @@ namespace stdex
|
||||
/// Adds two intervals by components
|
||||
///
|
||||
/// \param[in] a First interval
|
||||
/// \param[in] a Second interval
|
||||
/// \param[in] b Second interval
|
||||
///
|
||||
/// \returns Resulting interval
|
||||
///
|
||||
@@ -137,7 +137,7 @@ inline stdex::interval<T> operator++(_Inout_ stdex::interval<T>& i, int) // Post
|
||||
/// Subtracts two intervals by components
|
||||
///
|
||||
/// \param[in] a First interval
|
||||
/// \param[in] a Second interval
|
||||
/// \param[in] b Second interval
|
||||
///
|
||||
/// \returns Resulting interval
|
||||
///
|
||||
|
@@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace stdex
|
||||
|
@@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include "system.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2023 Amebis
|
||||
*/
|
||||
@@ -19,7 +19,7 @@ namespace stdex
|
||||
template <class T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
|
||||
inline no_delete(const no_delete<T2>&) noexcept {}
|
||||
|
||||
inline void operator()(T* p) const noexcept { p; }
|
||||
inline void operator()(T* p) const noexcept { _Unreferenced_(p); }
|
||||
};
|
||||
|
||||
///
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2023 Amebis
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include "interval.hpp"
|
||||
#include <chrono>
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace stdex
|
||||
///
|
||||
virtual void set_text(_In_z_ const char* msg)
|
||||
{
|
||||
msg;
|
||||
_Unreferenced_(msg);
|
||||
}
|
||||
|
||||
///
|
||||
@@ -56,7 +56,7 @@ namespace stdex
|
||||
///
|
||||
virtual void show(_In_ bool show = true)
|
||||
{
|
||||
show;
|
||||
_Unreferenced_(show);
|
||||
}
|
||||
|
||||
///
|
||||
@@ -168,7 +168,7 @@ namespace stdex
|
||||
///
|
||||
inline progress<T>* detach()
|
||||
{
|
||||
progress* k = m_host;
|
||||
progress<T>* k = m_host;
|
||||
m_host = NULL;
|
||||
return k;
|
||||
}
|
||||
@@ -258,7 +258,7 @@ namespace stdex
|
||||
}
|
||||
|
||||
protected:
|
||||
progress* m_host;
|
||||
progress<T>* m_host;
|
||||
interval<T> m_local, m_global, m_section;
|
||||
};
|
||||
|
||||
@@ -281,10 +281,10 @@ namespace stdex
|
||||
|
||||
~progress_switcher()
|
||||
{
|
||||
m_host_ref = detach();
|
||||
m_host_ref = this->detach();
|
||||
}
|
||||
|
||||
protected:
|
||||
progress<T>*& m_host_ref;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include <assert.h>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2023 Amebis
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "compat.hpp"
|
||||
#include "mapping.hpp"
|
||||
#include "sal.hpp"
|
||||
#include "sgml_unicode.hpp"
|
||||
#include "string.hpp"
|
||||
#include <assert.h>
|
||||
@@ -173,7 +173,8 @@ namespace stdex
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline _Deprecated_("Use stdex::sgml2wstrcat") void sgml2wstr(
|
||||
_Deprecated_("Use stdex::sgml2wstrcat")
|
||||
inline void sgml2wstr(
|
||||
_Inout_ std::wstring& dst,
|
||||
_In_reads_or_z_opt_(count_src) const T* src, _In_ size_t count_src,
|
||||
_In_ int skip = 0,
|
||||
@@ -192,8 +193,6 @@ namespace stdex
|
||||
/// \param[in] offset Logical starting offset of source and destination strings. Unused when map parameter is nullptr.
|
||||
/// \param[in,out] map The vector to append index mapping between source and destination string to.
|
||||
///
|
||||
/// \return Unicode string
|
||||
///
|
||||
template <class T>
|
||||
inline void sgml2wstrcat(
|
||||
_Inout_ std::wstring& dst,
|
||||
@@ -206,7 +205,8 @@ namespace stdex
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline _Deprecated_("Use stdex::sgml2wstrcat") void sgml2wstr(
|
||||
_Deprecated_("Use stdex::sgml2wstrcat")
|
||||
inline void sgml2wstr(
|
||||
_Inout_ std::wstring& dst,
|
||||
_In_ const std::basic_string<T>& src,
|
||||
_In_ int skip = 0,
|
||||
@@ -324,7 +324,8 @@ namespace stdex
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline _Deprecated_("Use stdex::sgml2wstrcat") size_t sgml2wstr(
|
||||
_Deprecated_("Use stdex::sgml2wstrcat")
|
||||
inline size_t sgml2wstr(
|
||||
_Inout_cap_(count_dst) wchar_t* dst, _In_ size_t count_dst,
|
||||
_In_reads_or_z_opt_(count_src) const T* src, _In_ size_t count_src,
|
||||
_In_ int skip = 0,
|
||||
@@ -582,7 +583,8 @@ namespace stdex
|
||||
}
|
||||
}
|
||||
|
||||
inline _Deprecated_("Use stdex::wstr2sgmlcat") void wstr2sgml(
|
||||
_Deprecated_("Use stdex::wstr2sgmlcat")
|
||||
inline void wstr2sgml(
|
||||
_Inout_ std::string& dst,
|
||||
_In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
|
||||
_In_ size_t what = 0)
|
||||
@@ -605,7 +607,8 @@ namespace stdex
|
||||
wstr2sgmlcat(dst, src.c_str(), src.size(), what);
|
||||
}
|
||||
|
||||
inline _Deprecated_("Use stdex::wstr2sgmlcat") void wstr2sgml(
|
||||
_Deprecated_("Use stdex::wstr2sgmlcat")
|
||||
inline void wstr2sgml(
|
||||
_Inout_ std::string& dst,
|
||||
_In_ const std::wstring& src,
|
||||
_In_ size_t what = 0)
|
||||
@@ -745,7 +748,8 @@ namespace stdex
|
||||
return j;
|
||||
}
|
||||
|
||||
inline _Deprecated_("Use stdex::wstr2sgmlcat") size_t wstr2sgml(
|
||||
_Deprecated_("Use stdex::wstr2sgmlcat")
|
||||
inline size_t wstr2sgml(
|
||||
_Inout_cap_(count_dst) char* dst, _In_ size_t count_dst,
|
||||
_In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
|
||||
_In_ size_t what = 0)
|
||||
|
@@ -1,15 +1,15 @@
|
||||
/*
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2023 Amebis
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "compat.hpp"
|
||||
#include "endian.hpp"
|
||||
#include "interval.hpp"
|
||||
#include "math.hpp"
|
||||
#include "ring.hpp"
|
||||
#include "sal.hpp"
|
||||
#include "string.hpp"
|
||||
#include "system.hpp"
|
||||
#include "unicode.hpp"
|
||||
@@ -19,6 +19,10 @@
|
||||
#if defined(_WIN32)
|
||||
#include <asptlb.h>
|
||||
#include <objidl.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
@@ -254,7 +258,7 @@ namespace stdex
|
||||
return *this;
|
||||
}
|
||||
if (read_array(&data, sizeof(T), 1) == 1)
|
||||
LE2HE(&data);
|
||||
(void)LE2HE(&data);
|
||||
else {
|
||||
data = 0;
|
||||
if (ok())
|
||||
@@ -279,7 +283,7 @@ namespace stdex
|
||||
{
|
||||
if (!ok()) _Unlikely_
|
||||
return *this;
|
||||
#ifdef BIG_ENDIAN
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
T data_le = HE2LE(data);
|
||||
write(&data_le, sizeof(T));
|
||||
#else
|
||||
@@ -630,10 +634,6 @@ namespace stdex
|
||||
inline basic& operator <<(_In_ const uint32_t data) { return write_data(data); }
|
||||
inline basic& operator >>(_Out_ uint64_t& data) { return read_data(data); }
|
||||
inline basic& operator <<(_In_ const uint64_t data) { return write_data(data); }
|
||||
#if defined(_WIN64) && defined(_NATIVE_SIZE_T_DEFINED)
|
||||
inline basic& operator >>(_Out_ size_t& data) { return read_data(data); }
|
||||
inline basic& operator <<(_In_ const size_t data) { return write_data(data); }
|
||||
#endif
|
||||
inline basic& operator >>(_Out_ float& data) { return read_data(data); }
|
||||
inline basic& operator <<(_In_ const float data) { return write_data(data); }
|
||||
inline basic& operator >>(_Out_ double& data) { return read_data(data); }
|
||||
@@ -683,10 +683,11 @@ namespace stdex
|
||||
};
|
||||
|
||||
#if _HAS_CXX20
|
||||
using time_point = std::chrono::time_point<std::chrono::file_clock>;
|
||||
using clock = std::chrono::file_clock;
|
||||
#else
|
||||
using time_point = std::chrono::time_point<std::chrono::system_clock>;
|
||||
using clock = std::chrono::system_clock;
|
||||
#endif
|
||||
using time_point = std::chrono::time_point<clock>;
|
||||
|
||||
///
|
||||
/// Basic seekable stream operations
|
||||
@@ -755,7 +756,7 @@ namespace stdex
|
||||
{
|
||||
_Unreferenced_(offset);
|
||||
_Unreferenced_(length);
|
||||
throw std::exception("not implemented");
|
||||
throw std::domain_error("not implemented");
|
||||
}
|
||||
|
||||
///
|
||||
@@ -765,7 +766,7 @@ namespace stdex
|
||||
{
|
||||
_Unreferenced_(offset);
|
||||
_Unreferenced_(length);
|
||||
throw std::exception("not implemented");
|
||||
throw std::domain_error("not implemented");
|
||||
}
|
||||
|
||||
///
|
||||
@@ -809,7 +810,7 @@ namespace stdex
|
||||
virtual void set_ctime(time_point date)
|
||||
{
|
||||
_Unreferenced_(date);
|
||||
throw std::exception("not implemented");
|
||||
throw std::domain_error("not implemented");
|
||||
}
|
||||
|
||||
///
|
||||
@@ -818,7 +819,7 @@ namespace stdex
|
||||
virtual void set_atime(time_point date)
|
||||
{
|
||||
_Unreferenced_(date);
|
||||
throw std::exception("not implemented");
|
||||
throw std::domain_error("not implemented");
|
||||
}
|
||||
|
||||
///
|
||||
@@ -827,7 +828,7 @@ namespace stdex
|
||||
virtual void set_mtime(time_point date)
|
||||
{
|
||||
_Unreferenced_(date);
|
||||
throw std::exception("not implemented");
|
||||
throw std::domain_error("not implemented");
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -855,7 +856,7 @@ namespace stdex
|
||||
///
|
||||
/// \param[in] default_charset Fallback charset to return when no BOM detected.
|
||||
///
|
||||
charset_id read_charset(_In_ charset_id default_charset = charset_id::default)
|
||||
charset_id read_charset(_In_ charset_id default_charset = charset_id::system)
|
||||
{
|
||||
if (seek(0) != 0)
|
||||
throw std::runtime_error("failed to seek");
|
||||
@@ -1057,6 +1058,7 @@ namespace stdex
|
||||
case op_t::flush:
|
||||
w.source->flush();
|
||||
break;
|
||||
case op_t::noop:;
|
||||
}
|
||||
w.op = op_t::noop;
|
||||
lk.unlock();
|
||||
@@ -2096,8 +2098,7 @@ namespace stdex
|
||||
}
|
||||
if (!succeeded) _Unlikely_
|
||||
#else
|
||||
ssize_t num_read = static_cast<ssize_t>(std::min<size_t>(to_read, block_size));
|
||||
num_read = read(m_h, data, num_read);
|
||||
ssize_t num_read = ::read(m_h, data, static_cast<ssize_t>(std::min<size_t>(to_read, block_size)));
|
||||
if (num_read < 0) _Unlikely_
|
||||
#endif
|
||||
{
|
||||
@@ -2149,7 +2150,7 @@ namespace stdex
|
||||
return length - to_write;
|
||||
}
|
||||
#else
|
||||
ssize_t num_written = write(m_h, data, static_cast<ssize_t>(std::min<size_t>(to_write, block_size)));
|
||||
ssize_t num_written = ::write(m_h, data, static_cast<ssize_t>(std::min<size_t>(to_write, block_size)));
|
||||
if (num_written < 0) _Unlikely_ {
|
||||
m_state = state_t::fail;
|
||||
return length - to_write;
|
||||
@@ -2487,13 +2488,18 @@ namespace stdex
|
||||
m_h = CreateFile(filename, dwDesiredAccess, dwShareMode, &sa, dwCreationDisposition, dwFlagsAndAttributes, nullptr);
|
||||
#else
|
||||
int flags = 0;
|
||||
if (mode & mode_for_reading) flags |= O_RDONLY;
|
||||
if (mode & mode_for_writing) flags |= O_WRONLY;
|
||||
switch (mode & (mode_for_reading | mode_for_writing)) {
|
||||
case mode_for_reading: flags |= O_RDONLY; break;
|
||||
case mode_for_writing: flags |= O_WRONLY; break;
|
||||
case mode_for_reading | mode_for_writing: flags |= O_RDWR; break;
|
||||
}
|
||||
if (mode & mode_create) flags |= mode & mode_preserve_existing ? O_CREAT : (O_CREAT | O_EXCL);
|
||||
if (mode & hint_write_thru) flags |= O_DSYNC;
|
||||
#ifndef __APPLE__
|
||||
if (mode & hint_no_buffering) flags |= O_RSYNC;
|
||||
#endif
|
||||
|
||||
m_h = open(filename, flags, DEFFILEMODE);
|
||||
m_h = ::open(filename, flags, DEFFILEMODE);
|
||||
#endif
|
||||
if (m_h != invalid_handle) {
|
||||
m_state = state_t::ok;
|
||||
@@ -2515,7 +2521,7 @@ namespace stdex
|
||||
return li.QuadPart;
|
||||
}
|
||||
#else
|
||||
off64_t result = lseek64(m_h, offset, how);
|
||||
off64_t result = lseek64(m_h, offset, static_cast<int>(how));
|
||||
if (result >= 0) {
|
||||
m_state = state_t::ok;
|
||||
return result;
|
||||
@@ -2667,8 +2673,8 @@ namespace stdex
|
||||
return ft2tp(ft);
|
||||
#else
|
||||
struct stat buf;
|
||||
if (fstat(m_h, &buf) >= 0);
|
||||
return time_point::from_time_t(buf.st_atim);
|
||||
if (fstat(m_h, &buf) >= 0)
|
||||
return clock::from_time_t(buf.st_atime);
|
||||
#endif
|
||||
return time_point::min();
|
||||
}
|
||||
@@ -2682,7 +2688,7 @@ namespace stdex
|
||||
#else
|
||||
struct stat buf;
|
||||
if (fstat(m_h, &buf) >= 0)
|
||||
return time_point::from_time_t(buf.st_mtim);
|
||||
return clock::from_time_t(buf.st_mtime);
|
||||
#endif
|
||||
return time_point::min();
|
||||
}
|
||||
@@ -2708,9 +2714,10 @@ namespace stdex
|
||||
if (SetFileTime(m_h, nullptr, &ft, nullptr))
|
||||
return;
|
||||
#else
|
||||
struct timespec ts[2];
|
||||
ts[0].tv_sec = date;
|
||||
ts[1].tv_nsec = UTIME_OMIT;
|
||||
struct timespec ts[2] = {
|
||||
{ date.time_since_epoch().count(), 0 },
|
||||
{ 0, UTIME_OMIT },
|
||||
};
|
||||
if (futimens(m_h, ts) >= 0)
|
||||
return;
|
||||
#endif
|
||||
@@ -2725,9 +2732,10 @@ namespace stdex
|
||||
if (SetFileTime(m_h, nullptr, nullptr, &ft))
|
||||
return;
|
||||
#else
|
||||
struct timespec ts[2];
|
||||
ts[0].tv_nsec = UTIME_OMIT;
|
||||
ts[1].tv_sec = date;
|
||||
struct timespec ts[2] = {
|
||||
{ 0, UTIME_OMIT },
|
||||
{ date.time_since_epoch().count(), 0 },
|
||||
};
|
||||
if (futimens(m_h, ts) >= 0)
|
||||
return;
|
||||
#endif
|
||||
@@ -2773,7 +2781,6 @@ namespace stdex
|
||||
///
|
||||
/// \param[in] filename Filename
|
||||
/// \param[in] mode Bitwise combination of mode_t flags
|
||||
/// \param[in] cache_size Size of the cache block
|
||||
///
|
||||
void open(_In_z_ const schar_t* filename, _In_ int mode)
|
||||
{
|
||||
@@ -3077,7 +3084,7 @@ namespace stdex
|
||||
if (end_offset <= m_size) {
|
||||
uint32_t num_chars = LE2HE(*reinterpret_cast<uint32_t*>(m_data + m_offset));
|
||||
m_offset = end_offset;
|
||||
end_offset = stdex::add(m_offset + stdex::mul(num_chars, sizeof(_Elem)));
|
||||
end_offset = stdex::add(m_offset, stdex::mul(num_chars, sizeof(_Elem)));
|
||||
_Elem* start = reinterpret_cast<_Elem*>(m_data + m_offset);
|
||||
if (end_offset <= m_size) {
|
||||
data.assign(start, start + num_chars);
|
||||
@@ -3208,7 +3215,7 @@ namespace stdex
|
||||
if (!ok()) _Unlikely_
|
||||
return *this;
|
||||
}
|
||||
auto p = tok.m_podatki + m_offset;
|
||||
auto p = m_data + m_offset;
|
||||
*reinterpret_cast<uint32_t*>(p) = HE2LE((uint32_t)num_chars);
|
||||
memcpy(p + sizeof(uint32_t), data, size_chars);
|
||||
m_offset = end_offset;
|
||||
@@ -3384,9 +3391,6 @@ namespace stdex
|
||||
inline void set(_In_ fpos_t offset, _In_ const uint16_t data) { set<uint16_t>(offset, data); }
|
||||
inline void set(_In_ fpos_t offset, _In_ const uint32_t data) { set<uint32_t>(offset, data); }
|
||||
inline void set(_In_ fpos_t offset, _In_ const uint64_t data) { set<uint64_t>(offset, data); }
|
||||
#if defined(_WIN64) && defined(_NATIVE_SIZE_T_DEFINED)
|
||||
inline void set(_In_ fpos_t offset, _In_ const size_t data) { set<size_t>(offset, data); }
|
||||
#endif
|
||||
inline void set(_In_ fpos_t offset, _In_ const float data) { set<float>(offset, data); }
|
||||
inline void set(_In_ fpos_t offset, _In_ const double data) { set<double>(offset, data); }
|
||||
inline void set(_In_ fpos_t offset, _In_ const char data) { set<char>(offset, data); }
|
||||
@@ -3421,9 +3425,6 @@ namespace stdex
|
||||
inline void get(_In_ fpos_t offset, _Out_ uint16_t & data) { get<uint16_t>(offset, data); }
|
||||
inline void get(_In_ fpos_t offset, _Out_ uint32_t & data) { get<uint32_t>(offset, data); }
|
||||
inline void get(_In_ fpos_t offset, _Out_ uint64_t & data) { get<uint64_t>(offset, data); }
|
||||
#if defined(_WIN64) && defined(_NATIVE_SIZE_T_DEFINED)
|
||||
inline void get(_In_ fpos_t offset, _Out_ size_t & data) { get<size_t>(offset, data); }
|
||||
#endif
|
||||
inline void get(_In_ fpos_t offset, _Out_ float& data) { get<float>(offset, data); }
|
||||
inline void get(_In_ fpos_t offset, _Out_ double& data) { get<double>(offset, data); }
|
||||
inline void get(_In_ fpos_t offset, _Out_ char& data) { get<char>(offset, data); }
|
||||
@@ -3447,10 +3448,6 @@ namespace stdex
|
||||
inline memory_file& operator >>(_Out_ uint32_t & data) { return read_data(data); }
|
||||
inline memory_file& operator <<(_In_ const uint64_t data) { return write_data(data); }
|
||||
inline memory_file& operator >>(_Out_ uint64_t & data) { return read_data(data); }
|
||||
#if defined(_WIN64) && defined(_NATIVE_SIZE_T_DEFINED)
|
||||
inline memory_file& operator <<(_In_ const size_t data) { return write_data(data); }
|
||||
inline memory_file& operator >>(_Out_ size_t & data) { return read_data(data); }
|
||||
#endif
|
||||
inline memory_file& operator <<(_In_ const float data) { return write_data(data); }
|
||||
inline memory_file& operator >>(_Out_ float& data) { return read_data(data); }
|
||||
inline memory_file& operator <<(_In_ const double data) { return write_data(data); }
|
||||
|
@@ -1,16 +1,22 @@
|
||||
/*
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2016-2023 Amebis
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <locale.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#ifdef __APPLE__
|
||||
#include <xlocale.h>
|
||||
#endif
|
||||
#include <locale>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -36,19 +42,35 @@ namespace stdex
|
||||
///
|
||||
void operator()(_In_ locale_t locale) const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
free_locale(locale);
|
||||
#else
|
||||
freelocale(locale);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
///
|
||||
/// locale_t helper class to free_locale when going out of scope
|
||||
///
|
||||
#if defined(_WIN32)
|
||||
using locale = std::unique_ptr<__crt_locale_pointers, free_locale_delete>;
|
||||
#elif defined(__APPLE__)
|
||||
using locale = std::unique_ptr<struct _xlocale, free_locale_delete>;
|
||||
#else
|
||||
using locale = std::unique_ptr<struct __locale_struct, free_locale_delete>;
|
||||
#endif
|
||||
|
||||
///
|
||||
/// Reusable C locale
|
||||
///
|
||||
#if defined(_WIN32)
|
||||
static locale locale_C(_create_locale(LC_ALL, "C"));
|
||||
#elif defined(__APPLE__)
|
||||
static locale locale_C(newlocale(LC_ALL_MASK, "C", LC_GLOBAL_LOCALE));
|
||||
#else
|
||||
#error TODO
|
||||
#endif
|
||||
|
||||
///
|
||||
/// UTF-16 code unit
|
||||
@@ -124,10 +146,10 @@ namespace stdex
|
||||
inline bool iscombining(_In_ char32_t chr)
|
||||
{
|
||||
return
|
||||
0x0300 <= chr && chr < 0x0370 ||
|
||||
0x1dc0 <= chr && chr < 0x1e00 ||
|
||||
0x20d0 <= chr && chr < 0x2100 ||
|
||||
0xfe20 <= chr && chr < 0xfe30;
|
||||
(0x0300 <= chr && chr < 0x0370) ||
|
||||
(0x1dc0 <= chr && chr < 0x1e00) ||
|
||||
(0x20d0 <= chr && chr < 0x2100) ||
|
||||
(0xfe20 <= chr && chr < 0xfe30);
|
||||
}
|
||||
|
||||
///
|
||||
@@ -151,7 +173,7 @@ namespace stdex
|
||||
inline size_t islbreak(_In_reads_or_z_opt_(count) const T* chr, _In_ size_t count)
|
||||
{
|
||||
_Analysis_assume_(chr || !count);
|
||||
if (count >= 2 && (chr[0] == '\r' && chr[1] == '\n' || chr[0] == '\n' && chr[1] == '\r'))
|
||||
if (count >= 2 && ((chr[0] == '\r' && chr[1] == '\n') || (chr[0] == '\n' && chr[1] == '\r')))
|
||||
return 2;
|
||||
if (count > 1 && (chr[0] == '\n' || chr[0] == '\r'))
|
||||
return 1;
|
||||
@@ -811,7 +833,7 @@ namespace stdex
|
||||
goto error;
|
||||
|
||||
if (value < max_ui_pre1 || // Multiplication nor addition will not overflow.
|
||||
value == max_ui_pre1 && digit <= max_ui_pre2) // Small digits will not overflow.
|
||||
(value == max_ui_pre1 && digit <= max_ui_pre2)) // Small digits will not overflow.
|
||||
value = value * (T_bin)radix + digit;
|
||||
else {
|
||||
// Overflow!
|
||||
@@ -1060,7 +1082,7 @@ namespace stdex
|
||||
#pragma warning(suppress: 4996)
|
||||
r = _vsnprintf_l(str, capacity, format, locale, arg);
|
||||
#else
|
||||
r = vsnprintf(str, capacity, format, arg);
|
||||
r = ::vsnprintf(str, capacity, format, arg);
|
||||
#endif
|
||||
if (r == -1 && strnlen(str, capacity) == capacity) {
|
||||
// Buffer overrun. Estimate buffer size for the next iteration.
|
||||
@@ -1206,4 +1228,4 @@ namespace stdex
|
||||
va_end(arg);
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,9 +12,11 @@
|
||||
#include <oaidl.h>
|
||||
#include <tchar.h>
|
||||
#else
|
||||
#define _LARGEFILE64_SOURCE
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include <assert.h>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
@@ -1,14 +1,19 @@
|
||||
/*
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2023 Amebis
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
#include "endian.hpp"
|
||||
#include "math.hpp"
|
||||
#include "system.hpp"
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#ifndef _WIN32
|
||||
#include <iconv.h>
|
||||
#endif
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
@@ -16,28 +21,87 @@ namespace stdex
|
||||
{
|
||||
enum class charset_id : uint16_t {
|
||||
#ifdef _WIN32
|
||||
default = CP_ACP,
|
||||
system = CP_ACP,
|
||||
utf8 = CP_UTF8,
|
||||
utf16 = 1200 /*CP_WINUNICODE*/,
|
||||
#else
|
||||
default = 0,
|
||||
system = 0,
|
||||
utf8,
|
||||
utf16,
|
||||
utf32,
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifndef _WIN32
|
||||
///
|
||||
/// Convert string to Unicode (UTF-16 on Windows) and append to string
|
||||
/// Unicode converter context
|
||||
///
|
||||
template <typename T_from, typename T_to>
|
||||
class iconverter
|
||||
{
|
||||
public:
|
||||
iconverter(_In_ charset_id from, _In_ charset_id to)
|
||||
{
|
||||
m_handle = iconv_open(to_encoding(to), to_encoding(from));
|
||||
if (m_handle == (iconv_t)-1)
|
||||
throw std::runtime_error("iconv_open failed");
|
||||
}
|
||||
|
||||
~iconverter()
|
||||
{
|
||||
iconv_close(m_handle);
|
||||
}
|
||||
|
||||
void convert(_Inout_ std::basic_string<T_to> &dst, _In_reads_or_z_opt_(count) const T_from* src, _In_ size_t count_src) const
|
||||
{
|
||||
T_to buf[0x100];
|
||||
count_src = stdex::strnlen(src, count_src);
|
||||
size_t src_size = stdex::mul(sizeof(T_from), count_src);
|
||||
do {
|
||||
T_to* output = &buf[0];
|
||||
size_t output_size = sizeof(buf);
|
||||
errno = 0;
|
||||
iconv(m_handle, (char**)&src, &src_size, (char**)&output, &output_size);
|
||||
if (errno)
|
||||
throw std::runtime_error("iconv failed");
|
||||
dst.insert(dst.end(), buf, (T_to*)((char*)buf + sizeof(buf) - output_size));
|
||||
} while (src_size);
|
||||
}
|
||||
|
||||
protected:
|
||||
static const char* to_encoding(_In_ charset_id charset)
|
||||
{
|
||||
switch (charset) {
|
||||
case charset_id::system:
|
||||
case charset_id::utf8: return "UTF-8";
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
case charset_id::utf16: return "UTF-16BE";
|
||||
case charset_id::utf32: return "UTF-32BE";
|
||||
#else
|
||||
case charset_id::utf16: return "UTF-16LE";
|
||||
case charset_id::utf32: return "UTF-32LE";
|
||||
#endif
|
||||
default: throw std::invalid_argument("unsupported charset");
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
iconv_t m_handle;
|
||||
};
|
||||
#endif
|
||||
|
||||
///
|
||||
/// Convert string to Unicode (UTF-16 on Windows, UTF-32 elsewhere)) and append to string
|
||||
///
|
||||
/// \param[in,out] dst String to append Unicode to
|
||||
/// \param[in] src String
|
||||
/// \param[in] count_src String character count limit
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
///
|
||||
/// \return Unicode string
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
inline void strcat(
|
||||
_Inout_ std::wstring& dst,
|
||||
_In_reads_or_z_opt_(count_src) const char* src, _In_ size_t count_src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
assert(src || !count_src);
|
||||
#ifdef _WIN32
|
||||
@@ -59,14 +123,15 @@ namespace stdex
|
||||
dst.append(szBuffer.get(), count_src != SIZE_MAX ? wcsnlen(szBuffer.get(), cch) : (size_t)cch - 1);
|
||||
}
|
||||
#else
|
||||
throw std::exception("not implemented");
|
||||
iconverter<char, wchar_t>(charset, charset_id::utf32).convert(dst, src, count_src);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline _Deprecated_("Use stdex::strcat") void str2wstr(
|
||||
_Deprecated_("Use stdex::strcat")
|
||||
inline void str2wstr(
|
||||
_Inout_ std::wstring& dst,
|
||||
_In_reads_or_z_opt_(count_src) const char* src, _In_ size_t count_src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
strcat(dst, src, count_src, charset);
|
||||
}
|
||||
@@ -76,22 +141,21 @@ namespace stdex
|
||||
///
|
||||
/// \param[in,out] dst String to append Unicode to
|
||||
/// \param[in] src String
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
///
|
||||
/// \return Unicode string
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
inline void strcat(
|
||||
_Inout_ std::wstring& dst,
|
||||
_In_ const std::string& src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
strcat(dst, src.data(), src.size(), charset);
|
||||
}
|
||||
|
||||
inline _Deprecated_("Use stdex::strcat") void str2wstr(
|
||||
_Deprecated_("Use stdex::strcat")
|
||||
inline void str2wstr(
|
||||
_Inout_ std::wstring& dst,
|
||||
_In_ const std::string& src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
strcat(dst, src, charset);
|
||||
}
|
||||
@@ -102,14 +166,12 @@ namespace stdex
|
||||
/// \param[in,out] dst String to write Unicode to
|
||||
/// \param[in] src String
|
||||
/// \param[in] count_src String character count limit
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
///
|
||||
/// \return Unicode string
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
inline void strcpy(
|
||||
_Inout_ std::wstring& dst,
|
||||
_In_reads_or_z_opt_(count_src) const char* src, _In_ size_t count_src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
dst.clear();
|
||||
strcat(dst, src, count_src, charset);
|
||||
@@ -120,14 +182,12 @@ namespace stdex
|
||||
///
|
||||
/// \param[in,out] dst String to write Unicode to
|
||||
/// \param[in] src String
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
///
|
||||
/// \return Unicode string
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
inline void strcpy(
|
||||
_Inout_ std::wstring& dst,
|
||||
_In_ const std::string& src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
strcpy(dst, src.data(), src.size(), charset);
|
||||
}
|
||||
@@ -136,13 +196,13 @@ namespace stdex
|
||||
/// Convert string to Unicode string (UTF-16 on Windows)
|
||||
///
|
||||
/// \param[in] src String. Must be zero-terminated.
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
/// \return Unicode string
|
||||
///
|
||||
inline std::wstring str2wstr(
|
||||
_In_z_ const char* src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
std::wstring dst;
|
||||
strcat(dst, src, SIZE_MAX, charset);
|
||||
@@ -154,13 +214,13 @@ namespace stdex
|
||||
///
|
||||
/// \param[in] src String
|
||||
/// \param[in] count_src String character count limit
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
/// \return Unicode string
|
||||
///
|
||||
inline std::wstring str2wstr(
|
||||
_In_reads_or_z_opt_(count_src) const char* src, _In_ size_t count_src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
std::wstring dst;
|
||||
strcat(dst, src, count_src, charset);
|
||||
@@ -171,29 +231,29 @@ namespace stdex
|
||||
/// Convert string to Unicode string (UTF-16 on Windows)
|
||||
///
|
||||
/// \param[in] src String
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
/// \return Unicode string
|
||||
///
|
||||
inline std::wstring str2wstr(
|
||||
_In_ const std::string& src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
return str2wstr(src.c_str(), src.size(), charset);
|
||||
}
|
||||
|
||||
///
|
||||
/// Convert Unicode string (UTF-16 on Windows) to SGML and append to string
|
||||
/// Convert Unicode string (UTF-16 on Windows, UTF-32 elsewhere) to SGML and append to string
|
||||
///
|
||||
/// \param[in,out] dst String to append SGML to
|
||||
/// \param[in] src Unicode string
|
||||
/// \param[in] count_src Unicode string character count limit
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
inline void strcat(
|
||||
_Inout_ std::string& dst,
|
||||
_In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
assert(src || !count_src);
|
||||
#ifdef _WIN32
|
||||
@@ -216,14 +276,15 @@ namespace stdex
|
||||
dst.append(szBuffer.get(), count_src != SIZE_MAX ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
|
||||
}
|
||||
#else
|
||||
throw std::exception("not implemented");
|
||||
iconverter<wchar_t, char>(charset_id::utf32, charset).convert(dst, src, count_src);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline _Deprecated_("Use stdex::strcat") void wstr2str(
|
||||
_Deprecated_("Use stdex::strcat")
|
||||
inline void wstr2str(
|
||||
_Inout_ std::string& dst,
|
||||
_In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
strcat(dst, src, count_src, charset);
|
||||
}
|
||||
@@ -233,20 +294,21 @@ namespace stdex
|
||||
///
|
||||
/// \param[in,out] dst String to append SGML to
|
||||
/// \param[in] src Unicode string
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
inline void strcat(
|
||||
_Inout_ std::string& dst,
|
||||
_In_ const std::wstring& src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
strcat(dst, src.c_str(), src.size(), charset);
|
||||
}
|
||||
|
||||
inline _Deprecated_("Use stdex::strcat") void wstr2str(
|
||||
_Deprecated_("Use stdex::strcat")
|
||||
inline void wstr2str(
|
||||
_Inout_ std::string& dst,
|
||||
_In_ const std::wstring& src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
strcat(dst, src, charset);
|
||||
}
|
||||
@@ -257,12 +319,12 @@ namespace stdex
|
||||
/// \param[in,out] dst String to write SGML to
|
||||
/// \param[in] src Unicode string
|
||||
/// \param[in] count_src Unicode string character count limit
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
inline void strcpy(
|
||||
_Inout_ std::string& dst,
|
||||
_In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
dst.clear();
|
||||
strcat(dst, src, count_src, charset);
|
||||
@@ -273,12 +335,12 @@ namespace stdex
|
||||
///
|
||||
/// \param[in,out] dst String to write SGML to
|
||||
/// \param[in] src Unicode string
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
inline void strcpy(
|
||||
_Inout_ std::string& dst,
|
||||
_In_ const std::wstring& src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
strcpy(dst, src.data(), src.size(), charset);
|
||||
}
|
||||
@@ -287,13 +349,13 @@ namespace stdex
|
||||
/// Convert Unicode string (UTF-16 on Windows) to string
|
||||
///
|
||||
/// \param[in] src Unicode string. Must be zero-terminated.
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
/// \return String
|
||||
///
|
||||
inline std::string wstr2str(
|
||||
_In_z_ const wchar_t* src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
std::string dst;
|
||||
strcat(dst, src, SIZE_MAX, charset);
|
||||
@@ -305,13 +367,13 @@ namespace stdex
|
||||
///
|
||||
/// \param[in] src Unicode string
|
||||
/// \param[in] count_src Unicode string character count limit
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
/// \return String
|
||||
///
|
||||
inline std::string wstr2str(
|
||||
_In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
std::string dst;
|
||||
strcat(dst, src, count_src, charset);
|
||||
@@ -322,14 +384,14 @@ namespace stdex
|
||||
/// Convert Unicode string (UTF-16 on Windows) to string
|
||||
///
|
||||
/// \param[in] src Unicode string
|
||||
/// \param[in] charset Charset (stdex::charset_id::default - system default)
|
||||
/// \param[in] charset Charset (stdex::charset_id::system - system default)
|
||||
///
|
||||
/// \return String
|
||||
///
|
||||
inline std::string wstr2str(
|
||||
_In_ const std::wstring& src,
|
||||
_In_ charset_id charset = charset_id::default)
|
||||
_In_ charset_id charset = charset_id::system)
|
||||
{
|
||||
return wstr2str(src.c_str(), src.size(), charset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sal.hpp"
|
||||
#include "compat.hpp"
|
||||
|
||||
namespace stdex
|
||||
{
|
||||
|
Reference in New Issue
Block a user