X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FBasics%2FBasics_DirUtils.cxx;h=e3f224c3b644ed3a156849fa1f71582bcd427c4f;hb=1bbffef73eeeb5b2a14d8fc7bdf91affdafae664;hp=22359e8d393f65294e42f3468f6041f389366953;hpb=288dc1c84630e521220d796b7c88c518f34856d5;p=modules%2Fkernel.git diff --git a/src/Basics/Basics_DirUtils.cxx b/src/Basics/Basics_DirUtils.cxx index 22359e8d3..e3f224c3b 100644 --- a/src/Basics/Basics_DirUtils.cxx +++ b/src/Basics/Basics_DirUtils.cxx @@ -1,9 +1,9 @@ -// Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE // // 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. +// version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,21 +18,28 @@ // // File : Basics_DirUtils.cxx -// Autor : Alexander A. BORODIN +// Author : Alexander A. BORODIN // Module : SALOME // #include "Basics_DirUtils.hxx" +#include "Basics_Utils.hxx" #include #include #include +#include + #ifndef WIN32 -# include -# include -# include +#include +#include +#include #else -# include -# include +#include +#define F_OK 0 +#define access _access +#include +#include +#include #endif #ifdef WIN32 @@ -41,89 +48,122 @@ # define _separator_ '/' #endif +#define _extension_ ".hdf" + namespace Kernel_Utils { - std::string GetBaseName( const std::string& file_path ) + std::string GetBaseName( const std::string& file_path, const bool with_extension ) { - int pos = file_path.rfind( _separator_ ); - if ( pos >= 0 ) - return pos < (int)file_path.size()-1 ? file_path.substr( pos+1 ) : ""; - return file_path; + std::string tmp_str = file_path; + auto pos = file_path.rfind( _separator_ ); + if ( pos != std::string::npos ) + tmp_str = pos < file_path.size()-1 ? file_path.substr( pos+1 ) : ""; + + pos = tmp_str.rfind( _extension_ ); + if( !with_extension && pos != std::string::npos ) + tmp_str = pos < tmp_str.size()-1 ? tmp_str.substr( 0, pos ) : ""; + + return tmp_str; } std::string GetDirName( const std::string& file_path ) { - int pos = file_path.rfind( _separator_ ); - if ( pos >= 0 ) - return pos < (int)file_path.size()-1 ? file_path.substr(0, pos ) : ""; + auto pos = file_path.rfind( _separator_ ); + if ( pos != std::string::npos ) + return pos < file_path.size()-1 ? file_path.substr(0, pos ) : ""; return std::string("."); } std::string GetTmpDirByEnv( const std::string& tmp_path_env ) { +#if defined WIN32 && defined UNICODE + std::wstring w_tmp_path_env = utf8_decode_s( tmp_path_env ); + wchar_t* val = _wgetenv( w_tmp_path_env.c_str() ); + std::string dir = val ? utf8_encode_s(val) : ""; +#else char* val = getenv( tmp_path_env.c_str() ); - std::string dir = val ? val : ""; + std::string dir = val ? val : ""; +#endif return GetTmpDirByPath( dir ); } std::string GetTmpDirByPath( const std::string& tmp_path ) { std::string aTmpDir = tmp_path; - if ( aTmpDir == "" ) - { + if ( aTmpDir == "" || !IsExists( aTmpDir )) + { #ifdef WIN32 - char *Tmp_dir = getenv("TEMP"); - if( Tmp_dir == NULL ) - { - Tmp_dir = getenv("TMP"); - if (Tmp_dir == NULL) - aTmpDir = std::string("C:\\"); - else - aTmpDir = std::string(Tmp_dir); - } +#ifdef UNICODE + wchar_t *wTmp_dir = _wgetenv( L"TEMP" ); + if ( wTmp_dir == NULL ) { + wTmp_dir = _wgetenv(L"TMP"); + if (wTmp_dir == NULL) + wTmp_dir = L"C:\\"; + } + aTmpDir = utf8_encode_s( wTmp_dir ); +#else + char *Tmp_dir = getenv("TEMP"); + if ( Tmp_dir == NULL ) + { + Tmp_dir = getenv("TMP"); + if ( Tmp_dir == NULL ) + aTmpDir = "C:\\"; else - aTmpDir = std::string(Tmp_dir); + aTmpDir = Tmp_dir; + } + else + aTmpDir = Tmp_dir; +#endif #else - aTmpDir = std::string("/tmp/"); + aTmpDir = "/tmp/"; #endif - } - - if(aTmpDir[aTmpDir.size()-1] != _separator_) - aTmpDir+=_separator_; - - srand((unsigned int)time(NULL)); + } + + if ( aTmpDir.back() != _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); - std::string aSubDir(buffer); - if(aSubDir.size() <= 1) aSubDir = std::string("123409876"); - + sprintf( buffer, "%d", aRND ); + std::string aSubDir( buffer ); + if ( aSubDir.size() <= 1 ) aSubDir = "123409876"; + aTmpDir += aSubDir; //Get RND sub directory - - std::string aDir = aTmpDir; - - if(IsExists(aDir)) { - for(aRND = 0; IsExists(aDir); aRND++) { - sprintf(buffer, "%d", aRND); - aDir = aTmpDir+buffer; //Build a unique directory name - } + +#ifdef WIN32 + int pid = _getpid(); +#else + int pid = getpid(); +#endif + + std::string aDir = aTmpDir + std::to_string(pid) + "_"; + + 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_; - + + if ( aDir.back() != _separator_ ) aDir += _separator_; + #ifdef WIN32 - CreateDirectory(aDir.c_str(), NULL); +#ifdef UNICODE + std::wstring aDirToCreate = utf8_decode_s(aDir); +#else + std::string aDirToCreate = aDir; +#endif + CreateDirectory( aDirToCreate.c_str(), NULL ); #else - mkdir(aDir.c_str(), 0x1ff); + mkdir( aDir.c_str(), 0x1ff ); #endif - return aDir; } - + //============================================================================ // function : GetTempDir - // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/" - //============================================================================ + // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/" + //============================================================================ std::string GetTmpDir() { return GetTmpDirByPath( "" ); @@ -132,7 +172,7 @@ namespace Kernel_Utils //============================================================================ // function : GetTempFileName // purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32 - //============================================================================ + //============================================================================ std::string GetTmpFileName() { std::string tmpDir = GetTmpDir(); @@ -144,7 +184,7 @@ namespace Kernel_Utils sprintf(buffer, "%d", aRND); std::string aSubDir(buffer); if(aSubDir.size() <= 1) aSubDir = std::string("123409876"); - + aFilePath = tmpDir; for(aRND = 0; IsExists(aFilePath); aRND++) { sprintf(buffer, "%d", aRND); @@ -153,40 +193,50 @@ namespace Kernel_Utils } return aFilePath; } - + + std::string AddExtension( const std::string& name ) + { + std::string tmp_str = name; + auto pos = tmp_str.rfind( _extension_ ); + if( pos == std::string::npos ) + return tmp_str.append( _extension_ ); + return tmp_str; + } + //============================================================================ // function : IsExists // purpose : Returns True(False) if the path (not)exists - //============================================================================ - bool IsExists(const std::string& thePath) + //============================================================================ + bool IsExists(const std::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; +#if defined WIN32 && defined UNICODE + int status = _waccess( utf8_decode_s( thePath).c_str(), F_OK ); +#else + int status = access ( thePath.c_str() , F_OK ); #endif + if (status != 0) return false; return true; } //============================================================================ // function : IsWritable // purpose : Returns True(False) if the path is (not) writable - //============================================================================ - bool IsWritable(const std::string& thePath) + //============================================================================ + bool IsWritable(const std::string& thePath) { -#ifdef WIN32 - if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) { +#ifdef WIN32 +#ifdef UNICODE + std::wstring aPathToCheck = utf8_decode_s( thePath ); +#else + std::string aPathToCheck = thePath; +#endif + if ( GetFileAttributes ( aPathToCheck.c_str() ) == 0xFFFFFFFF ) { if ( GetLastError () == FILE_ATTRIBUTE_READONLY ) { return false; } } -#else - int status = access(thePath.c_str(),W_OK); +#else + int status = access(thePath.c_str(),W_OK); if (status != 0) return false; #endif return true; @@ -196,7 +246,7 @@ namespace Kernel_Utils //============================================================================ // function : GetDirByPath // purpose : Returns directory by path and converts it to native system format - //============================================================================ + //============================================================================ std::string GetDirByPath(const std::string& thePath) { if (thePath.empty()) @@ -250,8 +300,8 @@ namespace Kernel_Utils // function : IsEmptyDir // purpose : Returns True(False) if the path (not) empty // Also returns False if the path is not valid - //============================================================================ - bool IsEmptyDir(const std::string& thePath) + //============================================================================ + bool IsEmptyDir(const std::string& thePath) { if ( thePath.empty() || !IsExists(thePath)) return false; @@ -260,7 +310,13 @@ namespace Kernel_Utils #ifdef WIN32 WIN32_FIND_DATA aFileData; - HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData ); +#ifdef UNICODE + std::wstring aPathToCheck = utf8_decode_s(thePath); +#else + std::string aPathToCheck = thePath; +#endif + + HANDLE hFile = FindFirstFile( aPathToCheck.c_str(), &aFileData ); if ( hFile == INVALID_HANDLE_VALUE ) { //empty dir @@ -292,4 +348,28 @@ namespace Kernel_Utils #endif return result; } + + //============================================================================ + // function : BackSlashToSlash + // purpose : Convert back slash to slash + //============================================================================ + std::string BackSlashToSlash(const std::string& path) { + std::string res = path; + std::replace(res.begin(), res.end(), '\\', '/'); + return res; + } + + + //============================================================================ + // function : BackSlashToSlash + // purpose : Convert back slash to slash + //============================================================================ + std::string HomePath() { +#ifdef WIN32 + std::string homedir = getenv("USERPROFILE"); +#else + std::string homedir = getenv("HOME"); +#endif + return homedir; + } }