Salome HOME
Merge branch 'V9_7_BR'
[modules/kernel.git] / src / Basics / Basics_Utils.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
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 //  File   : Basics_Utils.cxx
21 //  Author  : Alexander A. BORODIN
22 //  Module : SALOME
23 //
24
25 #include "Basics_Utils.hxx"
26 #include <string.h>
27 #include <stdlib.h>
28
29 #ifndef WIN32
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <execinfo.h>
33 #endif
34
35
36 namespace Kernel_Utils
37 {
38   // threadsafe
39   std::string GetHostname()
40   {
41     int ls = 100, r = 1;
42     char *s;
43
44     while (ls < 10000 && r)
45       {
46         ls *= 2;
47         s = new char[ls];
48         r = gethostname(s, ls-1);//threadsafe see man 7 pthread
49         switch (r)
50           {
51           case 0:
52             break;
53           default:
54 #ifdef EINVAL
55           case EINVAL:
56 #endif
57 #ifdef ENAMETOOLONG
58           case ENAMETOOLONG:
59 #endif
60 #ifdef WIN32
61           case WSAEFAULT:
62 #endif
63             delete [] s;
64             continue;
65           }
66
67       }
68
69     if (r != 0)
70       {
71         s = new char[50];
72         strcpy(s, "localhost");
73       }
74
75     // remove all after '.'
76     char *aDot = (strchr(s,'.'));
77     if (aDot) aDot[0] = '\0';
78
79     std::string p = s;
80     delete [] s;
81     return p;
82   }
83
84   Localizer::Localizer()
85   {
86     init(LC_NUMERIC, "C");
87   }
88
89   Localizer::Localizer(int category, const char* locale)
90   {
91     init(category, locale);
92   }
93
94   void Localizer::init(int category, const char* locale)
95   {
96     myCategory = category;
97     myOriginalLocale = setlocale(category, NULL);
98     setlocale(category, locale);
99   }
100
101   Localizer::~Localizer()
102   {
103     setlocale(myCategory, myOriginalLocale.c_str());
104   }
105
106   std::string GetGUID( GUIDtype type )
107   {
108     std::string guid;
109
110     switch ( type ) {
111     case DefUserID:
112       guid = "FFFFFFFF-D9CD-11d6-945D-1050DA506788"; break;
113     case ObjectdID:
114       guid = "C08F3C95-F112-4023-8776-78F1427D0B6D"; break;
115     }
116
117     return guid;
118   }
119
120   const wchar_t* decode(const char* encoded)
121   {
122     Localizer loc(LC_CTYPE, "");
123     size_t length = strlen(encoded) + sizeof(char);
124     wchar_t* decoded = new wchar_t[length];
125     memset( decoded, '\0', length);
126     mbstowcs(decoded, encoded, length);
127     return decoded;
128   }
129
130   const wchar_t* decode_s(std::string encoded)
131   {
132     return decode(encoded.c_str());
133   }
134
135   const char* encode(const wchar_t* decoded)
136   {
137     Localizer loc(LC_CTYPE, "");
138     size_t length = std::wcslen(decoded) + sizeof(wchar_t);
139     char* encoded = new char[length];
140     memset( encoded, '\0', length);
141     wcstombs(encoded, decoded, length);
142     return encoded;
143   }
144
145   std::string encode_s(const wchar_t* decoded)
146   {
147     return std::string(encode(decoded));
148   }
149
150 #ifndef WIN32
151   void print_traceback()
152   {
153     void *array[50];
154     size_t size;
155     char **strings;
156     size_t i;
157
158     size = backtrace (array, 40);
159     strings = backtrace_symbols (array, size);
160
161     for (i = 0; i < size; i++)
162       {
163         std::cerr << strings[i] << std::endl;
164       }
165
166     free (strings);
167   }
168 #else
169   #if (_MSC_VER >= 1400) // Visual Studio 2005
170   #include <sstream>
171   int setenv(const char *name, const char *value, int rewrite)
172   {
173     std::stringstream sstr;
174     sstr<<name<<'='<<value;
175     if(rewrite || std::string(getenv(name)).length() == 0)
176       return _putenv(sstr.str().c_str());
177     else return -1;
178   }
179   #endif
180 #endif
181
182 #if defined(WIN32)
183   // Convert a wide Unicode string to an UTF8 string
184   char* utf8_encode(const wchar_t* encoded)
185   {
186           if (encoded == NULL) return NULL;
187     auto size_needed = WideCharToMultiByte(CP_UTF8, 0, encoded, (int)std::wcslen(encoded), NULL, 0, NULL, NULL);
188           char* strTo = new char[ size_needed + 1 ];
189           WideCharToMultiByte(CP_UTF8, 0, encoded, (int)std::wcslen(encoded), strTo, size_needed, NULL, NULL);
190           strTo[size_needed] = '\0';
191           return strTo;
192   }
193   // Convert an UTF8 string to a wide Unicode String
194   wchar_t* utf8_decode(const char* decoded)
195   {
196           if (decoded == NULL) return NULL;
197     auto size_needed = MultiByteToWideChar(CP_UTF8, 0, decoded, (int)strlen(decoded), NULL, 0);
198           wchar_t* wstrTo = new wchar_t[ size_needed + 1 ];
199           MultiByteToWideChar(CP_UTF8, 0, decoded, (int)strlen(decoded), wstrTo, size_needed);
200           wstrTo[size_needed] = '\0';
201           return wstrTo;
202   }
203   // Convert a wide Unicode std::wstring to an UTF8 string
204   std::string utf8_encode_s(const std::wstring& encoded) {
205           char* anEncoded = utf8_encode(encoded.c_str());
206           std::string res = std::string(anEncoded);
207           delete [] anEncoded;
208           return res;
209   }
210   // Convert an UTF8 std::string to a wide Unicode std::wstring
211   std::wstring utf8_decode_s(const std::string& decoded) {
212           wchar_t* aDecoded = utf8_decode(decoded.c_str());
213           std::wstring res = std::wstring(aDecoded);
214           delete [] aDecoded;
215           return res;
216   }
217 #endif
218 }