From 8462c9cfaf61b7d4318b6f7655a2858b9fabdf63 Mon Sep 17 00:00:00 2001 From: Artem Zhidkov Date: Sun, 28 Jun 2020 15:51:58 +0300 Subject: [PATCH] Task #3237: Allow usage of accented characters in ObjectBrowser Fix compilation on Linux with -std=c++0x --- src/Events/Events_InfoMessage.cpp | 21 ++++++++++++++++++++ src/ModelAPI/ModelAPI_Tools.cpp | 32 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/Events/Events_InfoMessage.cpp b/src/Events/Events_InfoMessage.cpp index 50f41d121..ba26c145f 100644 --- a/src/Events/Events_InfoMessage.cpp +++ b/src/Events/Events_InfoMessage.cpp @@ -19,7 +19,19 @@ #include "Events_InfoMessage.h" #include + +// To support old types of GCC (less than 5.0), check the wide-string conversion is working +#if (__cplusplus >= 201103L || _MSVC_LANG >= 201103L) && \ + (__cplusplus >= 201402L || !defined(__GLIBCXX__) || \ + (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE > 4)) +#define HAVE_WORKING_WIDESTRING 1 +#else +#define HAVE_WORKING_WIDESTRING 0 +#endif + +#if HAVE_WORKING_WIDESTRING #include +#endif void Events_InfoMessage::addParameter(double theParam) { @@ -43,7 +55,16 @@ void Events_InfoMessage::send() Events_InfoMessage& Events_InfoMessage::arg(const std::wstring& theParam) { +#if HAVE_WORKING_WIDESTRING static std::wstring_convert > aConvertor; addParameter(aConvertor.to_bytes(theParam)); +#else + char* aBuf = new char[2 * (theParam.size() + 1)]; + size_t aNbChars = std::wcstombs(aBuf, theParam.c_str(), theParam.size()); + if (aNbChars != (size_t)-1) + aBuf[aNbChars] = '\0'; + addParameter(aBuf); + delete[] aBuf; +#endif return *this; } diff --git a/src/ModelAPI/ModelAPI_Tools.cpp b/src/ModelAPI/ModelAPI_Tools.cpp index 7cbdd8089..e0da11a2f 100644 --- a/src/ModelAPI/ModelAPI_Tools.cpp +++ b/src/ModelAPI/ModelAPI_Tools.cpp @@ -37,7 +37,19 @@ #include #include #include + +// To support old types of GCC (less than 5.0), check the wide-string conversion is working +#if (__cplusplus >= 201103L || _MSVC_LANG >= 201103L) && \ + (__cplusplus >= 201402L || !defined(__GLIBCXX__) || \ + (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE > 4)) +#define HAVE_WORKING_WIDESTRING 1 +#else +#define HAVE_WORKING_WIDESTRING 0 +#endif + +#if HAVE_WORKING_WIDESTRING #include +#endif #include #include @@ -1107,8 +1119,18 @@ std::list referencedFeatures( // LCOV_EXCL_STOP std::string toString(const std::wstring& theWStr) { +#if HAVE_WORKING_WIDESTRING static std::wstring_convert > aConvertor; return aConvertor.to_bytes(theWStr); +#else + char* aBuf = new char[2 * (theWStr.size() + 1)]; + size_t aNbChars = std::wcstombs(aBuf, theWStr.c_str(), theWStr.size()); + if (aNbChars != (size_t)-1) + aBuf[aNbChars] = '\0'; + std::string aStr(aBuf); + delete[] aBuf; + return aStr; +#endif } /*! Converts a byte string to an extended string @@ -1116,8 +1138,18 @@ std::string toString(const std::wstring& theWStr) */ std::wstring toWString(const std::string& theStr) { +#if HAVE_WORKING_WIDESTRING static std::wstring_convert > aConvertor; return aConvertor.from_bytes(theStr); +#else + wchar_t* aBuf = new wchar_t[theStr.size()]; + size_t aNbWChars = std::mbstowcs(aBuf, theStr.c_str(), theStr.size()); + if (aNbWChars != (size_t)-1) + aBuf[aNbWChars] = '\0'; + std::wstring aWStr(aBuf); + delete[] aBuf; + return aWStr; +#endif } -- 2.39.2