1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : Basics_Utils.cxx
21 // Author : Alexander A. BORODIN
25 #include "Basics_Utils.hxx"
36 namespace Kernel_Utils
39 std::string GetHostname()
44 while (ls < 10000 && r)
48 r = gethostname(s, ls-1);//threadsafe see man 7 pthread
72 strcpy(s, "localhost");
75 // remove all after '.'
76 char *aDot = (strchr(s,'.'));
77 if (aDot) aDot[0] = '\0';
84 Localizer::Localizer()
86 myCurLocale = setlocale(LC_NUMERIC, 0);
87 setlocale(LC_NUMERIC, "C");
90 Localizer::~Localizer()
92 setlocale(LC_NUMERIC, myCurLocale.c_str());
95 std::string GetGUID( GUIDtype type )
101 guid = "FFFFFFFF-D9CD-11d6-945D-1050DA506788"; break;
103 guid = "C08F3C95-F112-4023-8776-78F1427D0B6D"; break;
109 const wchar_t* decode(const char* encoded)
111 setlocale(LC_ALL, "");
112 size_t length = strlen(encoded) + sizeof(char);
113 wchar_t* decoded = new wchar_t[length];
114 memset( decoded, '\0', length);
115 mbstowcs(decoded, encoded, length);
119 const wchar_t* decode_s(std::string encoded)
121 return decode(encoded.c_str());
124 const char* encode(const wchar_t* decoded)
126 setlocale(LC_ALL, "");
127 size_t length = std::wcslen(decoded) + sizeof(wchar_t);
128 char* encoded = new char[length];
129 memset( encoded, '\0', length);
130 wcstombs(encoded, decoded, length);
134 std::string encode_s(const wchar_t* decoded)
136 return std::string(encode(decoded));
140 void print_traceback()
147 size = backtrace (array, 40);
148 strings = backtrace_symbols (array, size);
150 for (i = 0; i < size; i++)
152 std::cerr << strings[i] << std::endl;
158 #if (_MSC_VER >= 1400) // Visual Studio 2005
160 int setenv(const char *name, const char *value, int rewrite)
162 std::stringstream sstr;
163 sstr<<name<<'='<<value;
164 if(rewrite || std::string(getenv(name)).length() == 0)
165 return _putenv(sstr.str().c_str());
172 // Convert a wide Unicode string to an UTF8 string
173 char* utf8_encode(const wchar_t* encoded)
175 if (encoded == NULL) return NULL;
176 int size_needed = WideCharToMultiByte(CP_UTF8, 0, encoded, std::wcslen(encoded), NULL, 0, NULL, NULL);
177 char* strTo = new char[ size_needed + 1 ];
178 WideCharToMultiByte(CP_UTF8, 0, encoded, std::wcslen(encoded), strTo, size_needed, NULL, NULL);
179 strTo[size_needed] = '\0';
182 // Convert an UTF8 string to a wide Unicode String
183 wchar_t* utf8_decode(const char* decoded)
185 if (decoded == NULL) return NULL;
186 int size_needed = MultiByteToWideChar(CP_UTF8, 0, decoded, strlen(decoded), NULL, 0);
187 wchar_t* wstrTo = new wchar_t[ size_needed + 1 ];
188 MultiByteToWideChar(CP_UTF8, 0, decoded, strlen(decoded), wstrTo, size_needed);
189 wstrTo[size_needed] = '\0';
192 // Convert a wide Unicode std::wstring to an UTF8 string
193 std::string utf8_encode_s(const std::wstring& encoded) {
194 char* anEncoded = utf8_encode(encoded.c_str());
195 std::string res = std::string(anEncoded);
199 // Convert an UTF8 std::string to a wide Unicode std::wstring
200 std::wstring utf8_decode_s(const std::string& decoded) {
201 wchar_t* aDecoded = utf8_decode(decoded.c_str());
202 std::wstring res = std::wstring(aDecoded);