From 68f9f6a881d0fcb3751cf7e347294992bb02da5d Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 25 Feb 2024 10:50:52 -0700 Subject: [PATCH] Use CMake to test for strncasecmp --- CMakeLists.txt | 3 +++ common/realdos.cpp | 1 + headers/port_config.h.in | 2 ++ headers/string_case_compare.h | 22 ++++++++++++++++++++++ win32/os_win32.cpp | 13 ------------- 5 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 headers/string_case_compare.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 28ac85e11..f3db51209 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ project("${ID_PROJECT_NAME}" HOMEPAGE_URL "https://LegalizeAdulthood.github.io/iterated-dynamics" LANGUAGES CXX) +include(CheckSymbolExists) include(CTest) include(TestBigEndian) @@ -62,10 +63,12 @@ add_subdirectory(win32) add_subdirectory(hc) test_big_endian(ID_BIG_ENDIAN) +check_symbol_exists(strncasecmp "strings.h" ID_HAVE_STRNCASECMP) configure_file(headers/port_config.h.in include/port_config.h) add_library(config INTERFACE) target_sources(config INTERFACE headers/port_config.h.in + headers/string_case_compare.h "${CMAKE_CURRENT_BINARY_DIR}/include/port_config.h" ) target_include_directories(config INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/include") diff --git a/common/realdos.cpp b/common/realdos.cpp index de3262c05..35eef39a1 100644 --- a/common/realdos.cpp +++ b/common/realdos.cpp @@ -17,6 +17,7 @@ #include "prompts2.h" #include "realdos.h" #include "rotate.h" +#include "string_case_compare.h" #include "zoom.h" #include diff --git a/headers/port_config.h.in b/headers/port_config.h.in index a4eec0089..e31871695 100644 --- a/headers/port_config.h.in +++ b/headers/port_config.h.in @@ -1,3 +1,5 @@ #pragma once #cmakedefine01 ID_BIG_ENDIAN + +#cmakedefine ID_HAVE_STRNCASECMP diff --git a/headers/string_case_compare.h b/headers/string_case_compare.h new file mode 100644 index 000000000..b4791b1eb --- /dev/null +++ b/headers/string_case_compare.h @@ -0,0 +1,22 @@ +#pragma once + +#ifdef ID_HAVE_STRNCASECMP +#include + +#else +#include + +// case independent version of std::strncmp +inline int strncasecmp(char const *s, char const *t, int ct) +{ + for (; (std::tolower(*s) == std::tolower(*t)) && --ct ; s++, t++) + { + if (*s == '\0') + { + return 0; + } + } + return std::tolower(*s) - std::tolower(*t); +} + +#endif diff --git a/win32/os_win32.cpp b/win32/os_win32.cpp index 0d295adc2..3940b4034 100644 --- a/win32/os_win32.cpp +++ b/win32/os_win32.cpp @@ -816,16 +816,3 @@ void init_failure(char const *message) { MessageBox(nullptr, message, "FractInt: Fatal Error", MB_OK); } - -// case independent version of std::strncmp -int strncasecmp(char const *s, char const *t, int ct) -{ - for (; (std::tolower(*s) == std::tolower(*t)) && --ct ; s++, t++) - { - if (*s == '\0') - { - return 0; - } - } - return std::tolower(*s) - std::tolower(*t); -}