From 51c5a95906c377ea9b3888b2fc84e66bfab57e42 Mon Sep 17 00:00:00 2001 From: abd Date: Fri, 10 Oct 2008 09:40:17 +0000 Subject: [PATCH] Merging from V4_1_0_maintainance for porting on Win32 Platform --- src/Basics/BasicsGenericDestructor.cxx | 2 +- src/Basics/BasicsGenericDestructor.hxx | 18 +- src/Basics/Basics_DirUtils.cxx | 262 +++++++++++++++++++++++++ src/Basics/Basics_DirUtils.hxx | 65 ++++++ src/Basics/Basics_Utils.cxx | 84 ++++++++ src/Basics/Basics_Utils.hxx | 40 ++++ src/Basics/Makefile.am | 13 +- src/Basics/SALOME_Basics.hxx | 43 ++++ 8 files changed, 507 insertions(+), 20 deletions(-) create mode 100644 src/Basics/Basics_DirUtils.cxx create mode 100644 src/Basics/Basics_DirUtils.hxx create mode 100644 src/Basics/Basics_Utils.cxx create mode 100644 src/Basics/Basics_Utils.hxx create mode 100644 src/Basics/SALOME_Basics.hxx diff --git a/src/Basics/BasicsGenericDestructor.cxx b/src/Basics/BasicsGenericDestructor.cxx index deca0e9c9..4f6cf3397 100644 --- a/src/Basics/BasicsGenericDestructor.cxx +++ b/src/Basics/BasicsGenericDestructor.cxx @@ -37,7 +37,7 @@ using namespace std; void HouseKeeping(); std::list PROTECTED_DELETE::_objList; -#ifndef WNT +#ifndef WIN32 pthread_mutex_t PROTECTED_DELETE::_listMutex; #else pthread_mutex_t PROTECTED_DELETE::_listMutex = diff --git a/src/Basics/BasicsGenericDestructor.hxx b/src/Basics/BasicsGenericDestructor.hxx index 9ccbb1749..36919df09 100644 --- a/src/Basics/BasicsGenericDestructor.hxx +++ b/src/Basics/BasicsGenericDestructor.hxx @@ -29,23 +29,7 @@ #ifndef _BASICGENERICDESTRUCTOR_HXX_ #define _BASICGENERICDESTRUCTOR_HXX_ -#ifdef WNT - #if defined BASICS_EXPORTS - #if defined WIN32 - #define BASICS_EXPORT __declspec( dllexport ) - #else - #define BASICS_EXPORT - #endif - #else - #if defined WIN32 - #define BASICS_EXPORT __declspec( dllimport ) - #else - #define BASICS_EXPORT - #endif - #endif -#else - #define BASICS_EXPORT -#endif +#include "SALOME_Basics.hxx" #include #include diff --git a/src/Basics/Basics_DirUtils.cxx b/src/Basics/Basics_DirUtils.cxx new file mode 100644 index 000000000..9b6afd8c3 --- /dev/null +++ b/src/Basics/Basics_DirUtils.cxx @@ -0,0 +1,262 @@ +// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// +// +// File : Basics_DirUtils.cxx +// Autor : Alexander A. BORODIN +// Module : SALOME + +#include "Basics_DirUtils.hxx" + +#include + +#ifndef WIN32 +# include +# include +#else +# include +# include +#endif + +using namespace std; + +#ifdef WIN32 +# define _separator_ '\\' +#else +# define _separator_ '/' +#endif + +namespace Kernel_Utils +{ + string GetTmpDirByEnv( const std::string& tmp_path_env ) + { + string dir; + char* val = getenv( tmp_path_env.c_str() ); + val ? dir = string( val ) : ""; + return GetTmpDirByPath( dir ); + } + + string GetTmpDirByPath( const std::string& tmp_path ) + { + string aTmpDir = tmp_path; + if ( aTmpDir == "" ) + { +#ifdef WIN32 + char *Tmp_dir = getenv("TEMP"); + if( Tmp_dir == NULL ) + { + Tmp_dir = getenv("TMP"); + if (Tmp_dir == NULL) + aTmpDir = string("C:\\"); + else + aTmpDir = string(Tmp_dir); + } + else + aTmpDir = string(Tmp_dir); +#else + aTmpDir = string("/tmp/"); +#endif + } + + if(aTmpDir[aTmpDir.size()-1] != _separator_) + aTmpDir+=_separator_; + + srand((unsigned int)time(NULL)); + int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory + char buffer[127]; + sprintf(buffer, "%d", aRND); + string aSubDir(buffer); + if(aSubDir.size() <= 1) aSubDir = string("123409876"); + + aTmpDir += aSubDir; //Get RND sub directory + + string aDir = aTmpDir; + + if(IsExists(aDir)) { + for(aRND = 0; IsExists(aDir); aRND++) { + sprintf(buffer, "%d", aRND); + aDir = aTmpDir+buffer; //Build a unique directory name + } + } + + if(aDir[aDir.size()-1] != _separator_) aDir += _separator_; + +#ifdef WIN32 + CreateDirectory(aDir.c_str(), NULL); +#else + mkdir(aDir.c_str(), 0x1ff); +#endif + + return aDir; + } + + //============================================================================ + // function : GetTempDir + // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/" + //============================================================================ + string GetTmpDir() + { + return GetTmpDirByPath( "" ); + } + + //============================================================================ + // function : GetTempFileName + // purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32 + //============================================================================ + string GetTmpFileName() + { + string tmpDir = GetTmpDir(); + string aFilePath = ""; + if(IsExists(tmpDir)) { + srand((unsigned int)time(NULL)); + int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory + char buffer[127]; + sprintf(buffer, "%d", aRND); + string aSubDir(buffer); + if(aSubDir.size() <= 1) aSubDir = string("123409876"); + + aFilePath = tmpDir; + for(aRND = 0; IsExists(aFilePath); aRND++) { + sprintf(buffer, "%d", aRND); + aFilePath = tmpDir+buffer; //Build a unique file name + } + } + return aFilePath; + } + + //============================================================================ + // function : IsExists + // purpose : Returns True(False) if the path (not)exists + //============================================================================ + bool IsExists(const string& thePath) + { +#ifdef WIN32 + if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) { + if ( GetLastError () == ERROR_FILE_NOT_FOUND ) { + return false; + } + } +#else + int status = access ( thePath.c_str() , F_OK ); + if (status != 0) return false; +#endif + return true; + } + + //============================================================================ + // function : GetDirByPath + // purpose : Returns directory by path and converts it to native system format + //============================================================================ + string GetDirByPath(const string& thePath) + { + if (thePath.empty()) + return ""; + string path = thePath; + string::size_type length = path.length(); + + //detect all separators in Unix format + for ( int i = 0; i < length; i++ ) + { + if( path[i] == '/' ) + path[i] = '|'; + } + + //detect all separators in Windows format + for ( int i = 0; i < length; i++ ) + { + if( path[i] == '\\' ) + path[i] = '|'; + } + + + string::size_type pos = path.rfind('|'); + if ( pos == string::npos ) + { +#ifdef WIN32 + //check for disk letter ( C: ) + if ( path.length() == 2 && path[1] == ':' ) + path += _separator_; +#else + //not valid path + return ""; +#endif + } + else + { + //remove right subdirectory or filename from path + path = path.substr( 0, pos ); + } + + length = path.length(); + for ( int i = 0; i < length; i++ ) + { + if( path[i] == '|' ) + path[i] = _separator_; + } + return path; + } + + //============================================================================ + // function : IsEmptyDir + // purpose : Returns True(False) if the path (not) empty + // Also returns False if the path is not valid + //============================================================================ + bool IsEmptyDir(const string& thePath) + { + if ( thePath.empty() || !IsExists(thePath)) + return false; + + bool result = false; + +#ifdef WIN32 + WIN32_FIND_DATA aFileData; + HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData ); + if ( hFile == INVALID_HANDLE_VALUE ) + { + //empty dir + result = true; + } + else + { + //close serching. path is not empty + FindClose( hFile ); + } +#else + DIR *dp; + struct dirent *dirp; + if((dp = opendir(thePath.c_str())) == NULL) + { + //Could not open directory + return false; + } + else + { + result = true; //empty if no file found + while ((dirp = readdir(dp)) != NULL && result ) + { + string file_name(dirp->d_name); + result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false + } + closedir(dp); + } +#endif + return result; + } +} diff --git a/src/Basics/Basics_DirUtils.hxx b/src/Basics/Basics_DirUtils.hxx new file mode 100644 index 000000000..5a64a84f9 --- /dev/null +++ b/src/Basics/Basics_DirUtils.hxx @@ -0,0 +1,65 @@ +// SALOME Utils : general SALOME's definitions and tools +// +// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// +// +// File : Basics_DirUtils.hxx +// Autor : Alexander A. BORODIN +// Module : SALOME + +#ifndef _Basics_DIRUTILS_HXX_ +#define _Basics_DIRUTILS_HXX_ + +#include "SALOME_Basics.hxx" + +#include + +namespace Kernel_Utils +{ + // Returns the unique temporary directory, that is defined in tmp_path_env if this variable is set + // otherwise return /tmp/something/ for Unix or c:\something\ for WIN32 + BASICS_EXPORT std::string GetTmpDirByEnv( const std::string& tmp_path_env ); + + // Returns the unique temporary directory, that is defined in tmp_path if this variable is set + // otherwise return /tmp/something/ for Unix or c:\something\ for WIN32 + BASICS_EXPORT std::string GetTmpDirByPath( const std::string& tmp_path ); + + // Returns the unique temporary directory in + // /tmp/something/ for Unix or c:\something\ for WIN32 + BASICS_EXPORT std::string GetTmpDir(); + + + // Returns the unique temporary file name without any extension + // /tmp/something/file for Unix or c:\something\file for WIN32 + BASICS_EXPORT std::string GetTmpFileName(); + + // Returns True(False) if the path (not)exists + BASICS_EXPORT bool IsExists( const std::string& path ); + + // Returns directory by path and converts it to native system format + BASICS_EXPORT std::string GetDirByPath( const std::string& path ); + + // Returns True(False) if the path (not) empty + // Also returns False if the path is not valid + BASICS_EXPORT bool IsEmptyDir( const std::string& path ); +} + +#endif diff --git a/src/Basics/Basics_Utils.cxx b/src/Basics/Basics_Utils.cxx new file mode 100644 index 000000000..b6e119b2f --- /dev/null +++ b/src/Basics/Basics_Utils.cxx @@ -0,0 +1,84 @@ +// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// +// +// File : Basics_Utils.cxx +// Autor : Alexander A. BORODIN +// Module : SALOME + +#include "Basics_Utils.hxx" + +#ifndef WIN32 +#include +#include +#else +#include +#endif + +using namespace std; + +namespace Kernel_Utils +{ + string GetHostname() + { + int ls = 100, r = 1; + char *s; + + while (ls < 10000 && r) + { + ls *= 2; + s = new char[ls]; + r = gethostname(s, ls-1); + switch (r) + { + case 0: + break; + default: +#ifdef EINVAL + case EINVAL: +#endif +#ifdef ENAMETOOLONG + case ENAMETOOLONG: +#endif +#ifdef WIN32 + case WSAEFAULT: +#endif + delete [] s; + continue; + } + + } + + if (r != 0) + { + s = new char[50]; + strcpy(s, "localhost"); + } + + // remove all after '.' + char *aDot = (strchr(s,'.')); + if (aDot) aDot[0] = '\0'; + + string p = s; + delete [] s; + return p; + } + +} diff --git a/src/Basics/Basics_Utils.hxx b/src/Basics/Basics_Utils.hxx new file mode 100644 index 000000000..9fb86c68b --- /dev/null +++ b/src/Basics/Basics_Utils.hxx @@ -0,0 +1,40 @@ +// SALOME Utils : general SALOME's definitions and tools +// +// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// +// +// File : Basics_DirUtils.hxx +// Autor : Alexander A. BORODIN +// Module : SALOME + +#ifndef _Basics_UTILS_HXX_ +#define _Basics_UTILS_HXX_ + +#include "SALOME_Basics.hxx" + +#include + +namespace Kernel_Utils +{ + BASICS_EXPORT std::string GetHostname(); +} + +#endif //_Basics_UTILS_HXX_ diff --git a/src/Basics/Makefile.am b/src/Basics/Makefile.am index 02dc1f1f4..4e4ba8061 100644 --- a/src/Basics/Makefile.am +++ b/src/Basics/Makefile.am @@ -28,11 +28,20 @@ include $(top_srcdir)/salome_adm/unix/make_common_starter.am -salomeinclude_HEADERS = BasicsGenericDestructor.hxx +salomeinclude_HEADERS = \ + SALOME_Basics.hxx \ + BasicsGenericDestructor.hxx \ + Basics_Utils.hxx \ + Basics_DirUtils.hxx lib_LTLIBRARIES = libSALOMEBasics.la libSALOMEBasics_la_SOURCES = \ BasicsGenericDestructor.cxx \ - BasicsGenericDestructor.hxx + Basics_Utils.cxx \ + Basics_DirUtils.cxx \ + SALOME_Basics.hxx \ + BasicsGenericDestructor.hxx \ + Basics_Utils.hxx \ + Basics_DirUtils.hxx libSALOMEBasics_la_LDFLAGS = -no-undefined -version-info=0:0:0 libSALOMEBasics_la_CPPFLAGS = @CPPFLAGS@ diff --git a/src/Basics/SALOME_Basics.hxx b/src/Basics/SALOME_Basics.hxx new file mode 100644 index 000000000..bbbb47227 --- /dev/null +++ b/src/Basics/SALOME_Basics.hxx @@ -0,0 +1,43 @@ +// SALOME Basics : general SALOME definitions and tools (C++ part - no CORBA) +// +// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// +// +// File : SALOME_Basics.hxx +// Author : Alexander A. BORODIN +// Module : SALOME +// $Header$ + +#ifndef _SALOME_BASICS_HXX_ +#define _SALOME_BASICS_HXX_ + + +#ifdef WIN32 +# ifdef BASICS_EXPORTS +# define BASICS_EXPORT __declspec( dllexport ) +# else +# define BASICS_EXPORT __declspec( dllimport ) +# endif +#else +# define BASICS_EXPORT +#endif + +#endif //_SALOME_BASICS_HXX_ -- 2.39.2