X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FBasics%2FBasics_Utils.cxx;h=1d8fd16e0f7f6091deb400f0e953f76d1539deeb;hb=f6120c1bdb84ed1cc827ffab8284c0ac2709189f;hp=0567251474c2a686cf387a0d9b72a57e394890d5;hpb=34bdd08c1cbba32e415e32489d56f91cf89e76da;p=modules%2Fkernel.git diff --git a/src/Basics/Basics_Utils.cxx b/src/Basics/Basics_Utils.cxx index 056725147..1d8fd16e0 100644 --- a/src/Basics/Basics_Utils.cxx +++ b/src/Basics/Basics_Utils.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -40,13 +40,13 @@ namespace Kernel_Utils { int ls = 100, r = 1; char *s; - + while (ls < 10000 && r) { ls *= 2; s = new char[ls]; r = gethostname(s, ls-1);//threadsafe see man 7 pthread - switch (r) + switch (r) { case 0: break; @@ -58,38 +58,49 @@ namespace Kernel_Utils case ENAMETOOLONG: #endif #ifdef WIN32 - case WSAEFAULT: + case WSAEFAULT: #endif delete [] s; continue; } - + } - + if (r != 0) { s = new char[50]; strcpy(s, "localhost"); } - + // remove all after '.' char *aDot = (strchr(s,'.')); if (aDot) aDot[0] = '\0'; - + std::string p = s; delete [] s; return p; } - + Localizer::Localizer() { - myCurLocale = setlocale(LC_NUMERIC, 0); - setlocale(LC_NUMERIC, "C"); + init(LC_NUMERIC, "C"); + } + + Localizer::Localizer(int category, const char* locale) + { + init(category, locale); + } + + void Localizer::init(int category, const char* locale) + { + myCategory = category; + myOriginalLocale = setlocale(category, NULL); + setlocale(category, locale); } Localizer::~Localizer() { - setlocale(LC_NUMERIC, myCurLocale.c_str()); + setlocale(myCategory, myOriginalLocale.c_str()); } std::string GetGUID( GUIDtype type ) @@ -108,7 +119,7 @@ namespace Kernel_Utils const wchar_t* decode(const char* encoded) { - setlocale(LC_ALL, ""); + Localizer loc(LC_CTYPE, ""); size_t length = strlen(encoded) + sizeof(char); wchar_t* decoded = new wchar_t[length]; memset( decoded, '\0', length); @@ -123,7 +134,7 @@ namespace Kernel_Utils const char* encode(const wchar_t* decoded) { - setlocale(LC_ALL, ""); + Localizer loc(LC_CTYPE, ""); size_t length = std::wcslen(decoded) + sizeof(wchar_t); char* encoded = new char[length]; memset( encoded, '\0', length); @@ -168,4 +179,40 @@ namespace Kernel_Utils #endif #endif +#if defined(WIN32) + // Convert a wide Unicode string to an UTF8 string + char* utf8_encode(const wchar_t* encoded) + { + if (encoded == NULL) return NULL; + auto size_needed = WideCharToMultiByte(CP_UTF8, 0, encoded, (int)std::wcslen(encoded), NULL, 0, NULL, NULL); + char* strTo = new char[ size_needed + 1 ]; + WideCharToMultiByte(CP_UTF8, 0, encoded, (int)std::wcslen(encoded), strTo, size_needed, NULL, NULL); + strTo[size_needed] = '\0'; + return strTo; + } + // Convert an UTF8 string to a wide Unicode String + wchar_t* utf8_decode(const char* decoded) + { + if (decoded == NULL) return NULL; + auto size_needed = MultiByteToWideChar(CP_UTF8, 0, decoded, (int)strlen(decoded), NULL, 0); + wchar_t* wstrTo = new wchar_t[ size_needed + 1 ]; + MultiByteToWideChar(CP_UTF8, 0, decoded, (int)strlen(decoded), wstrTo, size_needed); + wstrTo[size_needed] = '\0'; + return wstrTo; + } + // Convert a wide Unicode std::wstring to an UTF8 string + std::string utf8_encode_s(const std::wstring& encoded) { + char* anEncoded = utf8_encode(encoded.c_str()); + std::string res = std::string(anEncoded); + delete [] anEncoded; + return res; + } + // Convert an UTF8 std::string to a wide Unicode std::wstring + std::wstring utf8_decode_s(const std::string& decoded) { + wchar_t* aDecoded = utf8_decode(decoded.c_str()); + std::wstring res = std::wstring(aDecoded); + delete [] aDecoded; + return res; + } +#endif }