X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FBasics%2FBasics_Utils.cxx;h=1d8fd16e0f7f6091deb400f0e953f76d1539deeb;hb=7b6895b48ccd982f69db4fe3ecd30d75be0514dc;hp=77eaacc1962a41feb31412974193eead9eccebee;hpb=bbac39ee34bd6a5e6fd051024209399b97b818b1;p=modules%2Fkernel.git diff --git a/src/Basics/Basics_Utils.cxx b/src/Basics/Basics_Utils.cxx index 77eaacc19..1d8fd16e0 100644 --- a/src/Basics/Basics_Utils.cxx +++ b/src/Basics/Basics_Utils.cxx @@ -1,9 +1,9 @@ -// Copyright (C) 2007-2012 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 // License as published by the Free Software Foundation; either -// version 2.1 of the License. +// version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,7 +18,7 @@ // // File : Basics_Utils.cxx -// Autor : Alexander A. BORODIN +// Author : Alexander A. BORODIN // Module : SALOME // @@ -35,17 +35,18 @@ namespace Kernel_Utils { + // threadsafe std::string GetHostname() { int ls = 100, r = 1; char *s; - + while (ls < 10000 && r) { ls *= 2; s = new char[ls]; - r = gethostname(s, ls-1); - switch (r) + r = gethostname(s, ls-1);//threadsafe see man 7 pthread + switch (r) { case 0: break; @@ -57,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 ) @@ -105,6 +117,36 @@ namespace Kernel_Utils return guid; } + const wchar_t* decode(const char* encoded) + { + Localizer loc(LC_CTYPE, ""); + size_t length = strlen(encoded) + sizeof(char); + wchar_t* decoded = new wchar_t[length]; + memset( decoded, '\0', length); + mbstowcs(decoded, encoded, length); + return decoded; + } + + const wchar_t* decode_s(std::string encoded) + { + return decode(encoded.c_str()); + } + + const char* encode(const wchar_t* decoded) + { + Localizer loc(LC_CTYPE, ""); + size_t length = std::wcslen(decoded) + sizeof(wchar_t); + char* encoded = new char[length]; + memset( encoded, '\0', length); + wcstombs(encoded, decoded, length); + return encoded; + } + + std::string encode_s(const wchar_t* decoded) + { + return std::string(encode(decoded)); + } + #ifndef WIN32 void print_traceback() { @@ -123,6 +165,54 @@ namespace Kernel_Utils free (strings); } +#else + #if (_MSC_VER >= 1400) // Visual Studio 2005 + #include + int setenv(const char *name, const char *value, int rewrite) + { + std::stringstream sstr; + sstr<