Salome HOME
Merge changes from 'master' branch.
[modules/kernel.git] / src / Basics / Basics_Utils.cxx
1 // Copyright (C) 2007-2016  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     myCurLocale = setlocale(LC_NUMERIC, 0);
87     setlocale(LC_NUMERIC, "C");
88   }
89
90   Localizer::~Localizer()
91   {
92     setlocale(LC_NUMERIC, myCurLocale.c_str());
93   }
94
95   std::string GetGUID( GUIDtype type )
96   {
97     std::string guid;
98
99     switch ( type ) {
100     case DefUserID:
101       guid = "FFFFFFFF-D9CD-11d6-945D-1050DA506788"; break;
102     case ObjectdID:
103       guid = "C08F3C95-F112-4023-8776-78F1427D0B6D"; break;
104     }
105
106     return guid;
107   }
108
109   const wchar_t* decode(const char* encoded)
110   {
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);
116     return decoded;
117   }
118
119   const wchar_t* decode_s(std::string encoded)
120   {
121     return decode(encoded.c_str());
122   }
123
124   const char* encode(const wchar_t* decoded)
125   {
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);
131     return encoded;
132   }
133
134   std::string encode_s(const wchar_t* decoded)
135   {
136     return std::string(encode(decoded));
137   }
138
139 #ifndef WIN32
140   void print_traceback()
141   {
142     void *array[50];
143     size_t size;
144     char **strings;
145     size_t i;
146
147     size = backtrace (array, 40);
148     strings = backtrace_symbols (array, size);
149
150     for (i = 0; i < size; i++)
151       {
152         std::cerr << strings[i] << std::endl;
153       }
154
155     free (strings);
156   }
157 #else
158   #if (_MSC_VER >= 1400) // Visual Studio 2005
159   #include <sstream>
160   int setenv(const char *name, const char *value, int rewrite)\r
161   {\r
162     std::stringstream sstr;\r
163     sstr<<name<<'='<<value;\r
164     if(rewrite || std::string(getenv(name)).length() == 0)\r
165       return _putenv(sstr.str().c_str());\r
166     else return -1;\r
167   }
168   #endif
169 #endif
170
171 }