Salome HOME
Merge from V5_1_3_BR 07/12/2009
[modules/kernel.git] / src / Basics / Basics_Utils.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : Basics_Utils.cxx
23 //  Autor  : Alexander A. BORODIN
24 //  Module : SALOME
25 //
26 #include "Basics_Utils.hxx"
27 #include <string.h>
28
29 #ifndef WIN32
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #else
33 #include <winsock2.h>
34 #endif
35
36 using namespace std;
37
38 namespace Kernel_Utils
39 {
40   string GetHostname()
41   {
42     int ls = 100, r = 1;
43     char *s;
44     
45     while (ls < 10000 && r)
46       {
47         ls *= 2;
48         s = new char[ls];
49         r = gethostname(s, ls-1);
50         switch (r) 
51           {
52           case 0:
53             break;
54           default:
55 #ifdef EINVAL
56           case EINVAL:
57 #endif
58 #ifdef ENAMETOOLONG
59           case ENAMETOOLONG:
60 #endif
61 #ifdef WIN32
62           case WSAEFAULT:  
63 #endif
64             delete [] s;
65             continue;
66           }
67         
68       }
69     
70     if (r != 0)
71       {
72         s = new char[50];
73         strcpy(s, "localhost");
74       }
75     
76     // remove all after '.'
77     char *aDot = (strchr(s,'.'));
78     if (aDot) aDot[0] = '\0';
79     
80     string p = s;
81     delete [] s;
82     return p;
83   }
84   
85   Localizer::Localizer()
86   {
87     myCurLocale = setlocale(LC_NUMERIC, 0);
88     setlocale(LC_NUMERIC, "C");
89   }
90
91   Localizer::~Localizer()
92   {
93     setlocale(LC_NUMERIC, myCurLocale.c_str());
94   }
95 }