From 4291175a7ceff48d54ca142d40f4b3e14db9ad99 Mon Sep 17 00:00:00 2001 From: "Robin E. R. Davies" Date: Mon, 25 Nov 2024 23:11:46 -0500 Subject: [PATCH] Covert ICU API usage from C++ to C linkages. --- src/Locale.cpp | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/Locale.cpp b/src/Locale.cpp index 6caec02..a3f353f 100644 --- a/src/Locale.cpp +++ b/src/Locale.cpp @@ -18,25 +18,22 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include "pch.h" - #include "Locale.hpp" + +#define U_SHOW_CPLUSPLUS_API 0 + #include #include "Lv2Log.hpp" #include #include #include -#include #include #include "ss.hpp" #include -// Must be UNICODE. Should reflect system locale. (.e.g en-US.UTF8, de-de.UTF8) - -// This is correct for libstdc++; most probably not correct for Windows or CLANG. :-( using namespace pipedal; - std::string getCurrentLocale() { std::string locale = setlocale(LC_ALL, nullptr); if (locale.empty() || locale == "C") { @@ -69,33 +66,34 @@ std::string getCurrentLocale() { } Collator::~Collator() { - } class LocaleImpl; class CollatorImpl : public Collator { public: - CollatorImpl(std::shared_ptr LocaleImpl,icu::Locale &locale); + CollatorImpl(std::shared_ptr localeImpl, const char* localeStr); ~CollatorImpl(); virtual int Compare(const std::string &left, const std::string&right); private: - icu::Collator* collator = nullptr; + UCollator* collator = nullptr; std::shared_ptr localeImpl; }; CollatorImpl::~CollatorImpl() { - delete collator; + if (collator) { + ucol_close(collator); + } localeImpl = nullptr; } -CollatorImpl::CollatorImpl(std::shared_ptr localeImpl, icu::Locale &locale) -:localeImpl(localeImpl) + +CollatorImpl::CollatorImpl(std::shared_ptr localeImpl, const char* localeStr) +: localeImpl(localeImpl) { UErrorCode status = U_ZERO_ERROR; - - this->collator = icu::Collator::createInstance(locale, status); + collator = ucol_open(localeStr, &status); if (U_FAILURE(status)) { throw std::runtime_error(SS("Failed to create collator: " << u_errorName(status))); @@ -103,8 +101,12 @@ CollatorImpl::CollatorImpl(std::shared_ptr localeImpl, icu::Locale & } int CollatorImpl::Compare(const std::string &left, const std::string&right) { - return collator->compare(left.c_str(),right.c_str()); + UErrorCode status = U_ZERO_ERROR; + return ucol_strcoll(collator, + reinterpret_cast(left.c_str()), left.length(), + reinterpret_cast(right.c_str()), right.length()); } + Locale::ptr g_instance; class LocaleImpl: public Locale, public std::enable_shared_from_this { @@ -113,32 +115,30 @@ public: virtual const std::string &CurrentLocale() const ; virtual Collator::ptr GetCollator(); private: - std::unique_ptr locale; std::string currentLocale; }; LocaleImpl::LocaleImpl() { currentLocale = getCurrentLocale(); - locale = std::make_unique(currentLocale.c_str()); } + const std::string &LocaleImpl::CurrentLocale() const { return currentLocale; } - Collator::ptr LocaleImpl::GetCollator(){ auto pThis = shared_from_this(); - return std::shared_ptr(new CollatorImpl(pThis,*locale)); + return std::shared_ptr(new CollatorImpl(pThis, currentLocale.c_str())); } static std::mutex createMutex; Locale::~Locale() { - } + Locale::ptr Locale::g_instance; Locale::ptr Locale::GetInstance() @@ -151,6 +151,4 @@ Locale::ptr Locale::GetInstance() } g_instance = std::make_shared(); return g_instance; -} - - +} \ No newline at end of file