X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FBasics%2FBasics_Utils.cxx;h=2f0338f97274c59e673621d32a6b70a1beb1bbc8;hb=d1070d09cfbba6d9b72d8f2ca8d07d23f296686c;hp=0567251474c2a686cf387a0d9b72a57e394890d5;hpb=85eb31dcb591c2535c0b0d68c3525f7423888954;p=modules%2Fkernel.git diff --git a/src/Basics/Basics_Utils.cxx b/src/Basics/Basics_Utils.cxx index 056725147..2f0338f97 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-2024 CEA, EDF, OPEN CASCADE // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -32,6 +32,8 @@ #include #endif +#include +#include namespace Kernel_Utils { @@ -40,13 +42,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 +60,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 +121,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 +136,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); @@ -133,7 +146,8 @@ namespace Kernel_Utils std::string encode_s(const wchar_t* decoded) { - return std::string(encode(decoded)); + std::unique_ptr> tmp((char *)encode(decoded),[](char *ptr) { delete [] ptr; }); + return std::string(tmp.get()); } #ifndef WIN32 @@ -161,11 +175,47 @@ namespace Kernel_Utils { std::stringstream sstr; sstr<