Salome HOME
Merge branch 'V7_main'
[modules/kernel.git] / src / Basics / Basics_Utils.cxx
1 // Copyright (C) 2007-2014  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 //  Autor  : 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   std::string GetHostname()
39   {
40     int ls = 100, r = 1;
41     char *s;
42     
43     while (ls < 10000 && r)
44       {
45         ls *= 2;
46         s = new char[ls];
47         r = gethostname(s, ls-1);
48         switch (r) 
49           {
50           case 0:
51             break;
52           default:
53 #ifdef EINVAL
54           case EINVAL:
55 #endif
56 #ifdef ENAMETOOLONG
57           case ENAMETOOLONG:
58 #endif
59 #ifdef WIN32
60           case WSAEFAULT:  
61 #endif
62             delete [] s;
63             continue;
64           }
65         
66       }
67     
68     if (r != 0)
69       {
70         s = new char[50];
71         strcpy(s, "localhost");
72       }
73     
74     // remove all after '.'
75     char *aDot = (strchr(s,'.'));
76     if (aDot) aDot[0] = '\0';
77     
78     std::string p = s;
79     delete [] s;
80     return p;
81   }
82   
83   Localizer::Localizer()
84   {
85     myCurLocale = setlocale(LC_NUMERIC, 0);
86     setlocale(LC_NUMERIC, "C");
87   }
88
89   Localizer::~Localizer()
90   {
91     setlocale(LC_NUMERIC, myCurLocale.c_str());
92   }
93
94   std::string GetGUID( GUIDtype type )
95   {
96     std::string guid;
97
98     switch ( type ) {
99     case DefUserID:
100       guid = "FFFFFFFF-D9CD-11d6-945D-1050DA506788"; break;
101     case ObjectdID:
102       guid = "C08F3C95-F112-4023-8776-78F1427D0B6D"; break;
103     }
104
105     return guid;
106   }
107
108 #ifndef WIN32
109   void print_traceback()
110   {
111     void *array[50];
112     size_t size;
113     char **strings;
114     size_t i;
115
116     size = backtrace (array, 40);
117     strings = backtrace_symbols (array, size);
118
119     for (i = 0; i < size; i++)
120       {
121         std::cerr << strings[i] << std::endl;
122       }
123
124     free (strings);
125   }
126 #else
127   #if (_MSC_VER >= 1400) // Visual Studio 2005
128   #include <sstream>
129   int setenv(const char *name, const char *value, int rewrite)\r
130   {\r
131     std::stringstream sstr;\r
132     sstr<<name<<'='<<value;\r
133     if(rewrite || std::string(getenv(name)).length() == 0)\r
134       return _putenv(sstr.str().c_str());\r
135     else return -1;\r
136   }
137   #endif
138 #endif
139
140 }