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