Back to index

salome-kernel  6.5.0
Basics_Utils.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 //  File   : Basics_Utils.cxx
00021 //  Autor  : Alexander A. BORODIN
00022 //  Module : SALOME
00023 //
00024 
00025 #include "Basics_Utils.hxx"
00026 #include <string.h>
00027 #include <stdlib.h>
00028 
00029 #ifndef WIN32
00030 #include <unistd.h>
00031 #include <sys/stat.h>
00032 #include <execinfo.h>
00033 #endif
00034 
00035 
00036 namespace Kernel_Utils
00037 {
00038   std::string GetHostname()
00039   {
00040     int ls = 100, r = 1;
00041     char *s;
00042     
00043     while (ls < 10000 && r)
00044       {
00045         ls *= 2;
00046         s = new char[ls];
00047         r = gethostname(s, ls-1);
00048         switch (r) 
00049           {
00050           case 0:
00051             break;
00052           default:
00053 #ifdef EINVAL
00054           case EINVAL:
00055 #endif
00056 #ifdef ENAMETOOLONG
00057           case ENAMETOOLONG:
00058 #endif
00059 #ifdef WIN32
00060           case WSAEFAULT:  
00061 #endif
00062             delete [] s;
00063             continue;
00064           }
00065         
00066       }
00067     
00068     if (r != 0)
00069       {
00070         s = new char[50];
00071         strcpy(s, "localhost");
00072       }
00073     
00074     // remove all after '.'
00075     char *aDot = (strchr(s,'.'));
00076     if (aDot) aDot[0] = '\0';
00077     
00078     std::string p = s;
00079     delete [] s;
00080     return p;
00081   }
00082   
00083   Localizer::Localizer()
00084   {
00085     myCurLocale = setlocale(LC_NUMERIC, 0);
00086     setlocale(LC_NUMERIC, "C");
00087   }
00088 
00089   Localizer::~Localizer()
00090   {
00091     setlocale(LC_NUMERIC, myCurLocale.c_str());
00092   }
00093 
00094   std::string GetGUID( GUIDtype type )
00095   {
00096     std::string guid;
00097 
00098     switch ( type ) {
00099     case DefUserID:
00100       guid = "FFFFFFFF-D9CD-11d6-945D-1050DA506788"; break;
00101     case ObjectdID:
00102       guid = "C08F3C95-F112-4023-8776-78F1427D0B6D"; break;
00103     }
00104 
00105     return guid;
00106   }
00107 
00108 #ifndef WIN32
00109   void print_traceback()
00110   {
00111     void *array[50];
00112     size_t size;
00113     char **strings;
00114     size_t i;
00115 
00116     size = backtrace (array, 40);
00117     strings = backtrace_symbols (array, size);
00118 
00119     for (i = 0; i < size; i++)
00120       {
00121         std::cerr << strings[i] << std::endl;
00122       }
00123 
00124     free (strings);
00125   }
00126 #endif
00127 
00128 }