diff --git a/src/lib/coil/common/coil/Properties.cpp b/src/lib/coil/common/coil/Properties.cpp index d1f6c8485..c8e8d27ee 100644 --- a/src/lib/coil/common/coil/Properties.cpp +++ b/src/lib/coil/common/coil/Properties.cpp @@ -18,7 +18,6 @@ */ #include -#include #include #include @@ -73,11 +72,13 @@ namespace coil * @brief Constructor(Give the default value with char*[]) * @endif */ + /* Properties::Properties(const char* const defaults[], size_t num) { leaf.clear(); setDefaults(defaults, num); } + */ /*! * @if jp @@ -306,14 +307,16 @@ namespace coil * @brief Set a default value together in the property list * @endif */ + /* void Properties::setDefaults(const char* const defaults[], size_t num) { - for (size_t i = 0; i < num && defaults[i][0] != '\0' ; i += 2) + for (size_t i = 0; i+1 < num && defaults[i] && defaults[i][0] != '\0' ; i += 2) { setDefault(eraseBothEndsBlank(defaults[i]), eraseBothEndsBlank(defaults[i + 1])); } } + */ //============================================================ // load and save functions diff --git a/src/lib/coil/common/coil/Properties.h b/src/lib/coil/common/coil/Properties.h index 26633f19b..2836f01aa 100644 --- a/src/lib/coil/common/coil/Properties.h +++ b/src/lib/coil/common/coil/Properties.h @@ -20,6 +20,8 @@ #ifndef COIL_PROPERTIES_H #define COIL_PROPERTIES_H +#include + #include #include #include @@ -205,8 +207,14 @@ namespace coil * * @endif */ - explicit Properties(const char* const defaults[], - size_t num = std::numeric_limits::max()); + //explicit Properties(const char* const defaults[], + // size_t num = std::numeric_limits::max()); + template + explicit Properties(const char* const (&defaults)[N], size_t num = N) + { + leaf.clear(); + setDefaults(defaults, num); + } /*! * @if jp @@ -594,8 +602,17 @@ namespace coil * * @endif */ - void setDefaults(const char* const defaults[], - size_t num = std::numeric_limits::max()); + //void setDefaults(const char* const defaults[], + // size_t num = std::numeric_limits::max()); + template + void setDefaults(const char* const (&defaults)[N], size_t num = N) + { + for (size_t i = 0; i + 1 < N && i + 1 < num && defaults[i][0] != '\0'; i += 2) + { + setDefault(eraseBothEndsBlank(defaults[i]), + eraseBothEndsBlank(defaults[i + 1])); + } + } //============================================================ // load and save functions diff --git a/src/lib/coil/common/coil/crc.cpp b/src/lib/coil/common/coil/crc.cpp index 55096422e..cbf8f0d49 100644 --- a/src/lib/coil/common/coil/crc.cpp +++ b/src/lib/coil/common/coil/crc.cpp @@ -16,6 +16,8 @@ */ #include +#include +#include namespace coil { @@ -46,8 +48,8 @@ namespace coil */ unsigned short crc16(const char* str, size_t len) { - static const unsigned short crc16tab[256] = - { + static constexpr std::array crc16tab{ { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, @@ -83,11 +85,20 @@ namespace coil 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0, - }; + } }; unsigned short crc16(0xffff); + const unsigned char* p = reinterpret_cast(str); for (size_t i(0); i < len; ++i) { - crc16 = crc16tab[(crc16 >> 8) ^ str[i]] ^ (crc16 << 8); +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-warning-option" +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif + crc16 = crc16tab[(crc16 >> 8) ^ p[i]] ^ (crc16 << 8); +#ifdef __clang__ +#pragma clang diagnostic pop +#endif } return crc16 ^ 0x0000; } @@ -135,8 +146,8 @@ namespace coil * crc32table[n] = c; * } */ - static const unsigned int crc32tab[256] = - { + + static constexpr std::array crc32tab{ { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, @@ -180,11 +191,20 @@ namespace coil 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d, - }; + }}; unsigned long c(0xffffffffL); + const unsigned char* p = reinterpret_cast(str); for (size_t i(0); i < len; ++i) { - c = crc32tab[(0xff ^ str[i]) & 0xff] ^ (c >> 8); +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-warning-option" +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif + c = crc32tab[(0xff ^ p[i]) & 0xff] ^ (c >> 8); +#ifdef __clang__ +#pragma clang diagnostic pop +#endif } return c ^ 0xffffffffL; } diff --git a/src/lib/coil/common/coil/stringutil.cpp b/src/lib/coil/common/coil/stringutil.cpp index 24ad30b2a..8ae188ccf 100644 --- a/src/lib/coil/common/coil/stringutil.cpp +++ b/src/lib/coil/common/coil/stringutil.cpp @@ -50,10 +50,9 @@ namespace coil { #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) int buff_size = MultiByteToWideChar(CP_UTF7, 0, str.c_str(), -1, (wchar_t*)nullptr, 0); - wchar_t* ret = new wchar_t[buff_size]; - MultiByteToWideChar(CP_UTF7, 0, str.c_str(), -1, ret, buff_size); - std::wstring wstr(ret, ret + buff_size - 1); - delete[] ret; + std::wstring wstr(buff_size, 0); + + MultiByteToWideChar(CP_UTF7, 0, str.c_str(), -1, &wstr[0], buff_size); #else std::wstring wstr(str.length(), L' '); std::copy(str.begin(), str.end(), wstr.begin()); @@ -72,10 +71,8 @@ namespace coil { #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) int buff_size = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, (char*)nullptr, 0, nullptr, nullptr); - CHAR* ret = new CHAR[buff_size]; - WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, ret, buff_size, nullptr, nullptr); - std::string str(ret, ret + buff_size - 1); - delete[] ret; + std::string str(buff_size, 0); + WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], buff_size, nullptr, nullptr); #else std::string str(wstr.length(), ' '); std::copy(wstr.begin(), wstr.end(), str.begin()); @@ -853,7 +850,13 @@ namespace coil m_args.reserve(args.size()); for (auto&& arg : args) { - m_args.emplace_back(arg.c_str(), arg.c_str() + arg.size() + 1); + std::vector buf; + buf.reserve(arg.size() + 1); + + buf.insert(buf.end(), arg.begin(), arg.end()); + buf.push_back('\0'); + + m_args.emplace_back(std::move(buf)); } // make m_argv. m_argv.reserve(m_args.size() + 1); diff --git a/src/lib/coil/win32/coil/Routing.cpp b/src/lib/coil/win32/coil/Routing.cpp index 881b3a353..69da46e90 100644 --- a/src/lib/coil/win32/coil/Routing.cpp +++ b/src/lib/coil/win32/coil/Routing.cpp @@ -147,10 +147,18 @@ namespace coil for (int i(0); i < static_cast(ipaddr_table->dwNumEntries); ++i) { +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-warning-option" +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif if (bestifindex == ipaddr_table->table[i].dwIndex) { IN_ADDR inipaddr; inipaddr.S_un.S_addr = static_cast(ipaddr_table->table[i].dwAddr); +#ifdef __clang__ +#pragma clang diagnostic pop +#endif char str_buffer[INET_ADDRSTRLEN] = { 0 }; if (inet_ntop(AF_INET, &inipaddr.S_un, str_buffer, sizeof(str_buffer)) != nullptr) { diff --git a/src/lib/hrtm/properties.cpp b/src/lib/hrtm/properties.cpp index e14d3ff84..7f001a753 100644 --- a/src/lib/hrtm/properties.cpp +++ b/src/lib/hrtm/properties.cpp @@ -25,10 +25,12 @@ namespace utils : coil::Properties("", "") { } + /* Properties::Properties(const char* const defaults[], long num) : coil::Properties(defaults, num) { } + */ Properties::~Properties() = default; } // namespace utils } // namespace hrtm diff --git a/src/lib/hrtm/properties.h b/src/lib/hrtm/properties.h index 4b69740c7..15ce29a28 100644 --- a/src/lib/hrtm/properties.h +++ b/src/lib/hrtm/properties.h @@ -29,8 +29,13 @@ namespace utils { public: explicit Properties(); - explicit Properties(const char* const defaults[], - long num = std::numeric_limits::max()); + //explicit Properties(const char* const defaults[], + // long num = std::numeric_limits::max()); + template + explicit Properties(const char* const (&defaults)[N], size_t num = N) + : coil::Properties(defaults, num) + { + } ~Properties() override; }; } // namespace utils