]> SALOME platform Git repositories - modules/shaper.git/blob - src/Locale/Locale_Convert.cpp
Salome HOME
Task #3237: Allow usage of accented characters in ObjectBrowser
[modules/shaper.git] / src / Locale / Locale_Convert.cpp
1 // Copyright (C) 2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Locale_Convert.h>
21
22 #include <boost/locale/encoding_utf.hpp>
23
24 ////// To support old types of GCC (less than 5.0), check the wide-string conversion is working
25 ////#if (__cplusplus >= 201103L || _MSVC_LANG >= 201103L)  && \
26 ////    (__cplusplus >= 201402L || !defined(__GLIBCXX__)   || \
27 ////    (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE > 4))
28 ////#define HAVE_WORKING_WIDESTRING 1
29 ////#else
30 ////#define HAVE_WORKING_WIDESTRING 0
31 ////#endif
32 ////
33 ////#if HAVE_WORKING_WIDESTRING
34 ////#include <codecvt>
35 ////#endif
36
37 namespace Locale
38 {
39   std::string Convert::toString(const std::wstring& theWStr)
40   {
41 ////#if HAVE_WORKING_WIDESTRING
42 ////    static std::wstring_convert<std::codecvt_utf8<wchar_t> > aConvertor;
43 ////    return aConvertor.to_bytes(theWStr);
44 ////#else
45 ////    char* aBuf = new char[2 * (theWStr.size() + 1)];
46 ////    size_t aNbChars = std::wcstombs(aBuf, theWStr.c_str(), theWStr.size());
47 ////    if (aNbChars != (size_t)-1)
48 ////      aBuf[aNbChars] = '\0';
49 ////    std::string aStr(aBuf);
50 ////    delete[] aBuf;
51 ////    return aStr;
52 ////#endif
53     return boost::locale::conv::utf_to_utf<char>(theWStr);
54   }
55
56   std::string Convert::toString(const char16_t* theExtStr)
57   {
58     return boost::locale::conv::utf_to_utf<char>(theExtStr);
59   }
60
61   std::wstring Convert::toWString(const std::string& theStr)
62   {
63 ////#if HAVE_WORKING_WIDESTRING
64 ////    static std::wstring_convert<std::codecvt_utf8<wchar_t> > aConvertor;
65 ////    return aConvertor.from_bytes(theStr);
66 ////#else
67 ////    wchar_t* aBuf = new wchar_t[theStr.size() + 1];
68 ////    size_t aNbWChars = std::mbstowcs(aBuf, theStr.c_str(), theStr.size());
69 ////    if (aNbWChars != (size_t)-1)
70 ////      aBuf[aNbWChars] = L'\0';
71 ////    std::wstring aWStr(aBuf);
72 ////    delete[] aBuf;
73 ////    return aWStr;
74 ////#endif
75     return boost::locale::conv::utf_to_utf<wchar_t>(theStr);
76   }
77
78   std::wstring Convert::toWString(const char16_t* theExtStr)
79   {
80     return boost::locale::conv::utf_to_utf<wchar_t>(theExtStr);
81   }
82
83 }