]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/ModelAPI/ModelAPI_Tools.cpp
Salome HOME
Task #3237: Allow usage of accented characters in ObjectBrowser
[modules/shaper.git] / src / ModelAPI / ModelAPI_Tools.cpp
index 7cbdd8089f946ffe562ebe265f62dfb09fb93fe3..e0da11a2f9490ae9ba3e5bb452a64955ff599bd2 100644 (file)
 #include <map>
 #include <iostream>
 #include <sstream>
+
+// 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 <codecvt>
+#endif
 
 #include <Events_Loop.h>
 #include <ModelAPI_Events.h>
@@ -1107,8 +1119,18 @@ std::list<FeaturePtr> referencedFeatures(
 // LCOV_EXCL_STOP
 std::string toString(const std::wstring& theWStr)
 {
+#if HAVE_WORKING_WIDESTRING
   static std::wstring_convert<std::codecvt_utf8<wchar_t> > 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<std::codecvt_utf8<wchar_t> > 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
 }